博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
参考别人的代码写的aes加密,记录一下(AES,ECB模式,填充PKCS5Padding,数据块128位,偏移量无,以hex16进制输出)...
阅读量:4616 次
发布时间:2019-06-09

本文共 3990 字,大约阅读时间需要 13 分钟。

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;    }}

 

转载于:https://www.cnblogs.com/JimmySeraph/p/10937699.html

你可能感兴趣的文章
css3转换
查看>>
将字符串中不同字符的个数打印出来
查看>>
java第三次上机
查看>>
android Javah生成JNI头文件
查看>>
npm创建react项目
查看>>
关于u32中查找和定位最后到bit Number of 1 Bits
查看>>
sql数据库查询
查看>>
云计算技能图谱
查看>>
委托、Lambda表达式和事件
查看>>
typecho模板制作代码收集
查看>>
Python学习笔记4:集合方法
查看>>
elasticsearch的监控脚本
查看>>
你还在为使用P/Invoke时,写不出win32 api对应的C#声明而犯愁吗?
查看>>
msbuild property metadata会overwrite msbuild task中的properties
查看>>
python系列前期笔记
查看>>
Android -- sqlite数据库随apk发布
查看>>
Android -- Fragment
查看>>
前端性能优化和规范
查看>>
python 之进程篇
查看>>
框架编程之路一
查看>>