AES加密

来源:互联网 发布:淘宝宁美国度装机怎样 编辑:程序博客网 时间:2024/06/05 20:17
import java.util.Base64;import java.util.Base64.Decoder;import java.util.Base64.Encoder;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;public class AESUtil {    private static final String IV_STRING = "16-Bytes--String";    /**     * 加密     * @param content     * @param key     * @return     * @throws Exception     */    public static String encryptAES(String content, String key)            throws Exception {        byte[] byteContent = content.getBytes("UTF-8");        // 注意,为了能与 iOS 统一        // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成        byte[] enCodeFormat = key.getBytes();        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");        byte[] initParam = IV_STRING.getBytes();        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);        // 指定加密的算法、工作模式和填充方式        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);        byte[] encryptedBytes = cipher.doFinal(byteContent);        // 同样对加密后数据进行 base64 编码        Encoder encoder = Base64.getEncoder();        return encoder.encodeToString(encryptedBytes);    }    /**     * 解密     *      * @param content     * @param key     * @return     * @throws Exception     */    public static String decryptAES(String content, String key)            throws Exception {        // base64 解码        Decoder decoder = Base64.getDecoder();        byte[] encryptedBytes = decoder.decode(content);        byte[] enCodeFormat = key.getBytes();        SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");        byte[] initParam = IV_STRING.getBytes();        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);        byte[] result = cipher.doFinal(encryptedBytes);        return new String(result, "UTF-8");    }    public static void main(String[] args) throws Exception {        String key=getChar(16);        System.out.println(key);        String contextString ="http://60.255.161.39/vod/sccn,COST2016120214152367.m3u8";        String encryptAES = encryptAES(contextString, key);        System.out.println("加密后-》"+encryptAES);        System.err.println("-----------------------------");        String decryptAES = decryptAES(encryptAES, key);        System.out.println("解密后-》"+decryptAES);    }    private static String baseChars = "qwertyuiopasdfghjklzxcvbnm0123456789";    /**     * @Title: getChar      * @Description:  随机字符串     * @param @param len     * @param @return    参数说明     * @return String    返回类型     * @author lihongliang     */    public static String getChar(int len){        String chars = "";        for(int i=0;i<len;i++){            int baseIndex = (int)(Math.random() * baseChars.length()) + 1;            chars += baseChars.charAt(baseIndex);        }        return chars.toUpperCase();    }}
原创粉丝点击