【发布时间】:2012-04-08 01:05:53
【问题描述】:
我需要有关 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;
}
用于String的加密,这个函数:
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;
}
用于String的解密。
我使用crypt方法加密数据时没有报错,字符串被加密了:
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)
而且我不明白为什么会出现这个错误...
你有什么解决办法吗?或者给我一些解释?
【问题讨论】:
-
您应该发布使用过的密码,否则很难追溯您的代码和加密结果。顺便说一句:永远不要使用密码作为密钥!使用密钥派生函数从密码生成密钥。
-
@Robert 从密码生成密钥的密钥派生函数:你有例子吗?
-
@Robert 好的,但我在 Objective-C 中执行相同的代码(当然,使用 Apple 函数)但结果很好......所以,即使使用密码短语作为密钥也不好,这可能是工作,不是吗?
-
大多数时候是(字符)编码部分出了问题,而不是实际的加密/解密。那只是为了实现。安全方面仍然可能存在许多问题,例如使用 ECB 模式加密、忘记添加完整性/身份验证、忘记创建随机 IV、直接使用密码作为密钥...
标签: android cryptography aes encryption