1、密钥随机生成。

  1. import java.security.InvalidAlgorithmParameterException;
  2. import java.security.InvalidKeyException;
  3. import java.security.NoSuchAlgorithmException;
  4. import java.security.SecureRandom;
  5. import java.security.spec.AlgorithmParameterSpec;
  6.  
  7. import javax.crypto.BadPaddingException;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.IllegalBlockSizeException;
  10. import javax.crypto.KeyGenerator;
  11. import javax.crypto.NoSuchPaddingException;
  12. import javax.crypto.SecretKey;
  13. import javax.crypto.spec.IvParameterSpec;
  14.  
  15. /**
  16. * @ClassName: AESUtil
  17. * @Description: 对cookie进行加密解密
  18. * @author
  19. * @date 2015-9-23 上午9:07:18
  20. *
  21. */
  22. public class AesUtils {
  23.  
  24. public static final String logalrithm = "AES/CBC/PKCS5Padding";
  25.  
  26. private static byte[] keyValue = new byte[] {
  27. 22,25,-35,-45,25,98,-55,-45,10,20,-45,25,
  28. 26,-95,25,-65,-11,-99,85,45,-62,10,-0,11,
  29. -35,48,-98,65,-32,14,-78,25,36,-56,-45,-45,
  30. 12,15,-35,-75,15,-14,62,-25,33,-45,55,68,-88
  31. };
  32. private static byte[] iv = new byte[] {
  33. -12,35,-25,65,45,-87,95,-22,-15,45,55,-66,32,5-4,84,55
  34. };
  35. private static SecretKey key;
  36. private static AlgorithmParameterSpec paramSpec;
  37.  
  38. static{
  39. KeyGenerator kgen;
  40. try {
  41. kgen = KeyGenerator.getInstance("AES");
  42. kgen.init(128, new SecureRandom(keyValue));
  43. key = kgen.generateKey();
  44. paramSpec = new IvParameterSpec(iv);
  45. } catch (NoSuchAlgorithmException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. /**
  51. * @Title: encrypt
  52. * @Description: 加密,使用指定数据源生成密钥,使用用户数据作为算法参数进行AES加密
  53. * @return String 返回类型
  54. * @param msg 加密的数据
  55. * @return
  56. * @date 2015-9-23 上午9:09:20
  57. * @throws
  58. */
  59. public static String encrypt(String msg) {
  60. String str = "";
  61. try {
  62. Cipher ecipher = Cipher.getInstance(logalrithm);
  63. ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  64. str = asHex(ecipher.doFinal(msg.getBytes()));
  65. } catch (BadPaddingException e) {
  66. e.printStackTrace();
  67. } catch (InvalidKeyException e) {
  68. e.printStackTrace();
  69. } catch (InvalidAlgorithmParameterException e) {
  70. e.printStackTrace();
  71. } catch (IllegalBlockSizeException e) {
  72. e.printStackTrace();
  73. } catch (NoSuchAlgorithmException e) {
  74. e.printStackTrace();
  75. } catch (NoSuchPaddingException e) {
  76. e.printStackTrace();
  77. }
  78. return str;
  79. }
  80.  
  81. /**
  82. * @Title: decrypt
  83. * @Description: 解密,对生成的16进制的字符串进行解密
  84. * @return String 返回类型
  85. * @author WUWeidong
  86. * @param value
  87. * @return
  88. * @date 2015-9-23 上午9:10:01
  89. * @throws
  90. */
  91. public static String decrypt(String value) {
  92. try {
  93. Cipher ecipher = Cipher.getInstance(logalrithm);
  94. ecipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  95. return new String(ecipher.doFinal(asBin(value)));
  96. } catch (BadPaddingException e) {
  97. e.printStackTrace();
  98. } catch (InvalidKeyException e) {
  99. e.printStackTrace();
  100. } catch (InvalidAlgorithmParameterException e) {
  101. e.printStackTrace();
  102. } catch (IllegalBlockSizeException e) {
  103. e.printStackTrace();
  104. } catch (NoSuchAlgorithmException e) {
  105. e.printStackTrace();
  106. } catch (NoSuchPaddingException e) {
  107. e.printStackTrace();
  108. }
  109. return "";
  110. }
  111.  
  112. /**
  113. * @Title: asHex
  114. * @Description: 将字节数组转换成16进制字符串
  115. * @return String 返回类型
  116. * @param buf
  117. * @return
  118. * @date 2015-9-23 上午9:10:25
  119. * @throws
  120. */
  121. private static String asHex(byte[] buf) {
  122. StringBuffer strbuf = new StringBuffer(buf.length * 2);
  123. int i;
  124. for (i = 0; i < buf.length; i++) {
  125. if (((int) buf[i] & 0xff) < 0x10){
  126. strbuf.append("0");
  127. }
  128. strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
  129. }
  130. return strbuf.toString();
  131. }
  132.  
  133. /**
  134. * @Title: asBin
  135. * @Description: 将16进制字符串转换成字节数组
  136. * @return byte[] 返回类型
  137. * @author WUWeidong
  138. * @param src
  139. * @return
  140. * @date 2015-9-23 上午9:10:52
  141. * @throws
  142. */
  143. private static byte[] asBin(String src) {
  144. if (src.length() < 1){
  145. return null;
  146. }
  147. byte[] encrypted = new byte[src.length() / 2];
  148. for (int i = 0; i < src.length() / 2; i++) {
  149. int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
  150. int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
  151. encrypted[i] = (byte) (high * 16 + low);
  152. }
  153. return encrypted;
  154. }
  155.  
  156. public static void main(String[] args) {
  157. String msg="897807300";
  158. System.out.println(encrypt(msg));
  159. System.out.println(decrypt(encrypt(msg)));
  160. }
  161. }

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

  1. package com.cmcc.omp.securityplatform.base;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.InvalidAlgorithmParameterException;
  4. import java.security.InvalidKeyException;
  5. import java.security.Key;
  6. import java.security.NoSuchAlgorithmException;
  7.  
  8. import javax.crypto.BadPaddingException;
  9. import javax.crypto.Cipher;
  10. import javax.crypto.IllegalBlockSizeException;
  11. import javax.crypto.NoSuchPaddingException;
  12. import javax.crypto.spec.IvParameterSpec;
  13. import javax.crypto.spec.SecretKeySpec;
  14.  
  15. /**
  16. * @ClassName: AESUtil
  17. * @Description: 对cookie进行加密解密
  18. * @date 2015-9-23 上午9:07:18
  19. *
  20. */
  21. public class AesUtils {
  22.  
  23. public static final String logalrithm = "AES/CBC/PKCS5Padding";
  24. public static final String bm = "utf-8";
  25. private static byte[] keyValue = new byte[] {
  26. 22,-35,-45,25,98,-55,-45,10,35,-45,25,26,-95,25,-35,48
  27. };
  28. private static byte[] iv = new byte[] {
  29. -12,35,-25,65,45,-87,95,-22,-15,45,55,-66,32,5-4,84,55
  30. };
  31.  
  32. private static Key keySpec;
  33. private static IvParameterSpec ivSpec;
  34.  
  35. static{
  36. keySpec = new SecretKeySpec(keyValue, "AES");
  37. ivSpec = new IvParameterSpec(iv);
  38. }
  39.  
  40. /**
  41. * @Title: encrypt
  42. * @Description: 加密,使用指定数据源生成密钥,使用用户数据作为算法参数进行AES加密
  43. * @return String 返回类型
  44. * @param msg 加密的数据
  45. * @return
  46. * @date 2015-9-23 上午9:09:20
  47. * @throws
  48. */
  49. public static String encrypt(String msg) {
  50. byte[] encryptedData = null;
  51. try {
  52. Cipher ecipher = Cipher.getInstance(logalrithm);
  53. ecipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
  54. encryptedData = ecipher.doFinal(msg.getBytes(bm));
  55. } catch (IllegalBlockSizeException | BadPaddingException e) {
  56. e.printStackTrace();
  57. } catch (NoSuchAlgorithmException e) {
  58. e.printStackTrace();
  59. } catch (NoSuchPaddingException e) {
  60. e.printStackTrace();
  61. } catch (InvalidKeyException e) {
  62. e.printStackTrace();
  63. } catch (InvalidAlgorithmParameterException e) {
  64. e.printStackTrace();
  65. } catch (UnsupportedEncodingException e) {
  66. e.printStackTrace();
  67. }
  68. return asHex(encryptedData);
  69. }
  70.  
  71. /**
  72. * @Title: decrypt
  73. * @Description: 解密,对生成的16进制的字符串进行解密
  74. * @return String 返回类型
  75. * @param value
  76. * @return
  77. * @date 2015-9-23 上午9:10:01
  78. * @throws
  79. */
  80. public static String decrypt(String value) {
  81. try {
  82. Cipher ecipher = Cipher.getInstance(logalrithm);
  83. ecipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
  84. return new String(ecipher.doFinal(asBin(value)));
  85. } catch (BadPaddingException e) {
  86. System.out.println("解密错误:"+value);
  87. e.printStackTrace();
  88. } catch (IllegalBlockSizeException e) {
  89. System.out.println("解密错误:"+value);
  90. e.printStackTrace();
  91. } catch (NoSuchAlgorithmException e) {
  92. e.printStackTrace();
  93. } catch (NoSuchPaddingException e) {
  94. e.printStackTrace();
  95. } catch (InvalidKeyException e) {
  96. e.printStackTrace();
  97. } catch (InvalidAlgorithmParameterException e) {
  98. e.printStackTrace();
  99. }
  100. return "";
  101. }
  102.  
  103. /**
  104. * @Title: asHex
  105. * @Description: 将字节数组转换成16进制字符串
  106. * @return String 返回类型
  107. * @param buf
  108. * @return
  109. * @date 2015-9-23 上午9:10:25
  110. * @throws
  111. */
  112. private static String asHex(byte[] buf) {
  113. StringBuffer strbuf = new StringBuffer(buf.length * 2);
  114. int i;
  115. for (i = 0; i < buf.length; i++) {
  116. if (((int) buf[i] & 0xff) < 0x10){
  117. strbuf.append("0");
  118. }
  119. strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
  120. }
  121. return strbuf.toString();
  122. }
  123.  
  124. /**
  125. * @Title: asBin
  126. * @Description: 将16进制字符串转换成字节数组
  127. * @return byte[] 返回类型
  128. * @param src
  129. * @return
  130. * @date 2015-9-23 上午9:10:52
  131. * @throws
  132. */
  133. private static byte[] asBin(String src) {
  134. if (src.length() < 1){
  135. return null;
  136. }
  137. byte[] encrypted = new byte[src.length() / 2];
  138. for (int i = 0; i < src.length() / 2; i++) {
  139. int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
  140. int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
  141. encrypted[i] = (byte) (high * 16 + low);
  142. }
  143. return encrypted;
  144. }
  145.  
  146. public static void main(String[] args) {
  147. String userid = "897807300@qq.com";
  148. String token = "8aa8690f65f080aee595d8781e7044a7eacda7a86520786db0838136554920b6";
  149. System.out.println(encrypt(userid));
  150. System.out.println(decrypt(encrypt(userid)));
  151. }
  152.  
  153. }

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. 编译使用luasocket

    编译lua5.1: 因为luasocket使用的是lua5.1,所以先下载lua5.1,编译,并把头文件和dll放在xxx/lua5.1/include和xxx/lua5.1/lib 编译luasoc ...

  2. c语言-三字符组

    C 源程序源字符集在 7 位 ASCII 字符集中包含,但设置为 ISO 646-1983 固定的代码的超集. 三字符序列允许 C 程序编写使用 " 仅 ISO (国际标准组织的固定的代码. ...

  3. 其他应用和技巧-eval()函数大行其道

    ---------------------------------- <script type="text/javascript">                   ...

  4. openwrt ramips随记

    ar71xx / brcm47xx / brcm63xx / ramips是指cpu的系列,ramips是指ralink系列的

  5. Git 暂存区的概念

    工作区:我们在电脑里面能看到的目录,也就是我们用git init 命令初始化的那个目录.里面包含要添加文件和需要提交的文件,在这个目录下的文件,修改和变更,我们的git都能感知的到. 版本库:工作区有 ...

  6. MySQL数据库分区修改【原创】

    之前有个表分区添加时s201607添加成s201617,所以在查询7月份数据时报错 错误的 alter table statistics_ticket add partition (partition ...

  7. EntityFramwork所有 SSDL 项目都必须以同一提供程序为目标。ProviderManifestToken“2008”不同于以前遇到的“2005”

    再用spring+mvc+EF搭建框架时,出现这个问题,网上没有找到类似的问题,删除实体重建后莫名其妙的好了,2008指的是连得数据库实例是2008版本的,2005指的是2005版本,出现问题的原因是 ...

  8. 快速批量插入sqlserver方法之我见

    ---------------------------------------------------------------------------------------------------- ...

  9. Qt 学习之路 2(84):Repeater

    前面的章节我 们介绍过模型视图.这是一种数据和显示相分离的技术,在 Qt 中有着非常重要的地位.在 QtQuick 中,数据和显示的分离同样也是利用这种"模型-视图"技术实现的.对 ...

  10. UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-15: ordinal not in range(128)

    python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报类似这样的错误. UnicodeEncodeError: 'ascii' codec can't ...