当前位置 : 主页 > 编程语言 > c++ >

SecurityCoderUtils

来源:互联网 收集:自由互联 发布时间:2021-06-30
安全加密算法AES工具类 import java.security.SecureRandom;import javax.crypto.*;import javax.crypto.spec.SecretKeySpec;public class SecurityCoderUtils { public SecurityCoderUtils() { } /** * AES 加密 * * @param inStr 需要加密的字
安全加密算法AES工具类
import java.security.SecureRandom;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

public class SecurityCoderUtils {

    public SecurityCoderUtils() {
    }

    /**
     * AES 加密
     *
     * @param inStr 需要加密的字符串
     * @param encryptKey 密钥字符串
     * @return 二进制
     * @throws Exception
     */
    private static byte[] encrypt(String inStr, String encryptKey) throws Exception {
        SecretKeySpec key = new SecretKeySpec(enCodeFormat(encryptKey), "AES");
        //Cipher:加密或解密工作类
        Cipher cipher = Cipher.getInstance("AES");
        //对Cipher初始化,加密模式
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(inStr.getBytes("UTF-8"));
    }

    /**
     * AES 解密
     *
     * @param inByte 二进制
     * @param encryptKey 密钥字符串
     * @return
     * @throws Exception
     */
    private static String decrypt(byte[] inByte, String encryptKey) throws Exception {
        SecretKeySpec key = new SecretKeySpec(enCodeFormat(encryptKey), "AES");
        //Cipher:加密或解密工作类
        Cipher cipher = Cipher.getInstance("AES");
        //对Cipher初始化,解密模式
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(inByte), "UTF-8");
    }

    /**
     * @param encryptKey 密钥字符串
     * @return 密钥字节数组
     * @throws Exception
     */
    private static byte[] enCodeFormat(String encryptKey) throws Exception {
        //密钥生成器
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(encryptKey.getBytes("UTF-8"));
        //默认128,获得无政策权限后可为192或256
        keyGen.init(128, secureRandom);
        //生成密钥
        SecretKey secretKey = keyGen.generateKey();
        //返回密钥字节数组
        return secretKey.getEncoded();
    }

    /**
     * AES 加密
     *
     * @param inStr 需要加密的字符串
     * @return 十六进制
     * @throws Exception
     */
    public static String encryptHexString(String inStr) throws Exception {
        return parseByte2HexString(encrypt(inStr,
                "06A3547EB3716D1F97946795500E6AAA"));
    }

    /**
     * AES 加密
     *
     * @param inStr 需要加密的字符串
     * @param encryptKey 密钥字符串
     * @return 十六进制
     * @throws Exception
     */
    public static String encryptHexString(String inStr, String encryptKey) throws Exception {
        return parseByte2HexString(encrypt(inStr, encryptKey));
    }

    /**
     * AES 解密
     *
     * @param hexStr 十六进制
     * @return
     */
    public static String decryptHexString(String hexStr) throws Exception {
        return decrypt(parseHexStr2Byte(hexStr),
                "06A3547EB3716D1F97946795500E6AAA");
    }

    /**
     * AES 解密
     *
     * @param hexStr 十六进制
     * @param encryptKey
     * @return
     */
    public static String decryptHexString(String hexStr, String encryptKey) throws Exception {
        return decrypt(parseHexStr2Byte(hexStr), encryptKey);
    }

    /**
     * 将二进制转换成十六进制
     *
     * @param buffer
     * @return
     */
    private static String parseByte2HexString(byte buffer[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buffer.length; i++) {
            String hex = Integer.toHexString(buffer[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将十六进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    private static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * 随机生成密钥
     */
    private static String generateKey() throws Exception {
        //密钥生成器
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        //默认128,获得无政策权限后可为192或256
        keyGen.init(128);
        //生成密钥
        SecretKey secretKey = keyGen.generateKey();
        //密钥字节数组
        byte[] key = secretKey.getEncoded();
        return parseByte2HexString(key);
    }

//    public static void main(String args[]) throws Exception {
//		String str = String.format("%016d", 100);
//        //System.out.println("随机生成密钥:" + generateKey());
//        System.out.println("原始:" + str);
//        System.out.println("AES加密后:" + encryptHexString(str));
//        System.out.println("AES解密后:" + decryptHexString(encryptHexString(str)));
//        System.out.println("AES加密后:" + encryptHexString(str,
//                "06A3547EB3716D1F97946795500E6AAA"));
//        System.out.println("AES解密后:" + decryptHexString(encryptHexString(str),
//                "06A3547EB3716D1F97946795500E6AAA"));
//    }
}
网友评论