在安卓系统中使用RSA算法解密数据



我正在尝试在我的android应用程序中使用RSA加密算法加密数据。我可以加密数据,但每当我要解密加密的数据时,我就会得到Exception。

我在谷歌尝试过,也尝试过应用所有的解决方案,但得到了以下相同的异常。

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)

加密方法:

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;
    }
}

解密方法:

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;
    }
}

请帮帮我。

首先,请尝试使用私钥而不是公钥进行解密。绝对不可能使用RSA公钥来解密由RSA公钥加密的数据。

RSA密钥成对提供:公钥和私钥。您需要私钥才能解密。如果公钥真的可以解密由公钥加密的密文,那么所有的公钥密码学都将被破坏。

最新更新