AES 256加密问题

编程入门 行业动态 更新时间:2024-10-22 19:41:20
本文介绍了AES 256加密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码完美地实现了AES-128加密/解密。

public static void main(String [] args)throws异常 { String input = JOptionPane.showInputDialog(null,Enter your String); System.out.println(Plaintext:+ input +\\\); //生成密钥 KeyGenerator keygen = KeyGenerator.getInstance(AES); keygen.init(128); byte [] key = keygen.generateKey()。getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(key,AES); //随机生成IV SecureRandom random = new SecureRandom(); byte [] iv = new byte [16]; random.nextBytes(iv); IvParameterSpec ivspec = new IvParameterSpec(iv); //初始化加密模式密码加密= Cipher.getInstance(AES / CBC / PKCS5Padding); cipher.init(Cipher.ENCRYPT_MODE,skeySpec,ivspec); //加密消息 byte [] encryption = cipher.doFinal(input.getBytes()); System.out.println(Ciphertext:+ encryption +\\\); // //初始化用于解密的密码 cipher.init(Cipher.DECRYPT_MODE,skeySpec,ivspec); //解密消息 byte [] decrypt = cipher.doFinal(encryption); System.out.println(Plaintext:+ new String(decrypt)+\\\); }

当我要使用AES-256时,我认为可以通过只是修改 keygen.init(256); 和 byte [] iv = new byte [32]; 变为错误(线程主中的异常java.security.InvalidKeyException:非法密钥大小)!有人可以解释为什么错误发生时,我做这两个修改,我该怎么办。 )

解决方案

如果要使用AES 256加密,必须安装无限制强度管辖权政策文件: p>

www.oracle/technetwork/java/javase/downloads/jce-7-download-432124.html

这启用

使用< java-home> \\中的当前文件替换zip中的文件。 \\ lib \security 。

The following code perfectly implements AES-128 encryption/decryption.

public static void main(String[] args) throws Exception { String input = JOptionPane.showInputDialog(null, "Enter your String"); System.out.println("Plaintext: " + input + "\n"); // Generate a key KeyGenerator keygen = KeyGenerator.getInstance("AES"); keygen.init(128); byte[] key = keygen.generateKey().getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); // Generate IV randomly SecureRandom random = new SecureRandom(); byte[] iv = new byte[16]; random.nextBytes(iv); IvParameterSpec ivspec = new IvParameterSpec(iv); // Initialize Encryption Mode Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec); // Encrypt the message byte[] encryption = cipher.doFinal(input.getBytes()); System.out.println("Ciphertext: " + encryption + "\n"); // // Initialize the cipher for decryption cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec); // Decrypt the message byte[] decryption = cipher.doFinal(encryption); System.out.println("Plaintext: " + new String(decryption) + "\n"); }

When I want to use AES-256, I thought that can be done by just modifying keygen.init(256); and byte[] iv = new byte[32];, however this becomes error (Exception in thread "main" java.security.InvalidKeyException: Illegal key size)! Can someone explain why error occurs when I made these two modification and what should I do. Thank you guys :)

解决方案

If you want to use AES 256 encryption you must install the Unlimited Strength Jurisdiction Policy Files:

www.oracle/technetwork/java/javase/downloads/jce-7-download-432124.html

This enables the higher encryption levels like AES 256 and RSA 2048.

Replace the files from the zip with the current ones in <java-home>\lib\security.

更多推荐

AES 256加密问题

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

发布评论

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

>www.elefans.com

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