栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android:使用iv和密钥通过AES 256位加密对字符串进行加密

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android:使用iv和密钥通过AES 256位加密对字符串进行加密

通常,您不需要为具有确定性行为的算法生成随机数的对象。此外,在使用ECB块模式时,您不需要IV,这是Java默认设置。确切地说,Java默认为中的

"AES/ECB/PKCS5Padding"
for
Cipher.getInstance("AES")

因此,您应该可以使用如下代码:

// lets use the actual key value instead of the platform specific character decodingbyte[] secret = Hex.depreHex("25d6c7fe35b9979a161f2136cd13b0ff".toCharArray());// that's fineSecretKeySpec secretKey = new SecretKeySpec(secret, "AES");// SecureRandom should either be slow or be implemented in hardwareSecureRandom random = new SecureRandom();// first create the cipherCipher eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// filled with 00h characters first, use Cipher instance so you can switch algorithmsbyte[] realIV = new byte[eCipher.getBlockSize()];// actually fill with randomrandom.nextBytes(realIV);// MISSING: create IvParameterSpecIvParameterSpec ivSpec = new IvParameterSpec(realIV);// create the cipher using the IVeCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);// NOTE: you should really not encrypt passwords for verificationString stringToEncrypt = "mypassword";// convert to bytes first, but don't use the platform encodingbyte[] dataToEncrypt = stringToEncrypt.getBytes(Charset.forName("UTF-8"));// actually do the encryption using the databyte[] encryptedData = eCipher.doFinal(dataToEncrypt);

现在看起来好多了。我已使用Apache Commons编解码器解码十六进制字符串。

请注意,您需要保存

realIV
encryptedData
和你有没有包括完整性保护,如MAC(口令,你可能不需要,虽然)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506244.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号