-1

i am trying to encrypt data using RSA encryption algorithm in my android app. i am able to encrypt data but whenever i am going to decrypt that encrypted data then i am getting Exception.

i have tried in google and also tried by apply all the solutions but getting same Exception as below.

javax.crypto.IllegalBlockSizeException: input must be under 64 bytes
    at com.android.org.conscrypt.OpenSSLCipherRSA.engineDoFinal(OpenSSLCipherRSA.java:245)
    at javax.crypto.Cipher.doFinal(Cipher.java:1340)
    at eu.agilers.util.Utility.decryptData(Utility.java:193)
    at eu.agilers.supersail.SuperSail.onCreate(SuperSail.java:46)
    at android.app.Activity.performCreate(Activity.java:6283)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
    at android.app.ActivityThread.access$900(ActivityThread.java:177)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5942)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

Encryption Method:

public static String encryptData(String textToEncrypt) {

    String modulus = "sAyRG6mbVY1XoPGZ9Yh+ZJvI40wxiq4LzoSbLlIdrYLelvzeQZD6Y6eG9XIALpEvnL3ZECf1Emnv17yELrcQ5w==";
    String exponent = "AQAB";

    try {

        byte[] modulusBytes = Base64.decode(modulus.getBytes("UTF-8"), Base64.DEFAULT);
        byte[] exponentBytes = Base64.decode(exponent.getBytes("UTF-8"), Base64.DEFAULT);

        BigInteger modulusInt = new BigInteger(1, modulusBytes);
        BigInteger exponentInt = new BigInteger(1, exponentBytes);

       /* RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulusInt, exponentInt);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PrivateKey privKey = fact.generatePrivate(rsaPrivKey);*/

        RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulusInt, exponentInt);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(rsaPubKey);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        byte[] encryptedByteData = cipher.doFinal(textToEncrypt.getBytes());

        return Base64.encodeToString(encryptedByteData, Base64.NO_WRAP);

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

Decryption Method:

public static String decryptData(String data) {

    try {

        String modulus = "sAyRG6mbVY1XoPGZ9Yh+ZJvI40wxiq4LzoSbLlIdrYLelvzeQZD6Y6eG9XIALpEvnL3ZECf1Emnv17yELrcQ5w==";
        String exponent = "AQAB";

        byte[] modulusBytes = Base64.decode(modulus.getBytes("UTF-8"), Base64.DEFAULT);
        byte[] exponentBytes = Base64.decode(exponent.getBytes("UTF-8"), Base64.DEFAULT);

        BigInteger modulusInt = new BigInteger(1, modulusBytes);
        BigInteger exponentInt = new BigInteger(1, exponentBytes);

       /* RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulusInt, exponentInt);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PrivateKey privKey = fact.generatePrivate(rsaPrivKey);*/

        RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulusInt, exponentInt);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(rsaPubKey);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
        cipher.init(Cipher.DECRYPT_MODE, pubKey);

        byte[] base64String = Base64.decode(data, Base64.DEFAULT);

        byte[] plainBytes = new String(base64String).getBytes("UTF-8");

        plainBytes = cipher.update(plainBytes);

        byte[] values = cipher.doFinal(plainBytes);

        return new String(values, "UTF-8");

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

Please help me.

Ajay
  • 1,189
  • 1
  • 12
  • 28
  • 1
    Is your input under 64 bytes? – Shark Mar 30 '16 at 13:19
  • i am using same method encryptData() to encrypt string and then decrypting the same string which i got from encryptData() method. Like, String encr = encryptData("testData"); String decr = decryptData(encr); – Ajay Mar 30 '16 at 13:24
  • 1
    You didn't even bother to answer my question... – Shark Mar 30 '16 at 13:28
  • The specific error is because of the line `byte[] plainBytes = new String(base64String).getBytes("UTF-8");`, which makes no sense anyway. Even if you fix this you're still trying to decrypt with the public key (see @Jim Flood answer below), which again makes no sense. – President James K. Polk Mar 30 '16 at 22:24

1 Answers1

2

To start with, try decrypting with the private key, not the public key. There is absolutely no way that you can use the RSA public key to decrypt data encrypted by the RSA public key.

RSA keys come in pairs: public and private. You need the private key to decrypt. If the public key could actually decrypt ciphertext encrypted by the public key, then all of public key cryptography would be broken.

Jim Flood
  • 8,144
  • 3
  • 36
  • 48