Padding error when using RSA encryption in C # and decryption in Java

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); // Convert the input string to a byte array baIn = UnicodeEncoding.Unicode.GetBytes(value); // Encrypt baRet = rsa.Encrypt(baIn, false); // Convert the encrypted byte array to a base64 string return Convert.ToBase64String(baRet); } 

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

+6
java c # encryption rsa
source share
5 answers

Make sure you exchange the key correctly.

An attempt to decrypt using an incorrect key is indistinguishable from decrypting badly laid data.

+2
source share

I had exactly the same problem and finally found a solution!

I was stubborn using PKCS1Padding, but I was not able to get it to work.

The best result I got with "rsa.Encrypt (baIn, false)" on the C # side and "RSA / NONE / NoPadding" on the Java side was this string type: "โ˜ป? O + _> ?? 5? l0Q * ??? *? R โ–ฒ ??? โ™€7 ... "followed by my decrypted line. Thus, he received the decryption, but since it does not indicate the filling, the data is shifted. Therefore, I tried all the pads available in the bouncycastle, but I would always get errors such as "wrong block size" or "data hash hash".

So, I decided to start using OAEP firmware, and I finally managed to get it to work using "rsa.Encrypt (baIn, true)" on the C # side and "RSA / NONE / OAEPWithSHA1AndMGF1Padding" on the java side!

It worked for me, I hope it works for you too! If it does not work, make sure that you use the correct key, very often the problem arises from the key.

+4
source share

I am working with a similar problem working between .NET and iPhone in Objective-C, and I think the answer lies in this little gem from the RSACryptoServiceProvider documentation:

Unlike the RSA implementation in unmanaged CAPI, the RSACryptoServiceProvider class changes the order of the encrypted byte array after encryption and before decryption. By default, data encrypted with the RSACryptoServiceProvider class cannot be decrypted with the CAPI CryptDecrypt function, and data encrypted with the CAPI CryptEncrypt method cannot be decrypted with the RSACryptoServiceProvider class.

For more details see here http://msdn.microsoft.com/en-us/library/s575f7e2(v=VS.90).aspx

+2
source share

I assume that the C # version emits bytes in a small trailing format, and the Java version imports bytes in the expectation that they will be in a large end format. Try changing the bytes in baRet from end to end before converting them to base 64 and see if your Java program can decrypt them.

Just to guess.

0
source share

I had the same problem when using Bouncy Castle 1.48, but it was not related to keywords. Instead, I found that I needed to set the following system property:

 -Dorg.bouncycastle.pkcs1.strict=false 
0
source share

All Articles