I am trying to decode the encrypted data using Private Key, RSA, RSA/ECB/OAEPWithSHA1AndMGF1Padding in Golang, but received error
Failed to decrypt AES key: crypto/rsa: decryption error
exit status 1
I am using ParsePKCS8PrivateKey
When I am perform same in Javascript/Java, decoding is working fine, but failing in Golang, I suspect because of padding.
NOTE: Decrypting the encrypted data using private key and symmetric key
Any help understanding why DecryptOAEP might be failing in Golang would be appreciated!
Additional Information:
- Go version: version go1.21.3 (crypto/rsa, etc.)
- Javascript: Bun
v0.6.13 or Nodejs
Things I’ve Tried:
- Double-checking the base64 encoding and PEM format of the key.
Ensuring the private key format is compatible with
ParsePKCS8PrivateKey. - I’ve verified that the base64 encoded key and private key PEM are correct and its format.
I’m hoping to achieve the same decryption functionality in Golang as the Javascript code.
Golang Code
encryptedKey, err := base64.StdEncoding.DecodeString(base64encodedKey)
if err != nil {
log.Fatalf("Failed to decode base64 string: %v", err)
}
// Parse the PEM-encoded private key
block, _ := pem.Decode([]byte(privateKeyPEM))
if block == nil || block.Type != "PRIVATE KEY" {
log.Fatalf("Failed to decode PEM block containing private key")
}
// Parse the private key
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
}
// Perform RSA decryption
rsaPrivateKey := privateKey.(*rsa.PrivateKey)
decryptedKey, err := rsa.DecryptOAEP(sha256.New(), nil, rsaPrivateKey, encryptedKey, nil)
if err != nil {
log.Fatalf("Failed to decrypt AES key: %v", err)
}