I'm trying to decrypt a string which is encrypted in Golang using RSA-OAEP. but getting BadPaddingException: Decryption error. Having hard time to figure out what am I missing..
Here is the Golang encrypt method
func encryptString() {
rootPEM := io_related.ReadFile("../../resources/pubkey.pem")
//fmt.Printf("Cert String %q \n", rootPEM)
block, _ := pem.Decode([]byte(rootPEM))
var cert *x509.Certificate
cert, _ = x509.ParseCertificate(block.Bytes)
rsaPublicKey := cert.PublicKey.(*rsa.PublicKey)
secretMessage := []byte("password")
label := []byte("")
// crypto/rand.Reader is a good source of entropy for randomizing the
// encryption function.
rng := rand.Reader
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rng, rsaPublicKey, secretMessage, label)
if err != nil {
fmt.Fprintf(os.Stderr, "Error from encryption: %s\n", err)
return
}
// Since encryption is a randomized function, ciphertext will be
// different each time.
base64EncodedString := base64.StdEncoding.EncodeToString(ciphertext)
fmt.Println(base64EncodedString)
}
and my java decrypt method as
public void decryptString(String base64String) throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, UnrecoverableKeyException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
FileInputStream is = new FileInputStream("priv.p12");
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(is, "".toCharArray());
System.out.println("Successfully loaded");
String keyAlias = "1";
PrivateKey key = (PrivateKey)keystore.getKey(keyAlias, "".toCharArray());
System.out.println("key "+Base64.encodeBase64String(key.getEncoded()));
Cipher rsaDecryptCipher;
rsaDecryptCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
rsaDecryptCipher.init(Cipher.DECRYPT_MODE, key);
final byte[] plainText = rsaDecryptCipher.doFinal(Base64.decodeBase64(base64String));
System.out.println("Plain : " + new String(plainText));
}
- I made sure I'm using the same key pair and not a different private key
- Made sure hash algorithm used same in both encrypt and decrypt "SHA256"
I might be missing something, Please let me know if anyone need more details. Appreciate the help!!. Thanks