我需要帮助解决Android应用程序的加密/解密问题。
让我来解释一下情况。我实际上编写了一个应用程序,该应用程序使用由iPhone应用程序生成和加密的内容。
为了保证安全性,用户提供自己的密码短语以正确加密/解密不同平台之间的数据...
但是,在Android上遇到了加密/解密此密码短语的问题。
我有两个功能:
public byte[] crypt(String pStringToCrypt) throws Exception{
byte[] key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
System.arraycopy(this.passphrase.getBytes(), 0, key, 0, this.passphrase.getBytes().length);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(pStringToCrypt.getBytes());
return encrypted;
}
用于加密字符串的函数是:
public String decrypt(byte[] pCryptedStringtoDecrypt) throws Exception{
byte[] key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
System.arraycopy(this.passphrase.getBytes(), 0, key, 0, this.passphrase.getBytes().length);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
byte[] encrypted = pCryptedStringtoDecrypt;
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(encrypted);
String originalString = new String(original);
return originalString;
}
对于字符串的解密。
当我使用加密方法加密数据时,没有错误并且字符串已经被加密:
encrypted = [26, 119, -108, -24, 81, -128, 18, 35, -96, 10, -38, 69, 111, 40, 109, 107]
如果我尝试将这个字节转换为字符串,我会得到这个字符串:
encryptedString = "w��Q�#�\n�Eo(mk"
我认为加密阶段做得不错。现在当我尝试解密这个加密的字符串时,应用程序崩溃了:
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:711)
at javax.crypto.Cipher.doFinal(Cipher.java:1090)
at org.vincentsaluzzo.lightrpc.common.security.AES256.decrypt(AES256.java:61)
at com.vincentsaluzzo.LoginBox.model.SettingsManager.getUserPassphrase(SettingsManager.java:67)
at com.vincentsaluzzo.LoginBox.mainActivity.onCreate(mainActivity.java:26)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
我不明白为什么会出现这个错误...
你有解决方案吗?或者可以给我一些解释吗?