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. jquery文档内容的获取和设置

  2. Qt-MVC图形视图框架分解

    前面在<Qt-MVC图形视图框架出识>中我们了解了Qt图形视图框架中三个最基本的类,弄清他们的关系,本片小文,我们将对QGraphicsView,QGraphiceScene,QGraph ...

  3. Weekly Contest 78-------->808. Soup Servings

    There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a ...

  4. PTA 4-4 先序输出叶结点 【基础题】

    //二叉树的叶结点:度为0的结点. void PreorderPrintLeaves( BinTree BT ) { if(BT==NULL) //如果传下来根节点就是空,直接返回: return; ...

  5. memcached连接说明

    memcached 客户端与服务器端通信使用的基于文本的协议,不是二进制,可通过Telnet进行互交

  6. P3803 【模板】多项式乘法(NTT)

    传送门 NTT好像是比FFT快了不少 然而感觉不是很看得懂……主要是点值转化为系数表示那里…… upd:大概已经搞明白是个什么玩意儿了……吧…… //minamoto #include<bits ...

  7. 密码破解工具John the Ripper使用说明

    John the Ripper John 包描述 John the Ripper 既功能丰富又运行快速. 它在一个程序中结合了几种破解模式,并且可以根据您的特定需求进行全面地配置(你甚至可以使用支持C ...

  8. Promise对象深入理解

     目录 基本用法 返回另一个 Promise 实例 Promise.prototypeof.then Promise.prototype.catch Promise.prototype.finally ...

  9. mysql ERROR 2003 (HY000): Can't connect to MySQL server on '' (10060

    关闭防火墙即可连接成功: systemctl stop firewalld

  10. SpringBoot | quartz | @DisallowConcurrentExecution

    注释放在job类上, 作用: 将该注解加到job类上,告诉Quartz不要并发地执行同一个job定义(这里指特定的job类)的多个实例.