Encryption Decode-给定id_rsa.pem的内容时返回nil
Encryption Decode-给定id_rsa.pem的内容时返回nil,encryption,go,rsa,public-key-encryption,Encryption,Go,Rsa,Public Key Encryption,我正在尝试使用rsa包对encrypt和decrypt使用public和private密钥 func main() { flag.Parse() text := "my super secret" glog.Infof("Original text: %s\n", text) glog.Infoln("Loading public key..") pubKey, err := loadPublicKey("id_rsa.pub") if err
我正在尝试使用
rsa
包对encrypt
和decrypt
使用public
和private
密钥
func main() {
flag.Parse()
text := "my super secret"
glog.Infof("Original text: %s\n", text)
glog.Infoln("Loading public key..")
pubKey, err := loadPublicKey("id_rsa.pub")
if err == nil {
glog.Infoln("SUCCESS!")
} else {
glog.Errorln("LOOSE =(")
}
etext, err := encrypt([]byte(text), pubKey)
if err != nil {
glog.Errorf("Can't encrypt text: %+v\n", err)
}
glog.Infof("Encrypted text: %s\n", etext)
glog.Infoln("Loading private key..")
privKey, err := loadPrivateKey("id_rsa")
if err == nil {
glog.Infoln("SUCCESS!")
} else {
glog.Errorln("LOOSE =(")
}
dtext, err := decrypt(etext, privKey)
if err != nil {
glog.Errorf("Can't decrypt text: %+v\n", err)
}
glog.Infof("Decrypted text: %s\n", dtext)
glog.Flush()
}
func loadPublicKey(path string) (*rsa.PublicKey, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("no key found\n")
}
if block.Type != "PUBLIC KEY" {
return nil, fmt.Errorf("invalid key type - %s\n", block.Type)
}
pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("can't parse key - %+v\n", err)
}
return pubKey.(*rsa.PublicKey), nil
}
func loadPrivateKey(path string) (*rsa.PrivateKey, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("no key found\n")
}
if block.Type != "PRIVATE KEY" {
return nil, fmt.Errorf("invalid key type - %s\n", block.Type)
}
return x509.ParsePKCS1PrivateKey(block.Bytes)
}
func encrypt(data []byte, pubKey *rsa.PublicKey) ([]byte, error) {
return rsa.EncryptPKCS1v15(rand.Reader, pubKey, data)
}
func decrypt(data []byte, privKey *rsa.PrivateKey) ([]byte, error) {
return rsa.DecryptPKCS1v15(rand.Reader, privKey, data)
}
但是我在rsa中有一个错误。EncryptPKCS1v15:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x5c16c]
goroutine 1 [running]:
crypto/rsa.EncryptPKCS1v15(0x11641f8, 0xc8200762a0, 0x0, 0xc82004ddc8, 0xf, 0x20, 0x0, 0x0, 0x0, 0x0, ...)
/usr/local/go/src/crypto/rsa/pkcs1v15.go:32 +0x5c
main.encrypt(0xc82004ddc8, 0xf, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
/Users/cnaize/Dropbox/develop/gocode/src/test/main.go:132 +0x87
main.main()
/Users/cnaize/Dropbox/develop/gocode/src/test/main.go:67 +0x3b8
为什么错误在哪里?如何解决这个问题
已编辑:
谢谢,现在我了解了这个问题-在
loadPublicKey(“id\u rsa.pub”)
pem.Decode(data)
返回nil block
。
解析
ssh
密钥的正确方法是什么?请使用错误的完整输出进行编辑(包括堆栈跟踪)。另外,您肯定应该处理EncryptPKCS1v15返回的错误(不是问题的根源,但它将是未来问题的根源)。@voutasauruspem.Decode
不提供任何错误,它只返回nil block
哦,对了。所以你的数据可能不正确。输入log.Println(数据)并查看它是否与您的文件匹配。如果是,则文件本身不是pem格式。检查文件是否有PEM页眉/页脚(即开始证书和结束证书)。另外,我建议只使用日志而不是glog(教程:)。否则,存储在id_rsa中的ssh密钥是否有效?还有密码保护吗?