RSA stands for android lollipop

I have a decryption error with RSA. The code works with the Android 4.4 Android kit, but the same application does not work on the Android 5 Android lottery.

KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(modulusBytes), new BigInteger(exponentBytes)); RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec); byte[] decrypted = null; try { // get an RSA cipher object and print the provider final Cipher cipher = Cipher.getInstance("RSA/None/NoPadding"); // decrypt the text using the public key cipher.init(Cipher.DECRYPT_MODE, publicKey); decrypted = cipher.doFinal(area_fissa_byte); } catch (Exception ex) { ex.printStackTrace(); Log.d("error","error"); } 

Error: java.security.SignatureException: error: 04067084: rsa routines: RSA_EAY_PUBLIC_DECRYPT: the data is too large for the module .

My sdk goal:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> for Android 4.4

Do you know what the problem is?

EDIT: I notice that I have two different public keys with different lengths !!! Android 5: I have 382/383 bits (too small) Android 4.4: I have 384 bits (normal)

EDIT2: I found that there are differences with android 5.0 for TLS / SSL: https://developer.android.com/about/versions/android-5.0-changes.html But I do not know how to fix this problem.

+7
android-5.0-lollipop ssl rsa public-key android-4.4-kitkat
source share
1 answer

One mistake is how you created RSAPublicKeySpec:

new RSAPublicKeySpec(new BigInteger(modulusBytes), new BigInteger(exponentBytes));

If the first bit of the Bytes or exponentBytes module is one, the number will be interpreted as a negative value.

When working with RSA and BigInteger numbers, always use the BigInteger (int signum, byte[] magnitude) constructor BigInteger (int signum, byte[] magnitude) with signum=1 to indicate that the number is positive:

new RSAPublicKeySpec(new BigInteger(1, modulusBytes), new BigInteger(1, exponentBytes));

+1
source share

All Articles