带偏移量的AES加密工具
自定义的一个对称加密工具类AESUtil.java
public static final String ENCRYPTION_ALGORITHM = "AES";
public static final String CIPHER_PARAM = "AES/CBC/PKCS5Padding";
private static final String DEFAULT_KEY_AND_IV = "1234567890ABCDEF";//带偏移量的密钥和偏移量似乎是固定16位长度的
private static AESUtil instance = null; /** 偏移量 **/
private static IvParameterSpec iv;
/** 密钥 **/
private static SecretKeySpec key; /**
* @Title: getInstance
* @Description: 单例AES加密工具,使用默认的密钥以及偏移量
* @return AESUtil
* @author hanzhiyong
* @date 2019-4-26下午4:51:12
*/
public static AESUtil getInstance() {
if (instance == null) {
instance = new AESUtil(DEFAULT_KEY_AND_IV);
}
return instance;
}
/**
* @Title:AESUtil
* @Description:自定义偏移量构造AES加密工具,使用默认的KEY
* @param ivParameter 原始偏移量字符串
*/
public AESUtil(String ivParameter) {
iv = new IvParameterSpec(ivParameter.getBytes());
key = new SecretKeySpec(DEFAULT_KEY_AND_IV.getBytes(), ENCRYPTION_ALGORITHM);
} /**
* @Title:AESUtil
* @Description:自定义偏移量和密钥构造AES加密工具
* @param secretKey 原始密钥字符串
* @param ivParameter 原始偏移量字符串
*/
public AESUtil(String secretKey, String ivParameter) {
iv = new IvParameterSpec(ivParameter.getBytes());
key = new SecretKeySpec(secretKey.getBytes(), ENCRYPTION_ALGORITHM);
} /**
* 取得密钥
*
* @throws Exception
*/
public static String getKey() {
Base64 base64 = new Base64();
System.out.println("密钥String:" + new String(key.getEncoded()));
return base64.encodeToString(key.getEncoded());
} /**
* 取得偏移量
*/
public static String getIv() throws Exception {
Base64 base64 = new Base64();
System.out.println("偏移量String:" + new String(iv.getIV()));
return base64.encodeToString(iv.getIV());
}
/**
* @Title: encrypt
* @Description: AES加密
* @param enString 用来加密的明文
* @throws Exception
* @return String 加密后密文
* @author hanzhiyong
* @date 2019-4-26下午3:57:17
*/
public String encrypt(String enString) throws Exception {
Base64 base64 = new Base64();
Cipher cipher = Cipher.getInstance(CIPHER_PARAM);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encrypted = cipher.doFinal(enString.getBytes("utf-8"));
return base64.encodeToString(encrypted); }
/**
* @Title: decrypt
* @Description: AES解密
* @param deString 用来解密的密文
* @throws Exception
* @return String 解密后的明文
* @author hanzhiyong
* @date 2019-4-26下午3:58:06
*/
public String decrypt(String deString) throws Exception {
try {
Base64 base64 = new Base64();
Cipher cipher = Cipher.getInstance(CIPHER_PARAM);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decrypted = base64.decode(deString);
byte[] original = cipher.doFinal(decrypted);
return new String(original, "utf-8");
} catch (Exception ex) {
return null;
}
} /**
* @Title: main
* @Description: 加密解密测试用例
* @param args
* @throws Exception
* @return void
* @author hanzhiyong
* @date 2019-4-23下午6:41:16
*/
@Deprecated
public static void main(String args[]) throws Exception {
String sourceString = "测试用来加密的数据";
System.out.println("sourceString加密前: " + sourceString); System.out.println("=========================单例的加密工具=========================");
System.out.println("base64加密后密钥:" + AESUtil.getInstance().getKey());
System.out.println("base64加密后偏移量:" + AESUtil.getInstance().getIv());
System.out.println("加密后sourceString: " + AESUtil.getInstance().encrypt(sourceString));
System.out.println("解密后sourceString: " + AESUtil.getInstance().decrypt(AESUtil.getInstance().encrypt(sourceString))); AESUtil aesUtil = new AESUtil("0123456789111111");
System.out.println("=========================自定义偏移量的工具=========================");
System.out.println("base64加密后密钥:" + aesUtil.getKey());
System.out.println("base64加密后偏移量:" + aesUtil.getIv());
System.out.println("加密后sourceString: " + aesUtil.encrypt(sourceString));
System.out.println("解密后sourceString: " + aesUtil.decrypt(aesUtil.encrypt(sourceString))); }
带偏移量的AES加密工具的更多相关文章
- Java AES 加密工具类
package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...
- Android AES加密工具类实现(基础回顾)
package com.powercreator.cms.util; import java.security.SecureRandom; import javax.crypto.Cipher; im ...
- AES 加密工具类
/** * AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES加密后,在进行Base64编码转化: */public class AESOperator { /* * 加密用的 ...
- AES加密工具类(对称加密算法)
import java.nio.charset.Charset; import java.security.Key; import javax.crypto.Cipher;import javax.c ...
- AES加密工具
public class AES { /** * 加密 * * @param content * 需要加密的内容 * @param password * 加密密码 * @return */ publi ...
- AES加密解密工具类封装(AESUtil)
package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...
- 通过Go实现AES加密和解密工具
本文包含如下两个内容: AES加密介绍及实现原理 Go实现AES加密和解密工具 AES加密介绍及实现原理 AES( advanced encryption standard)使用相同密钥进行加密和解密 ...
- Jmeter参数的AES加密使用
在Jmeter日常实践中,大家应该都遇到过接口传参需要加密的情况.以登陆为例,用户名和密码一般都需要进行加密传输,在服务端再进行解密,这样安全系数会更高,但在使用jmeter进行接口测试的时候,怎样发 ...
- AES采用CBC模式128bit加密工具类
写在前面 安全测试ECB模式过于简单需要改为CBC模式加密以下为工具类及测试 AESUtils.java package com.sgcc.mobile.utils; import sun.misc. ...
随机推荐
- SOJ 1017 Power of Cryptography 库函数精度
Background Current work in cryptography involves (among other things) large prime numbers and comput ...
- webpack中热模块更新
Hot Module Replacement,热模块更新,很多时候会简写成HMR. "scripts": { "start": "webpack-de ...
- ZooKeeper学习之路 (七)ZooKeeper设计特点及典型应用场景
ZooKeeper 特点/设计目的 ZooKeeper 作为一个集群提供数据一致的协调服务,自然,最好的方式就是在整个集群中的 各服务节点进行数据的复制和同步. 数据复制的好处 1.容错:一个节点出错 ...
- 随手练——HDU Safe Or Unsafe (小根堆解决哈夫曼问题)
HDU 2527 :http://acm.hdu.edu.cn/showproblem.php?pid=2527 哈夫曼树,学完就忘得差不多了,题目的意思都没看懂,有时间复习下,看了别人的才知道是怎么 ...
- HTTP 错误 401.0 - Unauthorized 的解决方案
1.安装vs2015后,以前做的项目中Forms身份验证,竟然不能使用了 2.打开当前项目属性,将windows身份验证属性改为启用 3.vs2015生成的mvc项目中,webconfig缺失auth ...
- Go并发与.Net TAP
Go package main import "fmt" func sum(arrays []int, ch chan int) { fmt.Println(arrays) sum ...
- Dubbo实践(十六)集群容错
Dubbo作为一个分布式的服务治理框架,提供了集群部署,路由,软负载均衡及容错机制.下图描述了Dubbo调用过程中的对于集群,负载等的调用关系: 集群 Cluster 将Directory中的多个In ...
- ASP.NET Core Middleware (转载)
What is Middleware? Put simply, you use middleware components to compose the functionality of your A ...
- 一篇很好的解释了.Net Core, .Net Framework, .Net standard library, Xamarin 之间关系的文章 (转载)
Introducing .NET Standard In my last post, I talked about how we want to make porting to .NET Core e ...
- Oracle 11G 隐含参数“_controlfile_autobackup_delay”
RMAN设置控制文件自动备份,当发生数据库备份时,或建表空间,删除log文件等物理结构发生改变时,oracle会自动备份控制文件. Oracle 10g会立刻备份,Oracle 11g会有几分钟的延迟 ...