今天在网上看到一篇不错的博客,里面讲到加密及解密的信息。其实我在公司里面用到的和这几乎一样,为了涉及保密等东西就把这篇文章分享给大家。
更多交流、更多了解:
QQ:444084929 [回钦波]
个人主面: http://www.huiqinbo.com
AES加解密算法,使用Base64做转码以及辅助加密:
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); //用密钥和一组算法参数初始化此 Cipher
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 + "毫秒");
}
}
分享到:
相关推荐
java代码实例 AES加密解密 Base64加密解密
java AES加密解密,使用 CBC 解密模式,EncryptByAes Aes加密 后 base64 再加 DecryptByAes Aes 解密,对 PKCS7 填充的数据进行反填充,对数据进行 PKCS7 填充 使用 CBC 解密模式, // aesDecrypt 解密 public ...
java实现SHA1、SHA、MD5、AES加密、AES解密、BASE64解密、BASE64加密,以及apache BASE64 jar和源码
* 编码工具类 * 1.将byte[]转为各种进制的字符串 * 2.base 64 encode * 3.base 64 decode ...结合base64实现md5加密 * 7.AES加密 * 8.AES加密为base 64 code * 9.AES解密 * 10.将base 64 code AES解密
微信小程序 对称加密 AES ECB base64 加密解密 真实项目可用
Base64转码,AES加密解密,IOS,Android,Java 后台。 本工程可以直接使用,而且与IOS,Android,JAVA 已调通。 主要用于客户端进行通信,数据之间地安全性。对数据进行加密和解密。 适用于银行等较为保密的项目中。 ...
' 功能: AES加密解密 password<==>hwWpiegaWDeE8kTlb/5BQg== , 8705a989e81a583784f244e56ffe4142 ' [Str] 预备转换的byte ' [StrKey] 16位的密钥 ' [StrIv] 16位的偏移 ' [Encrypt] 加密(缺省值)还是解密 ' ...
C#加密 解密对接java 的加密AES/CBC/PKCS7Padding 转16进制 C#加密解密中的详细整理
实现了AES/ECB/PKCS5Padding、AES/CBC/PKCS5Padding 密钥长度128/192/256bit,密钥0填充。是标准的AES算法,支持在线AES加解密网站...需要注意的是仅在XE及以上版本支持Base64返回结果,Delphi7的base64不能直接支持。
加密工具类 包含md5加密 base加密解密 md5加密 aes加密解密 urlencoude
本地图片AES加密解密,利用AES加密的方式将图片保存于本地,其他App获取该图片无法显示,信息安全可靠加密速度高且十分有效,最好是保存的图片源文件在500KB以下为最佳!
Delphi7AES加密解密与JAVA互转(默认128位+ECB+PKCS5Padding+先BASE64再HEX)
不用cryptoJs的aes加密解密,代码包含Base64.js,pad-zeropadding-min.js,aes.js
非常实用的Base64加密,解密角本。基于UTF8,支持中文加解密。
SqlSerVer 进行Base64 加密解密,支持中文,数字,字母
加解密工具集 对字符串做BASE64加密 解密 MD5加密 解密 DES加密 DES解密 AES加密 AES解密
AES128加密,当前测试实现不超过64字节明文的加解密,Base64格式输入输出。 加密方式:ECB 0补齐 密钥16B 如需实现192/256加密,需修改内部函数。 测试代码,仅供参考。
VB6.0 RSA/AES/Md5/Base64加密 解密 微信小程序 javascript 通用 仅demo实例!可先测试使用!
包括了Delphi AES DES MD5 RSA BASE64 2DES 3DES Blowfish CRC32-Static SHA等 常用的加密算法用法实例,比较不错的Delphi加密实例代码了,浅显易懂而且实用的常见加密算法实例,包括字符串和文件加密解密等。
var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); var decryptedStr = ...