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 ...
随机推荐
- iosOC可变数组选择,冒泡排序
#pragma mark 可变数组的排序 NSMutableArray * array = [NSMutableArray arrayWithObjects: @"1",@&quo ...
- 使用CFile生成log文件的方法
下面实例是在退出程序点击退出按钮时,在主程序的根目录下生成一个Log记录,用来记录程序的退出时间,具体实现代码与调试代码如下: void CDebugDlg::OnClose(){ // TODO: ...
- Oracle小技术集锦
- Entity Framework技巧系列之十 - Tip 37 - 41
提示37. 怎样进行按条件包含(Conditional Include) 问题 几天前有人在StackOverflow上询问怎样进行按条件包含. 他们打算查询一些实体(比方说Movies),并且希望预 ...
- 轻松创建nodejs服务器(1):一个简单nodejs服务器例子
这篇文章主要介绍了一个简单nodejs服务器例子,本文实现了一个简单的hello world例子,并展示如何运行这个服务器,需要的朋友可以参考下 我们先来实现一个简单的例子,hello world ...
- listview必须设置数据适配器才能显示出来
listview必须设置数据适配器才能显示出来,哪怕只设置一个空的数据适配器都行: lvTabDetail.setAdapter(new NewsListAdapter()); class NewsL ...
- PHP的抽象类、接口类的区别和选择【转载】
本文转自:http://blog.csdn.net/fanteathy/article/details/7309966 区别: 1.对接口的使用是通过关键字implements.对抽象类的使用是通过关 ...
- 编译 wl18xx驱动源码
在做beagleboneblack移植的时候,wl18xx的驱动源码是自动编译的.但是移植到其他平台优越平台不一样,所以就不能自动编译 所以用其他方式编译.http://e2e.ti.com/supp ...
- oomph
http://blog.csdn.net/u011004037/article/details/45679573 这么好个功能起了这么操蛋个名字害得老子一直不知道他干啥的
- adodb.stream对象的方法/属性
Cancel 方法 使用方法如下 Object.Cancel 说明:取消执行挂起的异步 Execute 或 Open 方法的调用.Close 方法 使用方法 ...