android - AES Gingerbread

标签 android aes

This是我的 AES 代码。

在 Gingerbread 下,我在 2.2 上加密数据然后尝试在 2.3 上解密后出现以下错误:

 Caused by: javax.crypto.BadPaddingException: pad block corrupted
        at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:715)
        at javax.crypto.Cipher.doFinal(Cipher.java:1090)
        at com.citc.wallet.util.security.SimpleCrypto.decrypt(SimpleCrypto.java:63)
        ... 21 more

我发现一些帖子说 SecureRandom 在不同的系统下产生不同的结果:

BouncyCastle AES error when upgrading to 1.45

如何在 2.3 上避免这个问题?
我可以以某种方式强制 2.3 使用相同的 SecureRandom 类吗?
我是否需要使用某种可移植的加密方法,如果需要怎么办?

最佳答案

答案是你根本不应该真正做你正在做的事情。这是罪魁祸首:

sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();

你永远不应该用一些不可预测的随机值来填充你的 key ,因为你以后需要重新创建这个完全相同的 key 。以下是 android 文档中的一些关键行

"Seeding SecureRandom may be insecure"

Although it is common practice to seed Random with the current time, that is dangerous with SecureRandom since that value is predictable to an attacker and not appropriate for secure use.

无论如何,我知道你的论点是你只是在“填充” key ,而你所做的事情的安全性并不是什么大问题。

如果您要接受 192 位或 256 位实现的 128 位 key ,则必须实现一种可重复的方法,将 key 扩展到 192 位或 256 位。如果愿意,您甚至可以将全 0 添加到 key 中,但真正的关键是必须以某种方式完成,以便您可以在每个系统上重复它。

无论如何,您可能还需要考虑您正在做的事情可能会在 Android 以外的系统上使用。在这些情况下,应该选择使用更“便携”的方法来扩展 key 。

关于android - AES Gingerbread ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4536241/

相关文章:

java - 进入 Debug模式后 Eclipse 会阻止编辑

android - Optical Bounds 9-patch 禁用内容填充

javascript - Android 网络浏览器中的 XPathEvaluator 在哪里?

php - MySQL - 如何存储 AES 加密数据?

Java AES-256 解密 - 从 ActionScript 3 翻译代码

java - AES 加密 - 以后如何解密加密的字符串?

android - 如何为我的 Android Market APK 创建证书?

android - 开关按钮 - 点击工作但滑动开关在 android 中不起作用

java - 如果没有无限强度的 JCE 文件,Java 中是否可以进行 AES256 加密解密?

ios - 在 iOS 中解码 OpenSSL AES256 字符串