这是我的代码段:
user, err := user.Current()
key, err := ioutil.ReadFile(fmt.Sprintf("%s/.ssh/id_rsa", user.HomeDir))
fmt.Println(string(key)) // prints valid data
signer, err := ssh.ParsePrivateKeyWithPassphrase(key, []byte("test"))
if err != nil {
// error happens here
log.Fatalf("unable to parse private key: %v", err)
}
输出:
unable to parse private key: ssh: cannot decode encrypted private keys
这是我的id_rsa文件:
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABD5f5tjBE
IeNkF+DmGNAQLaAAAAEAAAAAEAAAEXAAAAB3NzaC1yc2EAAAADAQABAAABAQDSHOjIEPNh
9Qf0l7ILzAY5csC5fBrcykj8K49N6OLIofClwVxeeT9ucJAYz9um3QSnYBstxcozN+EG30
7b7gPCGEDLZbB2bOeyU1Wda8wBm6Jy3V1xw6XDvdC4ZZJeGEjBKeY/5l/scAurfcjNo6rG
dFNMyY8RAQb5XZEzvhzDJGebYH3ezMTf9zb2RXP8anud/qdAJMvm8uICSAg4gzI0C+fC8+
t7SeE6BWLHUNKyBLXB2/b74oXJCIgYou1pBd2xol/lSM9rqSNanzn7yhj4s1+BTFlJaijH
+xVxac3sbK9AfCPoq3zQA1W1QZQDghMMSd0OuQg6TR3w4X8rq3UBAAAD0FsXxxJuKOB9Pb
zNrZ69tHKBsTirWNkLq/0MfNJlqNCRYArsq0PH2IWiioKp2mQgRwXwIfrM71LnpemXmS/F
rtjJAabNifGfCT6v7BGM1OubFh+n3Y8ejSK8rPLh25m/KFwWABEwFkoXpSiY2e07Y8cf5i
BmnMpSDlmgkWUipRlDZq1NvXYovAlFzlzmbAlC2utqjiA+D2V/Pc+rI0E/t/iS2LuW4Y5q
7OfWxPynbQuKkt16cXVK1oXUOa7KJoy8+v7MJQRHP6Ipurrfn23DXyM+xXmMRBy9Ai+JI8
ffFh8DzCjL+AM88eHF13fwLMa1IXfvbJKsHk/SsoLq62EDinfvpTEUa/FmQLsiXpYKKm+r
kTvvTmcCTch0C24Haa5UxKrqD7F7V9i3nLYP7ot9gOJ2XkLWczYvnZJRlIEFuNiObxKuTe
3aUxWSQloVq8LwQR/N5fACH4nZmafk0JsXNq6ImBRwP0a7AxTZ6F9pAt57XjTYNAER6SBv
oHNxMnwxt31/kV+7SYs1OgburqBOYBDgtdjT4n2VlBtHjmiflxeychlr4G7zvR9hhK+fZl
AoskIJNYqrHgBimlTYwzvy+fJr2DbTZFkuyQyhzFBAtNnV04YWufAnPl/r7fB4GrNdqksi
pxAHUWpJLY47q0GIAMwdMt8lEBcQ1okg+9PLOsXOZCjkOx1/dcaRmNKC9TEU/0tvgIDybL
HVL15S/cZz19wNAt1Qvvx8LjNyq6xm0hbeRUldm8r0BQanK6Y6ReImnjwm9pojd7N/nPMt
E/zf0whJgzVikqt95SbCkyaW9wkMOayjxrm6b6hhNthldAgcz/vGQKNd8gyvUmgauHHrMI
NgbQL1N6dBY5rNF+SGPelEDNQu8n1l0sq0ln7BA3RiXVMff/FxgvV2b0DSTa4d7MfLm7Xi
S/2Cn1Tzwi+90m62zU38KbQJLAhXrB0KumFcOM3Tmp2V+kdqu2E0F2AOTs1uzTOzBALXSd
fThlLXxVS5VVrAbQajneVWRg741iHqFR+9tDRlOunjPfJ2v2PGqDyVEIVFsJ4d+mhzCM3z
Z2KNv1gjiOn9bgyfKldE8xA2ee+sax6HIUSa2u54LpWAhgGtJjAaYA9tWabjdTaKIpiFo6
DSIa39Kx7hkEjLstCN+OCzSGqiVw8RUsLcOMJi6gBzMTalgp8KeMkXwT2GUOvfFC1aHecJ
F2vApS+VW331FWYxNYPCAKbJx8iaKUZ7Ca6whPqwyB7lvdtjmi+3vREu3m4yROwDkknivc
SF7LrsG2766/Z09Cr+V7fT4yDP3EE=
-----END OPENSSH PRIVATE KEY-----
密钥的密码短语为test
,我可以使用ssh-keygen -y
进行验证