AES算法java实现浅析

      java jce对aes算法有很好的支持,但是默认安装的JDK或者JRE是不能支持aes192bit密钥和aes256bit密钥两种算法的,需要到sun官方下载Java(TM) Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files,以下是jce unilimited strength jurisdiction policy files 6的官方下载地址:

https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jce_policy-6-oth-JPR@CDS-CDS_Developer

     下载后解压文件,将里面的文件拷贝到

                <java-home>/lib/security            [Unix]
                <java-home>/lib/security           [Win32]

      覆盖原先的jar文件,请先备份原先的jar文件,以防你需要恢复密钥长度的限制,这里需要注意的是如果是安装的jdk,jdk目录中的jre下面的lib/security下的文件也需要覆盖。完成这个步骤之后就可以使用不限制密钥长度的aes算法了,简单的实现代码如下:

                KeyGenerator kgen = KeyGenerator.getInstance("AES");
                kgen.init(192); // 192 and 256 bits also available,这一步是否有安装jce unlimited strength jurisdiction policy files 都不会出现异常
                Generate the secret key specs.
                SecretKey skey = kgen.generateKey();
                byte[] raw = skey.getEncoded();

                SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

                Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);//这一步如果没有安装jce unlimited strength jurisdiction policy files,并且使用了192bits和256bits的密钥的话就会抛出java.security.InvalidKeyException:Invalid AES key length
                 byte[] output = cipher.doFinal(seed);//seed是一个byte[];

                

      最后讲下aes算法是块加密运算的,单位块大小为16bytes,所以输入的byte[]如果不是16的倍数就填充至16的倍数,然后aes算法的密文结构就是aes对每一个块(16bytes)的明文加密的密文的拼串。比如aes128加密16bytes的byte[]a得到16bytes的密文a,加密16bytes的byte[]b得到16bytes的密文b,这样我们构建一个32bytes的byte[]c,前16bytes数据来自copy至a,后16bytes数据copy至b,这样将c输入aes128加密将得到一个32bytes的密文,并且就是之前的密文a和密文b的顺序拼串,以上都是nopadding模式下的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
AES算法Java实现可以通过Java加密javax.crypto来实现。具体步骤如下: 1. 导入相应的包,引入javax.crypto.Cipher和javax.crypto.spec.SecretKeySpec类。 2. 创建一个Cipher对象,指定使用AES算法。 3. 创建一个SecretKeySpec对象,将密钥以字节数组的形式传递给它。 4. 使用Cipher对象的init()方法初始化加密/解密模式和密钥。 5. 调用Cipher对象的doFinal()方法,传入要加密/解密的数据。 6. 处理加密/解密后的数据。 以下是一个简单的AES加密和解密的示例代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; public class AESExample { private static final String key = "0123456789abcdef"; // 密钥,必须为16字节长度的字符串 public static byte[] encrypt(byte[] data) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); return cipher.doFinal(data); } public static byte[] decrypt(byte[] encryptedData) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return cipher.doFinal(encryptedData); } public static void main(String[] args) throws Exception { String text = "搞事情"; byte[] encryptedData = encrypt(text.getBytes()); String base64EncryptedData = new String(java.util.Base64.getEncoder().encode(encryptedData)); System.out.println("AES加密后:" + base64EncryptedData); byte[] decryptedData = decrypt(java.util.Base64.getDecoder().decode(base64EncryptedData)); String decryptedText = new String(decryptedData); System.out.println("AES解密后:" + decryptedText); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值