AES加密

****以上实现需要Apache下的codec相关jar文件和bouncy相关jar文件


package com.natureframework.util.cipher;



import java.security.Key;
import java.security.Security;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;


/**
 * AES安全编码通用组件工具
 * 
 * @author 周正德
 * @version 1.0
 * 
 */
public class AESCoder {


static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 密钥算法名
*/
public static final String KEY_ALGORITHM = "AES";


/**

* 加密/解密算法 / 工作模式 / 填充方式 Java 7支持PKCS5PADDEING ,需要修改JDK授权jar文件,只持256的长度

* 注意在不需要额外修改JDK相关安全授权jar文件,利用PKSC7Padding因为在国内能支持128位的长度

*/
public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";


/**
* 转换密钥

* @param key
*            二进制密钥
* @return Key 加密的密钥
* @throws Exception
*/
private static Key toKey(byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
return secretKey;
}


/**
* 解密

* @param data
*            等解密数据
* @param key
*            密钥
* @return byte[] 解密的数据
* @throws Exception
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}


/**
* 加密

* @param data
*            等加密数据
* @param key
*            密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}


/**
* 生成密钥

* @return byte[] 二进制密钥
* @throws Exception
*/
public static byte[] initKey() throws Exception {
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
kg.init(128);
SecretKey secretKey = kg.generateKey();
return secretKey.getEncoded();
}


/**
* 初始化密钥

* @return
* @throws Exception
*/
public static String initKeyString() throws Exception {
return Base64.encodeBase64String(initKey());
}


/**
* 获取密钥

* @param key
* @return
* @throws Exception
*/
public static byte[] getKey(String key) throws Exception {
return Base64.decodeBase64(key);
}


/**
* 解密

* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, String key) throws Exception {
return decrypt(data, getKey(key));
}


/**
* 加密

* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
return encrypt(data, getKey(key));
}


/**
* 摘要处理

* @param data
* @return
*/
public static String shaHex(byte[] data) {
return DigestUtils.md5Hex(data);
}

/**
* 验证
* @param data
* @param messageDigest
* @return
*/
public static boolean validate(byte[] data, String messageDigest) {
return messageDigest.equals(shaHex(data));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值