AES加密

为了保证数据传输过程中的安全,加密使用对称加密算法AES 128位加密,加密模式采用CBC,填充模式采用PKCS5Padding方式,字符编码uft-8。

ASE加密和解密验证工具

package cn.eakay.tbox.charge.util.crypto;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
* @Title: AESUtil
* @Description: 数据加密 对称加密算法AES 128位加密,加密模式采用CBC,填充模式采用PKCS5Padding方式
* @author chy
* @date 2018/6/11 15:33
*/
public class AESUtil {

    /**
     * 加密字符串
     * @param sSrc 源字符串
     * @param sKey 加密key
     * @param ivParameter   向量
     * @return
     * @throws Exception
     */
    public static String Encrypt(String sSrc, String sKey, String ivParameter) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16        if (sKey.length() != 16) {
            System.out.print("Key长度不是16");
            return null;
        }
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        //"算法/模式/补码方式"
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        //使用CBC模式,需要一个向量iv,可增加加密算法的强度
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        //此处使用BASE64做转码功能,同时能起到2次加密的作用。
        //过滤\r\n换行符
        return new BASE64Encoder().encode(encrypted).replaceAll("\\r\\n","");
    }


    /**
     * 解密
     * @param sSrc  加密字符串
     * @param sKey  密码key
     * @param ivParameter    向量
     * @return
     * @throws Exception
     */
    public static String Decrypt(String sSrc, String sKey, String ivParameter) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16            if (sKey.length() != 16) {
                System.out.print("Key长度不是16");
                return null;
            }
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }




    /**
     * 测试代码
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String src="{\"FailReason\":\"\",\"OperatorID\":\"123456789\",\"SuccStat\":1,\"AccessToken\":\"0F495FD8CFC664FE862C1B148FB4383D\",\"TokenAvailableTime\":36288000}";
        String str = AESUtil.Encrypt(src, "1234567890abcdef", "1234567890abcdef");
        System.out.println(src);
        System.out.println("Base64加密后字符串:"+str);

        String des64="M4doTaek+oiC3aig1avQhwCuieD0w9kV1RhoxX2jF44YKnvF39FHJoeVr7yoUiM8mDAvIzfcoX8ejMM0pyg9pBr2YOPeCdg/3nw74VL3fOZjgKOi9ywVwP7hkhFnA8Vk26nBLY0xzot4CySbG252B9sWpcer6TQPNl6O80IefCXRa/Gjb0z9IEcAHPR/+FKm";
        str=AESUtil.Decrypt(des64, "1234567890abcdef", "1234567890abcdef");
        System.out.println("解密字符串:"+str);
    }

}
AES加密_第1张图片

你可能感兴趣的:(加密算法)