C#- AES256 Key Encryption using (RSA/ECB/PKCS1Padding) and Decryption using Java (RSA/ECB/PKCS1Padding)

99 views Asked by At

Title: Issue with RSA decryption in Java, getting BadPaddingException

Question:

I'm working on a project that involves encrypting a secret key using AES and then decrypting it using RSA. The encryption part in C# works fine, but I'm encountering issues when trying to decrypt the encrypted secret key in Java. I keep getting a BadPaddingException.

Here's the relevant code:

C# Encryption Code (AES Key encryption):

public string GenerateAESKey()
{
    using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
    {
        aesProvider.KeySize = 256;
        aesProvider.GenerateKey();
        return Convert.ToBase64String(aesProvider.Key);
    }
}

C# Encryption Code (RSA encryption):

public string Encrypt(string plainAesKey)
{
    string publicKeyFileName = publicKeyPath;
    string cipherText = string.Empty;

    using (var rsa = new RSACryptoServiceProvider())
    {
        var param = getCertificate(publicKeyFileName);
        rsa.ImportParameters(param);
        var plainTextBytes = Encoding.UTF8.GetBytes(plainAesKey);
        byte[] cipherTextBytes = rsa.Encrypt(plainTextBytes, RSAEncryptionPadding.Pkcs1);
        cipherText = Convert.ToBase64String(cipherTextBytes);


    }
    return cipherText;
}

I also tried below encryption c# code:

static string contentRootPath = "Content";
        string publicKeyPath = Path.Combine(contentRootPath, "RSAKeys", "publicKey.cer");

        public string EncryptDataWithBouncyCastle(string aesKey)
        {
            string publicFilePath = publicKeyPath;
            try
            {
                
                using (FileStream fs = File.OpenRead(publicFilePath))
                {
                    X509CertificateParser parser = new X509CertificateParser();
                    X509Certificate certificate = parser.ReadCertificate(fs);

                    // Extract the RSA public key from the certificate
                    RsaKeyParameters publicKey = (RsaKeyParameters)certificate.GetPublicKey();

                    var rsaCipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
                   // var rsaCipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1PADDING");
                   // var rsaCipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1");
                    rsaCipher.Init(true, publicKey);

                    byte[] aesKeyBytes = Encoding.UTF8.GetBytes(aesKey);
                    byte[] EncData = rsaCipher.DoFinal(aesKeyBytes);

                    return Convert.ToBase64String(EncData);
                }
            }
            catch (Exception e)
            {
                return e.Message.ToString();
            }
        }
    }
}

Java Decryption Code:

public static byte[] decryptSecretKeyWithRSA(String encryptedSecretKey, PrivateKey privateKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, KeyStoreException, CertificateException, IOException, IllegalBlockSizeException, BadPaddingException, UnrecoverableKeyException {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] decoded = Base64.decodeBase64(encryptedSecretKey);
    byte[] decryptedKey = cipher.doFinal(decoded);
    return decryptedKey;
}

I'm confident that the encryption part in C# is working correctly, and I'm using the correct RSA public key. However, I can't seem to decrypt the secret key successfully in Java.

Is there something wrong with the decryption code in Java, or is there something I'm missing? Any help or insights would be greatly appreciated.

0

There are 0 answers