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

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

  1. 为什么加密后的数据往往都是base64输出而不是hex16进制输出

    通常加密后的数据都是字节数组,比如流行的aes128对称加密,还有Rsa非对称加密,加密后得到了一个字节数组,这个字节数组存在内存中,往往我们需要输出得到我们人眼能看到的字符. 加密aes(xxx) ...

  2. 前端JS AES加密 后端PHP AES加解密

    <!DOCTYPEhtml> <html> <head> <title>aes demo</title> </head> < ...

  3. PHP对称加密-AES加密、DES加密

    对称加密 对称加密算法是指,数据发信方将明文(原始数据)和密钥一起经过加密处理后,使其变成复杂的加密密文发送出去.收信方收到密文后,若要解读原文,则需要使用加密密钥及相关算法的逆算法对密文进行解密,使 ...

  4. 自写AES加密解密工具类

    此类主要用于加密与解密,采用128位ECB模式,PKCS5Padding填充补位. 可使用方法为加密返回二进制encryptBin(content, key).加密返回十六进制encryptHex(c ...

  5. AES加密原理和AOE工程实践

    在AI业务的开发的过程中,我们常常需要对模型文件进行加密.我们从以下几个方面来说一说AES的加密原理以及AOE里的工程实践. 常见的加密算法 AOE对模型加密需求的思考 AES的加密原理 AOE工程实 ...

  6. java php c# 三种语言的AES加密互转

    java php c# 三种语言的AES加密互转 最近做的项目中有一个领取优惠券的功能,项目是用php写得,不得不佩服,php自带的方法简洁而又方便好用.项目是为平台为其他公司发放优惠券,结果很囧的是 ...

  7. Java AES加密解密工具 -- GUI 、在线传输文件

    原理 对于任意长度的明文,AES首先对其进行分组,每组的长度为128位.分组之后将分别对每个128位的明文分组进行加密. 对于每个128位长度的明文分组的加密过程如下:     (1)将128位AES ...

  8. openssl AES加密以及padding

    好习惯,先上代码再说事 加密 void AesEncrypt(unsigned char* pchIn, int nInLen, unsigned char *ciphertext, int & ...

  9. Java 关于密码处理的工具类[MD5编码][AES加密/解密]

    项目中又遇到了加密问题,又去翻了半天,然后做测试,干脆就把常用的两类小结一下. 1.第一种所谓的MD5加密 其实也不算加密,只是基于Hash算法的不可逆编码而已,等于说,一旦经过MD5处理,是不可能从 ...

随机推荐

  1. QDUOJ LC的课后辅导 单调递增栈

    LC的课后辅导 发布时间: 2015年9月19日 21:42   时间限制: 1000ms   内存限制: 256M 描述 有一天,LC给我们出了一道题,如图: 这个图形从左到右由若干个 宽为1 高不 ...

  2. android通过获取摄像头照片,实时与点传输

    http://blog.csdn.net/csh159/article/details/7926654/ GetIP: [html] view plain copy print? package co ...

  3. NDAP 日志

    2014.04.29 1.理论债券价格CalculateExpetedBondPrice计算有误差 CalculateLibrary中的计算理论债券价格(计算理论期货价格的反函数)和正确结果有误差(可 ...

  4. 如何使得 python 脚本 不一闪而过

    1.  简单的方法是在最后加上如下语句: os.system("pause") 2. 但是这个不一定有用,原因是可能在之前的代码中发生异常,那么我们看到的效果也是直接一闪而过 办法 ...

  5. C 语言实例 - 计算平均值

    C 语言实例 - 计算平均值 C 语言实例 C 语言实例 使用数组来计算几个数的平均值. 实例 #include <stdio.h> int main() { int n, i; ], s ...

  6. 001-JDK安装

    1.确定JDK的具体版本号 [root@bogon ~]# rpm -qa | grep jdk java-1.7.0-openjdk-1.7.0.91-2.6.2.3.el7.x86_64 java ...

  7. Vuex目录结构推荐

    目录结构如下: - src - store // 在src目录下 新建一个store文件夹 - mutations.js // mutations - mutaions_types.js // mut ...

  8. Mysql | 总结 | 常用的查询语句(单表查询)

    1. 查询单表全部 select* from 数据表名; 2. 查询单表中一个或者多个字段 select 字段1,字段2 from 数据表名; 3. 查询单表中的指定信息 select* from 数 ...

  9. MySQL 命令(一)

    mysql 数据库:保存有组织的数据的容器.(通常是一个文件或一组文件) 表:某种特定类型数据的结构化清单.数据库中的每个表都有一个名字,用来标识自己,此名字是唯一的 列:表中的一个字段.所有表都是由 ...

  10. 洛谷1072(gcd的运用)

    已知正整数a0,a1,b0,b1,设某未知正整数x满足: 1. x 和 a0 的最大公约数是 a1​: 2. x 和 b0​ 的最小公倍数是b1. Hankson 的“逆问题”就是求出满足条件的正整数 ...