I am currently getting the following error when using Java to decrypt an RS64 encoded RS64 encoded string that was made in C #:
javax.crypto.BadPaddingException: not PKCS # 1 block type 2 or null padding
The configuration process between exchanging with .NET and Java is accomplished by creating a private key in the .NET keystore, and then from the extracted PEM file created using keytool to create the private key version of JKS. Java loads the already created JKS and decodes the Base64 string into an array of bytes, and then uses the private key to decrypt it.
Here is the code that I have in C # that creates an encrypted string:
public string Encrypt(string value) { byte[] baIn = null; byte[] baRet = null; string keyContainerName = "test"; CspParameters cp = new CspParameters(); cp.Flags = CspProviderFlags.UseMachineKeyStore; cp.KeyContainerName = keyContainerName; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
Here is the code that I have in Java that decrypts the input string:
public void decrypt(String base64String) { String keyStorePath = "C:\Key.keystore"; String storepass = "1234"; String keypass = "abcd"; byte[] data = Base64.decode(base64String); byte[] cipherData = null; keystore = KeyStore.getInstance("JKS"); keystore.load(new FileInputStream(keyStorePath), storepass.toCharArray()); RSAPrivateKey privateRSAKey = (RSAPrivateKey) keystore.getKey(alias, keypass.toCharArray()); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateRSAKey); cipherData = cipher.doFinal(data); System.out.println(new String(cipherData)); }
Does anyone see the missing step, or where do I need to change the add-on or item? I worked hours of reading on this site and others, but actually did not find a specific solution.
You need help.
Thanks. Matt
java c # encryption rsa
Matt shaver
source share