我使用AES加密了前2048字节数据,并对其进行了解密,但代码在解密时返回错误BadPaddingException。我不知道为什么?请给我正确的结果。
提前致谢:
错误:
11-18 15:59:35.452: WARN/System.err(30071): javax.crypto.BadPaddingException: pad block corrupted
11-18 15:59:35.456: WARN/System.err(30071): at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:653)
11-18 15:59:35.456: WARN/System.err(30071): at javax.crypto.Cipher.doFinal(Cipher.java:1116)
11-18 15:59:35.456: WARN/System.err(30071): at com.example.Hide.HideFolderActivity.decrypt(HideFolderActivity.java:191)
11-18 15:59:35.456: WARN/System.err(30071): at com.example.Hide.HideFolderActivity.decrypt(HideFolderActivity.java:167)
11-18 15:59:35.456: WARN/System.err(30071): at com.example.Hide.HideFolderActivity.onCreate(HideFolderActivity.java:65)
11-18 15:59:35.456: WARN/System.err(30071): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-18 15:59:35.456: WARN/System.err(30071): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-18 15:59:35.456: WARN/System.err(30071): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-18 15:59:35.456: WARN/System.err(30071): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-18 15:59:35.456: WARN/System.err(30071): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-18 15:59:35.460: WARN/System.err(30071): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 15:59:35.460: WARN/System.err(30071): at android.os.Looper.loop(Looper.java:123)
11-18 15:59:35.460: WARN/System.err(30071): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-18 15:59:35.460: WARN/System.err(30071): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 15:59:35.460: WARN/System.err(30071): at java.lang.reflect.Method.invoke(Method.java:521)
11-18 15:59:35.460: WARN/System.err(30071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-18 15:59:35.460: WARN/System.err(30071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-18 15:59:35.460: WARN/System.err(30071): at dalvik.system.NativeStart.main(Native Method)
11-18 15:59:35.480: WARN/InputManagerService(2501): Got RemoteException sending setActive(false) notification to pid 29873 uid 10092
代码:
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
//SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(raw, "AES"), new IvParameterSpec(new byte[cipher.getBlockSize()]));
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}