Java AES加密
来源:互联网 发布:js改变浏览器窗口大小 编辑:程序博客网 时间:2024/05/16 13:05
package com.dataservice.utils.crypto;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.Key;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;/** * * Aes 加解密 * */public class AesUtils { public static final String KEY_ALGORITHM = "AES"; public static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; private static Key toKey(byte[] key) { SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM); return secretKey; } public static byte[] generateKey() throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); keyGenerator.init(128); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } /** * * @param data * @param key * @param salt * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidAlgorithmParameterException */ public static byte[] encrypt(byte[] data, byte[] key, String salt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { Key k = toKey(key); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, k, initIvParameterSpec(salt)); return cipher.doFinal(data); } /** * * @param data * @param key * @param salt * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidAlgorithmParameterException */ public static byte[] dencrypt(byte[] data, byte[] key, String salt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { Key k = toKey(key); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, k, initIvParameterSpec(salt)); return cipher.doFinal(data); } public static IvParameterSpec initIvParameterSpec(String salt) { return new IvParameterSpec(String.format("%16s", salt).getBytes()); }}
阅读全文
0 0
- 加密解密。(aes, java)
- JAVA的AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- java,AES加密,解密
- java使用AES加密
- JAVA实现AES加密
- JAVA AES加密
- java AES加密解密
- JAVA实现AES加密
- java AES加密解密
- JAVA实现AES加密
- java使用AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- java AES加密
- org.apache.commons.net.ftp.FTPClient
- 项目报错:Caused by: java.lang.SecurityException:
- Mysql 中的事件//定时任务
- idea开发-使用oracle数据库-mapper文件中-常用语句
- spring boot启动报错Error creating bean with name 'entityManagerFactory' defined in class path resource [
- Java AES加密
- javascript垃圾回收机制
- DevExpress项目升级总结
- Autofac官方文档(一)【开始】
- UGUI雷达图
- java 对象的初始化顺序
- Mysql中的视图
- springboot(六):如何优雅的使用mybatis
- 白盒测试