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. supervisor简明教程

    一.supervisor是什么 Linux的后台进程运行有好几种方法,例如nohup,screen等,但是,如果是一个服务程序,要可靠地在后台运行,我们就需要把它做成daemon,最好还能监控进程状态 ...

  2. npm和package.json那些不为常人所知的小秘密

    此文已由作者黄锴授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 强大的命令功能 如果你没使用过script,那你可算是从来没手动编辑过package.json.script作 ...

  3. vue key值的重复键问题报错

    1.问题描述:在vue2.0+ 中做一个公用的评论组件,:key使用的时创建评论的时间,当加载更多的时候,会报错: Duplicate keys detected: '2019-01-24T07:15 ...

  4. atcoder057D(组合数模板)

    题目链接:http://abc057.contest.atcoder.jp/tasks/abc057_d 题意:给出n个数,可以选择x~y个数,使其平均值最大,求其最大平均数以及选择方案数. 思路:只 ...

  5. python3 requests模块 基本操作

    import requests import json # 1.HTTP方法 requests.get('https://github.com/timeline.json') #GET请求 reque ...

  6. assembly x86(nasm)画三角形等图形的实现

    参考了一位大佬的博客 https://blog.csdn.net/qq_40298054/article/details/84496944传送门 https://blog.csdn.net/qq_40 ...

  7. log4j.xml中Filter的用法

    前言 log4j中常用的Filter分为四种:DenyAllFilter.LevelMatchFilter.LevelRangeFilter.StringMatchFilter. 当appender匹 ...

  8. 在接口的实现类里使用@Override注解报错

    问题分析 @Override注解用来检测子类对父类或接口的方法的重写是否正确,但有一次我在Eclipse里对接口的实现类里使用@Override注解却报错,不过在父类的子类里使用该注解却是正常的. 百 ...

  9. Hexo搭建博客教程(2) - 博客的简单个性化配置

    本章主要讲博客的个性化,譬如站点的基本配置(语言.头像.站点图标等).安装新的Hexo主题(NexT主题)以及主题的配置. 1. 修改站点配置 打开站点配置文件 ,找到: # Site title: ...

  10. JSP | 基础 | 连接数据库

    package util; import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.*; ...