AES128加密解码
来源:互联网 发布:python中文 编辑:程序博客网 时间:2024/04/29 04:47
定义常量
//定义AES_128常量
public static final Integer AES_SIZE_128=128;
public static final String LAGORITHM_AES="AES";
public static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
public static final String BC_PROVIDER = "BC";
public static final String DEFAULT_ROOT_IV="000000";
public static final String DEFAULT_CHARSET="UTF-8";
加密解码实现
package com.wd.demo;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.web.crypty.AESConstants;
import com.web.crypty.WandaCryptoAESException;
public final class AES128Realize {
private final byte[] passwordKey;
private final Integer keySize;
public Integer getKeySize() {
return keySize;
}
public AES128Realize(byte[] passwordKey,Integer keySize) {
this.passwordKey=passwordKey;
this.keySize=keySize;
}
public AES128Realize(byte[] passwordKey) {
this(passwordKey,AES128Constant.AES_SIZE_128);
}
//创建AES_128密码规则
public Cipher getCipher(Integer a) throws WandaCryptoAESException{
//定义密钥规则
try {
SecretKeySpec key = new SecretKeySpec(passwordKey, AESConstants.ALGORITHM_AES);
Cipher cipher = Cipher.getInstance(AESConstants.CIPHER_ALGORITHM_CBC,AESConstants.BC_PROVIDER);// 创建密码器
cipher.init(a, key, new IvParameterSpec(AESConstants.DEFAULT_ROOT_IV.getBytes()));// 初始化
//返回cipher
return cipher;
}catch (Exception e) {
throw new WandaCryptoAESException("Failed to get the cipher with passwordBytes [" + passwordKey + "] key size + [" + keySize + "] mode [" + a + "]", e);
}
}
//调用创建AES_128规则方法
protected Cipher getCipherEncrypt() throws WandaCryptoAESException{
return getCipher(Cipher.ENCRYPT_MODE);
}
//调用创建AES_128规则方法
protected Cipher getCipherDecode() throws WandaCryptoAESException{
return getCipher(Cipher.DECRYPT_MODE);
}
//加密
public byte[] getEncrypt(String content) throws WandaCryptoAESException{
try{
byte[] byteContent=content.getBytes(AES128Constant.DEFAULT_CHARSET);
return getCipherEncrypt().doFinal(byteContent);
}catch (Exception e) {
throw new WandaCryptoAESException("Failed to get the cipher with passwordBytes [" + passwordKey + "] key size + [" + keySize + "] mode [" + content + "]", e);
}
}
//解密
public byte[] getDecode(String content) throws WandaCryptoAESException{
try{
byte[] byteContent=content.getBytes(AES128Constant.DEFAULT_CHARSET);
return getCipherDecode().doFinal(byteContent);
}catch (Exception e) {
throw new WandaCryptoAESException("Failed to get the cipher with passwordBytes [" + passwordKey + "] key size + [" + keySize + "] mode [" + content + "]", e);
}
}
}
调用接口
WandaAES wandaAES = new WandaAES(Hex.decodeHex(Main.HEX_AES_128_PASSWORD.toCharArray()));
// 1 将参数转换成JSON对象
ObjectMapper om = Jackson2ObjectMapperBuilder.json().build();
String jsonString = om.writeValueAsString(productParam);
// 2 加密
// 2.1 AES 128
byte[] encryptBytes = wandaAES.encrypt(jsonString);
// 2.2 BASE64
String base64String = Base64Utils.encodeToString(encryptBytes);
System.out.println(base64String);
- AES128加密解码
- JAVA AES128加密解密
- AES128加密解密方法
- 加密工具之AES128
- ios AES128加密解密算法
- AES128 ECB加密pkcs5padding补码
- AES128
- AES128
- php AES128加密解密类的使用
- Android通过AES128加密解密字符串
- iOS 调用 SecKeyWrapper 进行 AES128 加密解密
- AES128、AES256加密 在iOS上实现
- iOS AES128 CBC No Padding加密解密
- python加密模块使用,aes128,ecb模式
- AES128的加密解密学习总结
- java AES128加密压缩 模拟传输数据
- AES128加密解密以及参数的处理
- PHP与JAVA使用AES128位加密通信
- samba服务器配置
- window7的64bit平台搭建机器学习所需要的Python2.7开发环境,安装算法库以及开发测试
- 递归与非递归求二叉树深度
- shell浅谈之七文本处理工具grep、sed、awk
- java中的强引用,软引用,弱引用,虚引用
- AES128加密解码
- css3动画从入门到精通
- Ormlite 学习
- 叹息,卫津河水
- Android 命名规范 (提高代码可以读性)
- 计算机基础——字符与编码在程序中的实现
- gdal中文打开影像报错问题
- hdu-2795Billboard(线段树 找到可以贴当前广告最上方的位置)
- Unity VS脚本自动添加头部注释