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. SmartSql使用教程(1)——初探,建立一个简单的CURD接口服务

    一.引言 最近SmartSql被正式引入到了NCC,借着这个契机写一个使用教程系列 二.SmartSql简介[摘自官方文档] 1. SmartSql是什么? SmartSql = MyBatis + ...

  2. phpstudy的mysql版本升级至5.7

    phpstudy安装的mysql版本一般都是5.5或5.4的,但是有时候做项目又必须用到mysql5.7版本,所以我们现在来看一下如何在phpstudy的环境下将mysql版本升级至5.7 温馨提醒: ...

  3. 自定义Mybatis返回类型及注意事项

    一.自定义返回拦截器package com.yaoex.crm.service.util; import org.apache.ibatis.session.ResultContext;import ...

  4. 剑指OFFER之二叉树的镜像(九度OJ1521)

    题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000,n代表将要输入的二叉树节点的 ...

  5. JS中的柯里化(currying) 转载自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com]

    JS中的柯里化(currying) by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpr ...

  6. iOS集成支付宝支付

    本文由本人原创发表于博客园,转载请注明出处 http://www.cnblogs.com/wangqw/p/5074907.html 一. 开发前准备 iOS 支付宝SDK下载地址:(内含iOS An ...

  7. Zju1610 Count the Colors(lazy标记详解)

    Description 画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了. 你的任务就是要数出你随后能看到的不同颜色的段的数目.  Input 每组测试数据第一行只有一个整数n, 1 < ...

  8. ZOJ 4033 CONTINUE...?(The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)

    #include <iostream> #include <algorithm> using namespace std; ; int a[maxn]; int main(){ ...

  9. Mysql 开启 Slow 慢查询

    1:登录数据库查看是否已经开启了Slow慢查询: mysql> show variables like 'slow_query%'; 2:开启Mysql slow日志: 默认情况下slow_qu ...

  10. android webview 视频相关

    //设置为false则可以自动播放页面音视频 页面必须设置autoplay或者调用document.getElementById("video").play(); $(" ...