AES加密,解决了同步问题,和随机密钥和固定密钥,多端通信加密不一致解决办法
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)));
} }
AES加密,解决了同步问题,和随机密钥和固定密钥,多端通信加密不一致解决办法的更多相关文章
- zabbix与agent端通信加密
Zabbix版本从3.0之后,开始支持Zabbix server, Zabbix proxy, Zabbix agent, zabbix_sender and zabbix_get之间的通信加密,加密 ...
- golang-----golang sync.WaitGroup解决goroutine同步
go提供了sync包和channel来解决协程同步和通讯.新手对channel通道操作起来更容易产生死锁,如果时缓冲的channel还要考虑channel放入和取出数据的速率问题. 从字面就可以理解, ...
- windows 系统本地做mysql 主从同步,最后面解决主从同步库名不一致,表结构一致
原文:windows 系统本地做mysql 主从同步,最后面解决主从同步库名不一致,表结构一致 mysql主从同步的好处以及原理 之前看到很多新闻说某某的服务器奔溃,磁盘碎了,导致数据丢失 ...
- 配置linux系统时区---解决ntp同步完时间不准问题
ntp配置完成后时间仍然不准有下面两种情况: ntp服务端配置完ntp服务器后,查看时间和百度的时间不一样按照下面解决 ntp客户端同步完ntp服务器后,查看客户端的时间和百度不一致,按照下面解决 1 ...
- 《手把手教你》系列技巧篇(七十一)-java+ selenium自动化测试-自定义类解决元素同步问题(详解教程)
1.简介 前面宏哥介绍了几种关于时间等待的方法,也提到了,在实际自动化测试脚本开发过程,百分之90的报错是和元素因为时间不同步而发生报错.本文介绍如何新建一个自定义的类库来解决这个元素同步问题.这样, ...
- Android 网络交互之移动端与服务端的加密处理
在开发项目的网络模块时,我们为了保证客户端(Client)和服务端(Server)之间的通信安全,我们会对数据进行加密. 谈到网络通信加密,我们可以说出:对称加密,非对称加密,md5单向加密,也能提到 ...
- 解决持久化数据太大,单个节点的硬盘无法存储的问题;解决运算量太大,单个节点的内存、CPU无法处理的问题
需要学习的技术很多,要自学新知识也不是一件容易的事,选择一个自己比较感兴趣的会是一个比较好的开端,于是,打算学一学分布式系统. 带着问题,有目的的学习,先了解整体架构,在深入感兴趣的细节,这是我的计划 ...
- 7.生产者消费者 案例 (使用Lock 同步锁 方式,使用Condition完成线程之间的通信)
/* * 生产者消费者 案例 (使用Lock 同步锁 方式,使用Condition完成线程之间的通信) * */ public class TestProductorAndConsumerForLoc ...
- Linux学习66 运维安全-通信加密和解密技术入门
一.Linux Service and Security 1.OpenSSL(ssl/tls)协议 2.OpenSSH(ssh)协议 3.bind(dns) 4.web(http):httpd(apa ...
随机推荐
- C相关的图书(链接不可用)
Visual C++视频技术方案宝典.pdf: http://www.t00y.com/file/17628500 Windows 图形编程.pdf: http://www.t00y.com/file ...
- 第九节,基本条件语句if
条件语句 如果我们希望有效的响应用户的输入,代码就需要具有判断能力.能够让程序进行判断的结构成为条件,条件判断语句返回的是布尔值真或假,真就执行一条线路,假就执行另外一条线路 注意if判断如果怎样,否 ...
- servlet第1讲初识
- 两层嵌套的JSON包的解法
由于后台的变态,有时候会出现两层甚至多层嵌套的JSON包. 一层的很好解,而且我看过一些比较大的网站新闻接口返回的JSON包也仅仅是一层的. 比如下图所示一层的包 代码也很简单直观 dict = [d ...
- linux安装tomcat, jdk出现的问题
1)卸载系统自带的jdk版本: 查看自带的jdk: #rpm -qa|grep gcj 可能看到如下类似的信息: libgcj-4.1.2-44.el5 jav ...
- js获取页面的来源页
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...
- Java学习笔记之Scanner报错java.util.NoSuchElementException
转载自:IT学习者-螃蟹 一个方法A使用了Scanner,在里面把它关闭了.然后又在方法B里调用方法A之后就不能再用Scanner了Scanner in = new Scanner(System.in ...
- JavaScript的第一次小结
一. JavaScript是一种的脚本语言:特点是:具有解释性,基于对象,事件驱动,安全性和跨平台等特点 对于这几种特点有必要说明一下 解释性:就是JavaScripte本身就是一种解释性语言 基于对 ...
- 关于jQuery的条件判断问题
遇到的第一个问题,点击第一行用toggle实现背景的变换,需要在toggle函数里面判断,当下面显示的时候是一个状态,下面不显示的时候是另一个状态,我用了 if($(".check-box& ...
- 优化eclipse
1.取消自动validation windows–>perferences–>validation 除开Manual下面的复选框全部选中之外,其他全部不选 如需验证,在要验证的文件上,单击 ...