AES加密解密

package com.abc.util;

/**
 * Created by xxx.
 */

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

public class YxAESUtil {
    private final char[] bcdLookup = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    private final String charsetName = "utf-8";
    private final String defaultKey = "c63752f2258611eb800328107b42b865";

    private static YxAESUtil instance = null;

    public YxAESUtil() {}

    public static YxAESUtil getInstance() {
        if (null == instance) {
            instance = new YxAESUtil();
        }

        return instance;
    }

    /**
     * 解密
     *
     * @param content   密文
     * @param key 加密密码
     * @return String
     */
    public String decode(String content, String key) {
        String res = "";

        try {
            byte[] arr = string2ByteArr(content);
            byte[] raw = key.getBytes(charsetName);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] original = cipher.doFinal(arr);
            res = new String(original, charsetName);
        } catch (Exception e) {
            ///
        }

        return res;
    }

    /**
     * 加密
     *
     * @param content      原文
     * @param key 加密密码
     * @return String
     */
    public String encode(String content, String key) {
        String res = "";

        try {
            byte[] raw = key.getBytes(charsetName);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(content.getBytes(charsetName));
            return byteArrToString(encrypted);
        } catch (Exception e) {
            ///
        }

        return res;
    }

    /*
    根据默认密码进行加密
     */
    public String encode(String content) {
        return encode(content, this.defaultKey);
    }

    /*
    根据默认密码进行解密
     */
    public String decode(String content) {
        return decode(content, this.defaultKey);
    }

    /**
     * 将字节数组转换为16进制字符串
     * @param bcd 字节数组
     * @return String
     */
    private String byteArrToString(byte[] bcd) {
        StringBuffer s = new StringBuffer(bcd.length * 2);
        for (int i = 0; i < bcd.length; i++) {
            s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
            s.append(bcdLookup[bcd[i] & 0x0f]);
        }
        return s.toString();
    }

    /**
     * 将16进制字符串转换为字节数组
     * @param str 16进制字符串
     * @return byte[]
     */
    private byte[] string2ByteArr(String str) {
        byte[] bytes;
        bytes = new byte[str.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(str.substring(2 * i, 2 * i + 2),16);
        }
        return bytes;
    }
}

 

posted @ 2020-11-17 11:00  都是城市惹的祸  阅读(69)  评论(0)    收藏  举报