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加密,解决了同步问题,和随机密钥和固定密钥,多端通信加密不一致解决办法的更多相关文章

  1. zabbix与agent端通信加密

    Zabbix版本从3.0之后,开始支持Zabbix server, Zabbix proxy, Zabbix agent, zabbix_sender and zabbix_get之间的通信加密,加密 ...

  2. golang-----golang sync.WaitGroup解决goroutine同步

    go提供了sync包和channel来解决协程同步和通讯.新手对channel通道操作起来更容易产生死锁,如果时缓冲的channel还要考虑channel放入和取出数据的速率问题. 从字面就可以理解, ...

  3. windows 系统本地做mysql 主从同步,最后面解决主从同步库名不一致,表结构一致

    原文:windows 系统本地做mysql 主从同步,最后面解决主从同步库名不一致,表结构一致 mysql主从同步的好处以及原理       之前看到很多新闻说某某的服务器奔溃,磁盘碎了,导致数据丢失 ...

  4. 配置linux系统时区---解决ntp同步完时间不准问题

    ntp配置完成后时间仍然不准有下面两种情况: ntp服务端配置完ntp服务器后,查看时间和百度的时间不一样按照下面解决 ntp客户端同步完ntp服务器后,查看客户端的时间和百度不一致,按照下面解决 1 ...

  5. 《手把手教你》系列技巧篇(七十一)-java+ selenium自动化测试-自定义类解决元素同步问题(详解教程)

    1.简介 前面宏哥介绍了几种关于时间等待的方法,也提到了,在实际自动化测试脚本开发过程,百分之90的报错是和元素因为时间不同步而发生报错.本文介绍如何新建一个自定义的类库来解决这个元素同步问题.这样, ...

  6. Android 网络交互之移动端与服务端的加密处理

    在开发项目的网络模块时,我们为了保证客户端(Client)和服务端(Server)之间的通信安全,我们会对数据进行加密. 谈到网络通信加密,我们可以说出:对称加密,非对称加密,md5单向加密,也能提到 ...

  7. 解决持久化数据太大,单个节点的硬盘无法存储的问题;解决运算量太大,单个节点的内存、CPU无法处理的问题

    需要学习的技术很多,要自学新知识也不是一件容易的事,选择一个自己比较感兴趣的会是一个比较好的开端,于是,打算学一学分布式系统. 带着问题,有目的的学习,先了解整体架构,在深入感兴趣的细节,这是我的计划 ...

  8. 7.生产者消费者 案例 (使用Lock 同步锁 方式,使用Condition完成线程之间的通信)

    /* * 生产者消费者 案例 (使用Lock 同步锁 方式,使用Condition完成线程之间的通信) * */ public class TestProductorAndConsumerForLoc ...

  9. Linux学习66 运维安全-通信加密和解密技术入门

    一.Linux Service and Security 1.OpenSSL(ssl/tls)协议 2.OpenSSH(ssh)协议 3.bind(dns) 4.web(http):httpd(apa ...

随机推荐

  1. HDU 3338 Kakuro Extension

    网络最大流 TLE了两天的题目.80次Submit才AC,发现是刘汝佳白书的Dinic代码还可以优化.....瞬间无语..... #include<cstdio> #include< ...

  2. linux脚本Shell之awk详解(二)

    三.printf的使用   print format 生成报表 %d        十进制有符号整数 %u        十进制无符号整数 %f        浮点数 %s        字符串 %c ...

  3. Git的Bug分支----临时保存现场git stash

    软件开发中,bug就像家常便饭一样,有了bug就需要修复,在Git中,由于分支是如此的强大,所以每个bug通过一个新的分支来修复,在修复后,合并分支,然后将临时分支删除. 当你接到一个修复代号为119 ...

  4. Qt多线程编程总结(一)

    http://blog.csdn.net/mznewfacer/article/details/6965799 QMutex类 一个线程可以锁定互斥量,并且在它锁定之后,其它线程就不能再锁定这个互斥量 ...

  5. redis配置密码认证

    redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 ? 1 #requirepass foobared 去掉行前的注 ...

  6. spring xsd

    http://ljhzzyx.blog.163.com/blog/static/3838031220132239471608/ spring配置文件报找不到xsd文件错误 2013-03-23 10: ...

  7. Django 分页功能

    Django 分页功能比较强大,这边是结合官网的内容写的可以参考 https://docs.djangoproject.com/en/1.9/topics/pagination/ 分页命令行练习案列 ...

  8. C++虚函数的新用法

    1.今天在segmentfault上看到了一个C++虚函数的新用法,先上代码 #include <iostream> using namespace std; class B { publ ...

  9. Hibernate的dynamic-insert和dynamic-update的使用

    Hibernate在初始化的时候,默认按照配置为表预定义insert,delete,update,select(by id)的SQL语句放在session中,其中insert,update,selec ...

  10. Objective-C相关Category的收集

    Objective-C相关Category的收集 Categories是给你得不到源码的classes增加功能的一种方法.这个页面收集一些相关的Category,并且持续更新,你可以订阅关注.作者是F ...