1. 程式人生 > >RSA 解密的時候報錯 而且有亂碼:java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block

RSA 解密的時候報錯 而且有亂碼:java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block


報錯:

03-22 10:21:20.805 507-507/? E/AndroidRuntime: FATAL EXCEPTION: main

03-22 10:21:20.805 507-507/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{tech.androidstudio.encodecryp/tech.androidstudio.encodecryp.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:507)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:  Caused by: java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block

03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(JCERSACipher.java:449)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at javax.crypto.Cipher.doFinal(Cipher.java:1090)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at tech.androidstudio.encodecryp.CryptUtil.rsaDecrypt(CryptUtil.java:75)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at tech.androidstudio.encodecryp.MainActivity.rsaTest(MainActivity.java:154)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at tech.androidstudio.encodecryp.MainActivity.onCreate(MainActivity.java:36)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:3683) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:507) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 

03-22 10:21:20.805 507-507/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

原因:第二個問題也是由第一個亂碼的問題引起的:

程式碼:

    public void rsaTest(){
        String data = "Hello, Kodulf is a good father, 千雅爸爸是個好爸爸";
        byte[] bytes = data.getBytes();
        byte[] encryptByte = CryptUtil.rsaEncrypt(bytes, mPrivate);
                String encryptString = new String(encryptByte);//這裡產生亂碼

        Log.d("kodulf","Hello, Kodulf is a good father, 千雅爸爸是個好爸爸 加密後:"+encryptString);

        //解密:
        byte[] sourceBytes = encryptString.getBytes();//這裡會報錯java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
        byte[] decryptBytes = CryptUtil.rsaDecrypt(sourceBytes, mPublic);
        String decryptString = new String(decryptBytes);
        Log.d("kodulf","Hello, Kodulf is a good father, 千雅爸爸是個好爸爸 解密後:"+decryptString);
    }

獲取的時候直接就將資料輸出了,而沒有通過Base64來進行處理,如果通過Base64來進行處理了,就不會顯示亂碼,Base64 也就是將非Ascii的碼變成可以讀取的字元

解決辦法:

新增Base64到程式碼裡面,如下:

public void rsaTest(){
        String data = "Hello, Kodulf is a good father, 千雅爸爸是個好爸爸";
        byte[] bytes = data.getBytes();
        byte[] encryptByte = CryptUtil.rsaEncrypt(bytes, mPrivate);

        //要使用Base64來進行編碼,如果不這樣做就會顯示亂碼
        //String encryptString = new String(encryptByte);//這裡產生亂碼
        String encryptString = Base64.encodeToString(encryptByte, Base64.NO_WRAP);

        Log.d("kodulf","Hello, Kodulf is a good father, 千雅爸爸是個好爸爸 加密後:"+encryptString);

        //解密:
        //要使用Base64來進行解碼,如果不這樣做就會顯示亂碼
        //byte[] sourceBytes = encryptString.getBytes();//這裡會報錯java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
        byte[] sourceBytes = Base64.decode(encryptString.getBytes(), Base64.NO_WRAP);
        byte[] decryptBytes = CryptUtil.rsaDecrypt(sourceBytes, mPublic);
        String decryptString = new String(decryptBytes);
        Log.d("kodulf","Hello, Kodulf is a good father, 千雅爸爸是個好爸爸 解密後:"+decryptString);
    }