ASE加解密Base64 jdk1.7工具类

package com.hexin.utils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.Security;
//import java.util.Base64;
//import java.util.Base64.Decoder;
import sun.misc.BASE64Decoder;

/**

 * @version V1.0

 * @desc AES 加密工具类
https://my.oschina.net/leaforbook/blog/1808360?from=singlemessage
 */

public class AES {

    private static final String KEY_ALGORITHM = "AES";

    private static final String CBC_PKCS7PADDING_ALGORITHM = "AES/CBC/PKCS7Padding";//加密算法

    /**

     * AES 加密操作

     * @param data 待加密内容
     * @param sessionKey 加密密码
     * @param iv
     * @return 返回Base64转码后的加密数据

     */

    public static String encrypt(String data,String sessionKey,String iv) throws Exception {

        //加密之前,先从Base64格式还原到原始格式
        BASE64Decoder decoder = new BASE64Decoder();
        //Decoder decoder = Base64.getDecoder();
        byte[] dataByte = decoder.decodeBuffer(data);
        byte[] keyByte = decoder.decodeBuffer(sessionKey);
        byte[] ivByte = decoder.decodeBuffer(iv);

        String encryptedData = null;
        //指定算法,模式,填充方式,创建一个Cipher
        Cipher cipher = Cipher.getInstance(CBC_PKCS7PADDING_ALGORITHM,"BC");
        //生成Key对象
        Key sKeySpec = new SecretKeySpec(keyByte, KEY_ALGORITHM);
        //把向量初始化到算法参数
        AlgorithmParameters params = AlgorithmParameters.getInstance(KEY_ALGORITHM);
        params.init(new IvParameterSpec(ivByte));
        //指定用途,密钥,参数 初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, params);

        //指定加密
        byte[] result = cipher.doFinal(dataByte);

        //对结果进行Base64编码,否则会得到一串乱码,不便于后续操作
//        Base64.Encoder encoder = Base64.getEncoder();
//        encryptedData = encoder.encodeToString(result);
        encryptedData=new sun.misc.BASE64Encoder().encode(result);
        return encryptedData;
    }


    /**

     * AES 解密操作

     * @param encryptedData

     * @param sessionKey
     *
     * @param iv

     * @return

     */

    public static String decrypt(String encryptedData,String sessionKey,String iv) throws Exception {

          //加密之前,先从Base64格式还原到原始格式
        BASE64Decoder decoder = new BASE64Decoder();
        //Decoder decoder = Base64.getDecoder();
        byte[] dataByte = decoder.decodeBuffer(encryptedData);
        byte[] keyByte = decoder.decodeBuffer(sessionKey);
        byte[] ivByte = decoder.decodeBuffer(iv);
//        //解密之前先把Base64格式的数据转成原始格式
//        Decoder decoder = Base64.getDecoder();
//        byte[] dataByte = decoder.decode(encryptedData);
//        byte[] keyByte = decoder.decode(sessionKey);
//        byte[] ivByte = decoder.decode(iv);

        String data = null;
        //指定算法,模式,填充方法 创建一个Cipher实例
        Cipher cipher = Cipher.getInstance(CBC_PKCS7PADDING_ALGORITHM,"BC");
        //生成Key对象
        Key sKeySpec = new SecretKeySpec(keyByte, KEY_ALGORITHM);
        //把向量初始化到算法参数
        AlgorithmParameters params = AlgorithmParameters.getInstance(KEY_ALGORITHM);
        params.init(new IvParameterSpec(ivByte));

        //指定用途,密钥,参数 初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, sKeySpec, params);

        //执行解密
        byte[] result = cipher.doFinal(dataByte);

        //解密后转成字符串
        data = new String(result);

        return data;
    }


   public static String  checkEncoder(String data) throws Exception {  
 //   public static void main(String[] args) throws Exception {
        //原始数据
 //       String data = "13100008575";
        //密钥
        String sessionKey = "ea8a706c4c34a168";
        //向量
        String iv = "ea8a706c4c34a168";

        //用Base64编码
      //  Base64.Encoder encoder = Base64.getEncoder();
      //  BASE64Decoder
        String baseData = new sun.misc.BASE64Encoder().encode(data.getBytes());
        String baseSessionKey = new sun.misc.BASE64Encoder().encode(sessionKey.getBytes());
        String baseIv = new sun.misc.BASE64Encoder().encode(iv.getBytes());

        //导入支持AES/CBC/PKCS7Padding的Provider
        Security.addProvider(new BouncyCastleProvider());

        //获取加密数据
        String encryptedData = encrypt(baseData,baseSessionKey,baseIv);
        System.out.println(encryptedData);
        //通过加密数据获得原始数据
        String dataReborn = decrypt(encryptedData,baseSessionKey,baseIv);

        //打印解密出来的原始数据
        System.out.println(dataReborn);
        return dataReborn;
    }

}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值