如何从Go中的字符串中导入RSA公钥,以便将其用于加密数据?
我的程序应该做以下工作:
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, plaintextBytes, []byte(""))提前谢谢你!
溶液
公钥必须使用密码/x 509包解码。
例如:
publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyDER)
if err != nil {
log.Println("Could not parse DER encoded public key (encryption key)")
return []byte(""), err
}
publicKey, isRSAPublicKey := publicKeyInterface.(*rsa.PublicKey)
if !isRSAPublicKey {
log.Println("Public key parsed is not an RSA public key")
return []byte(""), err
}然后,您可以使用publicKey和RSA进行加密。
发布于 2017-11-20 11:19:11
当您收到base64编码的密码时,您的解码如下:
base64.StdEncoding.DecodeString("FExEiYPgwrHHyO7DOwCXHNRvVF2S8xirXftuFWmJUzo=")然后,您加密的字符串不能超过公共模数的长度减去散列长度的两倍,减2。有关示例和文档,请参阅文档。
https://stackoverflow.com/questions/47380374
复制相似问题