1、密钥随机生成。

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec; /**
* @ClassName: AESUtil
* @Description: 对cookie进行加密解密
* @author
* @date 2015-9-23 上午9:07:18
*
*/
public class AesUtils { public static final String logalrithm = "AES/CBC/PKCS5Padding"; private static byte[] keyValue = new byte[] {
22,25,-35,-45,25,98,-55,-45,10,20,-45,25,
26,-95,25,-65,-11,-99,85,45,-62,10,-0,11,
-35,48,-98,65,-32,14,-78,25,36,-56,-45,-45,
12,15,-35,-75,15,-14,62,-25,33,-45,55,68,-88
};
private static byte[] iv = new byte[] {
-12,35,-25,65,45,-87,95,-22,-15,45,55,-66,32,5-4,84,55
};
private static SecretKey key;
private static AlgorithmParameterSpec paramSpec; static{
KeyGenerator kgen;
try {
kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(keyValue));
key = kgen.generateKey();
paramSpec = new IvParameterSpec(iv);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
} /**
* @Title: encrypt
* @Description: 加密,使用指定数据源生成密钥,使用用户数据作为算法参数进行AES加密
* @return String 返回类型
* @param msg 加密的数据
* @return
* @date 2015-9-23 上午9:09:20
* @throws
*/
public static String encrypt(String msg) {
String str = "";
try {
Cipher ecipher = Cipher.getInstance(logalrithm);
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
str = asHex(ecipher.doFinal(msg.getBytes()));
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
return str;
} /**
* @Title: decrypt
* @Description: 解密,对生成的16进制的字符串进行解密
* @return String 返回类型
* @author WUWeidong
* @param value
* @return
* @date 2015-9-23 上午9:10:01
* @throws
*/
public static String decrypt(String value) {
try {
Cipher ecipher = Cipher.getInstance(logalrithm);
ecipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
return new String(ecipher.doFinal(asBin(value)));
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
return "";
} /**
* @Title: asHex
* @Description: 将字节数组转换成16进制字符串
* @return String 返回类型
* @param buf
* @return
* @date 2015-9-23 上午9:10:25
* @throws
*/
private static String asHex(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10){
strbuf.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
} /**
* @Title: asBin
* @Description: 将16进制字符串转换成字节数组
* @return byte[] 返回类型
* @author WUWeidong
* @param src
* @return
* @date 2015-9-23 上午9:10:52
* @throws
*/
private static byte[] asBin(String src) {
if (src.length() < 1){
return null;
}
byte[] encrypted = new byte[src.length() / 2];
for (int i = 0; i < src.length() / 2; i++) {
int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
encrypted[i] = (byte) (high * 16 + low);
}
return encrypted;
} public static void main(String[] args) {
String msg="897807300";
System.out.println(encrypt(msg));
System.out.println(decrypt(encrypt(msg)));
}
}

2、密钥固定,加密通信的时候可以使用

package com.cmcc.omp.securityplatform.base;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; /**
* @ClassName: AESUtil
* @Description: 对cookie进行加密解密
* @date 2015-9-23 上午9:07:18
*
*/
public class AesUtils { public static final String logalrithm = "AES/CBC/PKCS5Padding";
public static final String bm = "utf-8";
private static byte[] keyValue = new byte[] {
22,-35,-45,25,98,-55,-45,10,35,-45,25,26,-95,25,-35,48
};
private static byte[] iv = new byte[] {
-12,35,-25,65,45,-87,95,-22,-15,45,55,-66,32,5-4,84,55
}; private static Key keySpec;
private static IvParameterSpec ivSpec; static{
keySpec = new SecretKeySpec(keyValue, "AES");
ivSpec = new IvParameterSpec(iv);
} /**
* @Title: encrypt
* @Description: 加密,使用指定数据源生成密钥,使用用户数据作为算法参数进行AES加密
* @return String 返回类型
* @param msg 加密的数据
* @return
* @date 2015-9-23 上午9:09:20
* @throws
*/
public static String encrypt(String msg) {
byte[] encryptedData = null;
try {
Cipher ecipher = Cipher.getInstance(logalrithm);
ecipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
encryptedData = ecipher.doFinal(msg.getBytes(bm));
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return asHex(encryptedData);
} /**
* @Title: decrypt
* @Description: 解密,对生成的16进制的字符串进行解密
* @return String 返回类型
* @param value
* @return
* @date 2015-9-23 上午9:10:01
* @throws
*/
public static String decrypt(String value) {
try {
Cipher ecipher = Cipher.getInstance(logalrithm);
ecipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
return new String(ecipher.doFinal(asBin(value)));
} catch (BadPaddingException e) {
System.out.println("解密错误:"+value);
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
System.out.println("解密错误:"+value);
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return "";
} /**
* @Title: asHex
* @Description: 将字节数组转换成16进制字符串
* @return String 返回类型
* @param buf
* @return
* @date 2015-9-23 上午9:10:25
* @throws
*/
private static String asHex(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10){
strbuf.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
} /**
* @Title: asBin
* @Description: 将16进制字符串转换成字节数组
* @return byte[] 返回类型
* @param src
* @return
* @date 2015-9-23 上午9:10:52
* @throws
*/
private static byte[] asBin(String src) {
if (src.length() < 1){
return null;
}
byte[] encrypted = new byte[src.length() / 2];
for (int i = 0; i < src.length() / 2; i++) {
int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
encrypted[i] = (byte) (high * 16 + low);
}
return encrypted;
} public static void main(String[] args) {
String userid = "897807300@qq.com";
String token = "8aa8690f65f080aee595d8781e7044a7eacda7a86520786db0838136554920b6";
System.out.println(encrypt(userid));
System.out.println(decrypt(encrypt(userid)));
} }

最新文章

  1. svn常用命令
  2. Oracle 更新表(另一张表)
  3. cutpFTP设置步骤
  4. Node.js(1)-helloworld
  5. Socket Programming in C#--Conclusion
  6. Python学习(21)python操作mysql数据库_操作
  7. EF5.X Code First表关联与延迟加载
  8. php中的NOTICE 的错误解决方法
  9. 《ACM国际大学生程序设计竞赛题解Ⅰ》——模拟题
  10. Yii系列总结:yii 标签用法
  11. mysql 结合keepalived测试
  12. 新书《Linux就是这个范儿》预售
  13. VC2010工程依赖不再自动链接
  14. .NET CORE学习笔记系列(2)——依赖注入【2】基于IoC的设计模式
  15. xcode升级至9.0之后,新建xib报错: Safe Area Layout Guide Before IOS 9.0
  16. #科委外文文献发现系统——导出word模板1.0
  17. Ajax的爬取心得
  18. VMware&#160;Linux下拖拽补丁vmtools的安装和卸载
  19. ssl,proxy;部分http部分https;80,443,8080;nginx+tomcat;
  20. Java虚拟机的内存组成

热门文章

  1. js循环POST提交添加辅助单位
  2. Lowest Bit
  3. python常用操作
  4. Why isn&#39;t sizeof for a struct equal to the sum of sizeof of each member?
  5. Eclipse/MyEclipse中使用复制粘贴功能卡的解决办法
  6. shell之路【第一篇】shell简介与入门
  7. 批量删除实现js+springmvc
  8. Python 线程,进程
  9. 2.1 sikuli 中编程运行
  10. Haproxy的安装和配置示例