AES128加密解码

定义常量

//定义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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值