开箱即用的AES CBC加密解密算法分享

开箱即用的AES CBC加密解密算法分享

这两个天一个朋友让帮忙有java封装一个AES CBC算法的加解密,果断的Ctrl+CCtrl+V。修修改改形成了一下代码。至于什么是AES CBC模式加密算法,我也不知道(猛男落泪~~)。网上一搜一大堆这些资料感兴趣的可以百度。
Java自带没有AES加密算法的jar包,需要手动添加,此处使用maven自动下载:


    org.bouncycastle
        bcprov-jdk16
        1.46

接下来就是正文了:

package cn.diaoyc.aes;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author Tony     
 * @projectName  AESCB
 * @title  AecCBCUtil   
 * @package  cn.diaoyc.aes   
 * @date 2020/5/10 -- 10:06
 * @version v1.1
 *  
 */
public class AecCBCUtil {
    // 加密方式
    private static String ALGORITHM = "AES";
    //算法数据填充方式
    private static String ALGORITHM_FILL_TYPE = "AES/CBC/PKCS5Padding";
    //字符编码(String转byte[] 使用UTF-8的编码格式)
    private static String ENCODING_FORMAT = "UTF-8";

    // 内部实例参数
    private static AecCBCUtil instance = null;

    private AecCBCUtil() {

    }

    //采用单例模式,此静态方法供外部直接访问
    public static AecCBCUtil getInstance() {
        if (instance == null) {
            instance = new AecCBCUtil();
        }
        return instance;
    }

    /**
     * 加密
     * @param originalContent 明文
     * @param encryptKey 密钥
     * @param ivParameter 初始偏移量
     * @return 返回加密后的字符串
     */
    public String encrypt(String originalContent, String encryptKey, String ivParameter) {
        try {
            //处理传进来的明文
            byte[] originalContentBytes = originalContent.getBytes(ENCODING_FORMAT);

            //处理传进来的密钥(String转成byte[])
            byte[] enKeyBytes = encryptKey.getBytes();

            //处理传进来的偏移量(String转成byte[])
            byte[] ivParameterBytes = ivParameter.getBytes();

            //根据传入的密钥按照AEC方式构造密钥
            SecretKeySpec sKeySpec = new SecretKeySpec(enKeyBytes, ALGORITHM);

            //根据传入的偏移量指定一个初始化偏移量
            IvParameterSpec iv = new IvParameterSpec(ivParameterBytes);
            //根据数据填充方式生成一个加解密对象
            Cipher cipher = Cipher.getInstance(ALGORITHM_FILL_TYPE);

            //初始化 传入类型(加密/解密)、构造过的密钥、指定的初始偏移量
            cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);

            //加密操作
            byte[] encrypted = cipher.doFinal(originalContentBytes);

            //base64转码
            String cipherString = new BASE64Encoder().encode(encrypted);
            return cipherString;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 解密
     * @param cipherStr 加密过的密文
     * @param encryptKey 密钥
     * @param ivParameter 偏移量
     * @return 返回解密过后的字符串
     */
    public String decrypt(String cipherStr, String encryptKey, String ivParameter) {
        try {
            //处理传进来的密文 使用base64解密
            byte[] cipherStrByte = new BASE64Decoder().decodeBuffer(cipherStr);

            //处理传进来的密钥(String转成byte[])   可以指定编码格式为:ASCII
            byte[] enKeyBytes = encryptKey.getBytes();

            //处理传进来的偏移量(String转成byte[])
            byte[] ivParameterBytes = ivParameter.getBytes();

            //根据传入的密钥按照AEC方式构造密钥
            SecretKeySpec sKeySpec = new SecretKeySpec(enKeyBytes, ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM_FILL_TYPE);
            IvParameterSpec iv = new IvParameterSpec(ivParameterBytes);
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec, iv);

            //获得解密的明文数组
            byte[] original = cipher.doFinal(cipherStrByte);
            return new String(original, ENCODING_FORMAT);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

public static void main(String[] args) {
        String originalContent = "{\"fsadfds\":{\"fdsfsdf\":\"sdfdfds\",\"sfdsfsd\":{\"sfdsfd\":\"\",\"sfsdfsdf\":\"\",\"sfdsfdsf\":\"\",\"sfsfsdf\":\"\"},\"sfsfs\":\"sfsfsf\",\"Type\":1,\"sfsfsf\":\"sfsfsfsdfsdfdfsdfs\"},\"P\":\"sfsfsf-sfdsfsdf-123456\",\"U\":\"fsdfdfds-sfsdf\"}";
        String encryptKey = "N200fqw455uEghx5"; //密钥
        String ivParameter = "Ipahgj7Jkl0Gwe7s";//偏移量

        System.out.println("原始字符串:" + originalContent);

        //加密
        String encrypt = AecCBCUtil.getInstance().encrypt(originalContent, encryptKey, ivParameter);
        System.out.println("加密后的字符串是:" + encrypt);

        System.out.println("****************我是分割线*******************");
        //解密
        String decrypt = AecCBCUtil.getInstance().decrypt(encrypt, encryptKey, ivParameter);
        System.out.println("解密后的:" + decrypt);

    }

}

你可能感兴趣的:(开箱即用的AES CBC加密解密算法分享)