package com.wintv.common;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/*******************************************************************************
* AES加解密算法
*
* @author arix04
*
*/
public class AES {
// 加密
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();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
return new BASE64Encoder().encode(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("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("0102030405060708"
.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
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 {
/*
* 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
* 此处使用AES-128-CBC加密模式,key需要为16位。
*/
String cKey = "1234567890123456";
// 需要加密的字串
String cSrc = "Email : arix04@xxx.com";
System.out.println(cSrc);
// 加密
long lStart = System.currentTimeMillis();
String enString = AES.Encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
long lUseTime = System.currentTimeMillis() - lStart;
System.out.println("加密耗时:" + lUseTime + "毫秒");
// 解密
lStart = System.currentTimeMillis();
String DeString = AES.Decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println("解密耗时:" + lUseTime + "毫秒");
}
}
AES加解密算法,使用byte2hex做转码:
package com.wintv.common;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/*******************************************************************************
* AES加解密算法
*
* @author arix04
*
*/
public class AES {
// 加密
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();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
return byte2hex(encrypted).toLowerCase();
}
// 解密
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("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("0102030405060708"
.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = hex2byte(sSrc);
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
public static byte[] hex2byte(String strhex) {
if (strhex == null) {
return null;
}
int l = strhex.length();
if (l % 2 == 1) {
return null;
}
byte[] b = new byte[l / 2];
for (int i = 0; i != l / 2; i++) {
b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
16);
}
return b;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
public static void main(String[] args) throws Exception {
/*
* 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
*/
String cKey = "1234567890123456";
// 需要加密的字串
String cSrc = "Email : arix04@xxx.com";
System.out.println(cSrc);
// 加密
long lStart = System.currentTimeMillis();
String enString = AES.Encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
long lUseTime = System.currentTimeMillis() - lStart;
System.out.println("加密耗时:" + lUseTime + "毫秒");
// 解密
lStart = System.currentTimeMillis();
String DeString = AES.Decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println("解密耗时:" + lUseTime + "毫秒");
}
}
分享到:
相关推荐
用C语言实现的AES加密解密算法,用C语言实现的AES加密解密算法
是标准的AES算法,支持在线AES加解密网站互解。 本源码从CSDN一位前辈的源码基础上做了更改,增加支持加密返回BASE64,更加完善。在此感谢原作者: http://download.csdn.net/download/qiaohaidong/9477264 需要注意...
java代码实例 AES加密解密 Base64加密解密
支持 CFB, OFB, CBC, ECB 模式,支持256字节以下任意长度数据加解密,也可以放开成任意长度数据加解密。 其中:AES.cpp 中为算法的实现代码;Test.cpp为测试程序
实现的AES加密解密算法,高级加密标准(AES,Advanced Encryption Standard)为最常见的对称加密算法(微信小程序加密传输就是用这个加密算法的)。对称加密算法也就是加密和解密用相同的密钥
java实现SHA1、SHA、MD5、AES加密、AES解密、BASE64解密、BASE64加密,以及apache BASE64 jar和源码
Base64转码,AES加密解密,IOS,Android,Java 后台。 本工程可以直接使用,而且与IOS,Android,JAVA 已调通。 主要用于客户端进行通信,数据之间地安全性。对数据进行加密和解密。 适用于银行等较为保密的项目中。 ...
AES加密解密轮转算法工具,最大支持256bit长度的密钥,明文支持HEX和字串。可供学习设计参考。
AES加密解密算法演示支持d7及xe AES加密解密算法演示支持d7及xe
C#实现AES算法,可自定义私钥的加密解密方法,使用此方法加密解密不容易被破解。
本文就简单介绍如何通过JAVA实现AES加密: /** ...我们在AES加密的时候需要使用一个加密算的公共密钥来实现加密和解密; 加密后的字节数组不能直接转化为字符串,需要我们通过给出的两个方法转化;
AES加密解密算法C++,Rijndael加密算法,可加密长字符串; Rijndael加密算法源代码,内附测试代码
AES加密解密算法的Java实现.doc
微信小程序 对称加密 AES ECB base64 加密解密 真实项目可用
AES加解密算法软件程序(包含全部模式128/256)AES加解密算法软件程序(包含全部模式128/256)AES加解密算法软件程序(包含全部模式128/256)
这是AES加解密算法,想要熟悉AES算法的可以在这里下载观看。
C# JAVA nodejs通用的AES加密解密算法,考虑到实际使用过程中由于客户端和服务端使用的语言不一样而又需要对数据进行加密和解密的情况,特此总结了C# JAVA nodejs通用的AES加密解密算法,供需要者参考。
1.版本:matlab2019a,不会运行可私信 2.领域:基础教程 3.内容:matlab实现AES加密和解密算法 4.适合人群:本科,硕士等教研学习使用
AES加解密算法工具,AES加解密算法工具,AES加解密算法工具,AES加解密算法工具,AES加解密算法工具
实现加密解密功能的dll,作为第三方接口提供加密解密服务。 目前只实现了AES256,有需要可以再增加其他加密解密算法。