Android:使用iv和密钥加密AES 256位加密的字符串

编程入门 行业动态 更新时间:2024-10-23 05:37:44
本文介绍了Android:使用iv和密钥加密AES 256位加密的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 SecureRandom random = new SecureRandom(); // quite heavy, look into a lighter method. String stringToEncrypt = "mypassword"; byte[] realiv = new byte[16]; random.nextBytes(realiv); Cipher ecipher = Cipher.getInstance("AES"); SecureRandom random = new SecureRandom(); // quite heavy, look into a lighter method. byte[] realiv = new byte[16]; random.nextBytes(realiv); byte[] secret = "somelongsecretkey".getBytes(); SecretKeySpec secretKey = new SecretKeySpec(secret, "AES"); ecipher.init(Cipher.ENCRYPT_MODE, secretKey, random); byte[] encryptedData = ecipher.doFinal();

但$ init()只需要3个参数。我需要一种方法来执行以下操作:

but the init() only takes in 3 parameters. I need a way to do something like:

ecipher.init(Cipher.ENCRYPT_MODE, stringToEncrypt, secretKey, random);

推荐答案

一般来说,你不需要产生随机的东西具有确定性行为的算法的数字。此外,当您使用ECB块模式时,您不需要IV,这是Java默认的模式。确切地说,Java默认为code $ c中的AES / ECB / PKCS5Padding Cipher.getInstance(AES)

In general you don't need something that generates random numbers for an algorithm that has deterministic behavior. Furthermore, you don't need an IV when you are using ECB block mode, which is what Java defaults to. To be precise, Java defaults to "AES/ECB/PKCS5Padding" for in Cipher.getInstance("AES").

所以你应该是这样的代码:

So you should be OK with code like this:

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

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

Now that looks a whole lot better. I've used the Apache commons codec for decoding the hexadecimal string.

请注意,您需要保存 realIV 与 encryptedData ,并且您没有包括完整性保护,例如一个MAC(用于密码,可能不需要)。

Note that you need to save the realIV with the encryptedData, and that you haven't included integrity protection, e.g. a MAC (for passwords, you may not need that though).

更多推荐

Android:使用iv和密钥加密AES 256位加密的字符串

本文发布于:2023-11-09 09:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572064.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   字符串   iv   Android   AES

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!