参考别人的代码写的aes加密,记录一下(AES,ECB模式,填充PKCS5Padding,数据块128位,偏移量无,以hex16进制输出)

package org.jimmy.autosearch2019.test;

import java.security.SecureRandom;

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

public class TestAes2019052801 {

    public static void main(String[] args) {
        try {
            String content = "123456";
            String secretKey = "123456";
            byte[] encryptedContentBytes = encrypt(content, secretKey);
            String encryptedContent = parseBinaryToHexStr(encryptedContentBytes);
            System.out.println("加密前的文本:" + content);
            System.out.println("加密后的文本:" + encryptedContent);
            byte[] encryptedContentToBinaryStr = parseHexToBinaryStr(encryptedContent);
            byte[] decryptedContentBytes = decrypt(encryptedContentToBinaryStr, secretKey);
            String decryptedContent = new String(decryptedContentBytes);
            System.out.println("解密后的文本:" + decryptedContent);
        } catch(Exception e) { 
            e.printStackTrace();
        }
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午2:56:42
     * @detail 16进制字符串转换2进制字符串
     */
    public static byte[] parseHexToBinaryStr(String hexStr) throws Exception {
        byte[] bytes = null;
        if(hexStr.length() < 1) {
            return bytes;
        }
        bytes = 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);
            bytes[i] = (byte) (high * 16 + low);
        }
        return bytes;
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午2:54:56
     * @detail 2进制字符串转换16进制字符串
     */
    public static String parseBinaryToHexStr(byte[] bytes) throws Exception {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xff);
            if(hex.length() == 1) {
                hex = "0" + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午3:30:33
     * @detail 解密
     */
    public static byte[] decrypt(byte[] content, String secretKey) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128, new SecureRandom(secretKey.getBytes()));
        SecretKey generatedSecretKey = keyGenerator.generateKey();
        byte[] encodedBytes = generatedSecretKey.getEncoded();
        SecretKeySpec secretKeySpec = new SecretKeySpec(encodedBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] result = cipher.doFinal(content);  
        return result;
    }
    
    /**
     * @author ラピスラズリ(Dawn)
     * @date 2019年5月28日 下午2:55:25
     * @detail aes加密
     */
    public static byte[] encrypt(String content, String secretKey) throws Exception {
        // 创建AES的Key生产者
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        // 利用用户密码作为随机数初始化出
        // 128位的key生产者
        //加密没关系,SecureRandom是生成安全随机数序列,password.getBytes()是种子,
        //只要种子相同,序列就一样,所以解密只要有password就行
        keyGenerator.init(128, new SecureRandom(secretKey.getBytes()));
        // 根据用户密码,生成一个密钥
        SecretKey generatedSecretKey = keyGenerator.generateKey();
        // 返回基本编码格式的密钥,如果此密钥不支持编码,则返回
        byte[] encodedBytes = generatedSecretKey.getEncoded();
        // 转换为AES专用密钥
        SecretKeySpec secretKeySpec = new SecretKeySpec(encodedBytes, "AES");
        // 创建密码器
        Cipher cipher = Cipher.getInstance("AES");
        byte[] contentBytes = content.getBytes("utf-8");
        // 初始化为加密模式的密码器
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        //加密
        byte[] result = cipher.doFinal(contentBytes);
        return result;
    }

}
2015年10月-2016年3月 总计:5个月.
2016年11月-2017年6月 总计:7个月.
2017年7月-2018年4月 总计:9个月.
2018年5月-2018年5月 总计:1个月.
2018年6月-2018年12月 总计:6个月.
2019年1月-2019年12月 总计11个月.
2020年2月-2021年2月 总计13个月.
所有总计:5+7+9+1+6+11+13=52个月(4年4个月).
本人认同二元论.我是理想主义者,现实主义者,乐观主义者,有一定的完美主义倾向.不过,一直都是咸鱼(菜鸟),就算有机会,我也不想咸鱼翻身.(并不矛盾,因为具体情况具体分析)
英语,高等数学,考研,其他知识学习打卡交流QQ群:946556683
原文地址:https://www.cnblogs.com/JimmySeraph/p/10937699.html