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. MessageFomat学习

    MessageFomat 提供了一种以与语言无关的方式生成连接消息的方法. 用它来构造消息,显示给最终用户. 1.MessageFormat的格式 MessageFormatPattern:Forma ...

  2. JsonCpp——json文件的解析

    定义: 官网: http://json.org/ 在线解析器:http://json.cn/ http://www.bejson.com/ JSON(JavaScript Object Notatio ...

  3. HDU2819【二分匹配与矩阵的秩】

    题意: 给出一个矩阵问能否实现对角线全部是1,能的话输出路径,不能的话输出-1 思路: 首先根据矩阵的性质,这一定是一个满秩矩阵,所以只根据行或列交换就一定能实现. 所以行和列构成二分图,然后跑一发匈 ...

  4. python 如何在 command 中能够找到 其他module

    部分代码如下: __author__ = 'norsd' # coding=utf8 # 上句说明使用utf8编码 try: import os import sys import time #关键语 ...

  5. bzoj 3158: 千钧一发【最小割】

    这个条件非常妙啊,奇数和奇数一定满足1,因为\( (2a+1)^2+(2b+1)^2=4a^2+4a+4b^2+4b+2=2(2(a^2+a+b^2+b)+1) \)里面这个一定不是平方数因为除二后是 ...

  6. nacos启动

    nacos下载 https://github.com/alibaba/nacos 1.执行数据库脚本 2.修改配置文件application.propertiesspring.datasource.p ...

  7. Linq 知识总结

    一.说明: LINQ,语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相 ...

  8. Hexo - Template render error unexpected token

    问题与分析 今天发现在使用hexo g时报错如下: FATAL Something's wrong. Maybe you can find the solution here: http://hexo ...

  9. MySQL 命令(一)

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

  10. [coci2015-2016 coii] Palinilap【字符串 哈希】

    传送门:http://www.hsin.hr/coci/archive/2015_2016/ 进去之后点底下的那个.顺带说一句,题目既不是一个英文单词,也不是克罗地亚单词,估计只是从回文串的英文单词p ...