JAVA_AES加密
2016-06-17 16:13
405 查看
使用AES加密解密的一个小Demo,其中用到一个第三方库:commons-codec-1.6
commons-codec-1.6库的官网:http://commons.apache.org/proper/commons-codec/download_codec.cgi
下载地址: http://download.csdn.net/detail/two_water/9552669
Demo:
package com.liangdianshui;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
*
* @author liangdianshui
*
*/
public class AES_ECB {
// 加密
public static String Encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new Base64().encodeToString(encrypted);// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
// 解密
public static String Decrypt(String sSrc, String sKey) throws Exception {
try {
// 判断Key是否正确
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = new Base64().decode(sSrc);// 先用base64解密
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
public static void main(String[] args) throws Exception {
/*
* 此处使用AES-128-ECB加密模式,key需要为16位。
*/
String cKey = "12345678abcdefgh";
// 需要加密的字串
String cSrc = "liangdianshui";
System.out.println(cSrc);
// 加密
String enString = AES_ECB.Encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = AES_ECB.Decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
}
}运行结果:
package com.liangdianshui;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
* AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES加密后,在进行Base64编码转化;
*/
public class AES_CBC {
/*
* 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
*/
private String sKey = "12345678abcdefgh";
private String ivParameter = "12345678abcdefgh";
private static AES_CBC instance = null;
private AES_CBC() {
}
public static AES_CBC getInstance() {
if (instance == null)
instance = new AES_CBC();
return instance;
}
// 加密
public String encrypt(String sSrc) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new Base64().encodeToString(encrypted);// 此处使用BASE64做转码。
}
// 解密
public String decrypt(String sSrc) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
// byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);
// 先用base64解密
byte[] encrypted1 = new Base64().decode(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public static void main(String[] args) throws Exception {
// 需要加密的字串
String cSrc = "liangdianshui";
System.out.println(cSrc);
// 加密
long lStart = System.currentTimeMillis();
String enString = AES_CBC.getInstance().encrypt(cSrc);
System.out.println("加密后的字串是:" + enString);
// 解密
lStart = System.currentTimeMillis();
String DeString = AES_CBC.getInstance().decrypt(enString);
System.out.println("解密后的字串是:" + DeString);
}
}运行结果:
Demo的下载地址:http://download.csdn.net/detail/two_water/9552675
commons-codec-1.6库的官网:http://commons.apache.org/proper/commons-codec/download_codec.cgi
下载地址: http://download.csdn.net/detail/two_water/9552669
Demo:
package com.liangdianshui;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
*
* @author liangdianshui
*
*/
public class AES_ECB {
// 加密
public static String Encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new Base64().encodeToString(encrypted);// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
// 解密
public static String Decrypt(String sSrc, String sKey) throws Exception {
try {
// 判断Key是否正确
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = new Base64().decode(sSrc);// 先用base64解密
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
public static void main(String[] args) throws Exception {
/*
* 此处使用AES-128-ECB加密模式,key需要为16位。
*/
String cKey = "12345678abcdefgh";
// 需要加密的字串
String cSrc = "liangdianshui";
System.out.println(cSrc);
// 加密
String enString = AES_ECB.Encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = AES_ECB.Decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
}
}运行结果:
package com.liangdianshui;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
* AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES加密后,在进行Base64编码转化;
*/
public class AES_CBC {
/*
* 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
*/
private String sKey = "12345678abcdefgh";
private String ivParameter = "12345678abcdefgh";
private static AES_CBC instance = null;
private AES_CBC() {
}
public static AES_CBC getInstance() {
if (instance == null)
instance = new AES_CBC();
return instance;
}
// 加密
public String encrypt(String sSrc) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new Base64().encodeToString(encrypted);// 此处使用BASE64做转码。
}
// 解密
public String decrypt(String sSrc) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
// byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);
// 先用base64解密
byte[] encrypted1 = new Base64().decode(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public static void main(String[] args) throws Exception {
// 需要加密的字串
String cSrc = "liangdianshui";
System.out.println(cSrc);
// 加密
long lStart = System.currentTimeMillis();
String enString = AES_CBC.getInstance().encrypt(cSrc);
System.out.println("加密后的字串是:" + enString);
// 解密
lStart = System.currentTimeMillis();
String DeString = AES_CBC.getInstance().decrypt(enString);
System.out.println("解密后的字串是:" + DeString);
}
}运行结果:
Demo的下载地址:http://download.csdn.net/detail/two_water/9552675
相关文章推荐
- Java中Iterable和Iterator的辨析
- java中栈和队列的实现和API用法
- Spring AOP
- spring mvc拦截器的使用记录
- Java线程:概念及原理
- java Date的格式转换
- kafka集群搭建和使用Java写kafka生产者消费者
- java 反射的基本使用
- maven在eclipse 中 导出war 的时候 导入本地jar
- Java中主线程如何捕获子线程抛出的异常
- rpm方式安装jdk后,路径查找
- Java进程的Perm Size突然陡增
- Java容器类的深入理解
- web项目中获取spring的bean对象
- java集合框架详解
- 单线程解析文件,根据文件关键词进行统计,并将结果输出(二)
- RxJava基础练习(1)
- spring代理模式 service远程调用,插件执行
- java中非对称加密(RSA)的使用
- java IO