Encrypt.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package com.hotent.im.encrypt;
  2. public class Encrypt {
  3. private final int _randomKeyLength;
  4. private final String _publicKey;
  5. /**
  6. * 随机秘钥长度
  7. */
  8. private String _RANDOMKEYLENGTH = "16";
  9. /**
  10. * 公有秘钥
  11. */
  12. private String _PUBLICKEY = "112233";
  13. public Encrypt()
  14. {
  15. String randomKeyLength = _RANDOMKEYLENGTH;
  16. String publicKey = _PUBLICKEY;
  17. _publicKey = publicKey;
  18. System.out.println("_publicKey>>==--"+_publicKey);
  19. if("".equals(randomKeyLength)){
  20. _randomKeyLength = 32;
  21. }else{
  22. _randomKeyLength = Integer.parseInt(randomKeyLength);
  23. }
  24. }
  25. // 加密 JSON
  26. public String encodeJSON(String encodeJSON,String privateKey){
  27. //获取随机秘钥
  28. String randomKeyChars = getRandomKey(_randomKeyLength);
  29. String publicKeyChars = bufferWithString(_publicKey);
  30. String privateKeyChars = bufferWithString(privateKey);
  31. StringBuilder contentChars = new StringBuilder(bufferWithString(encodeJSON));
  32. // 第一步先用随机密钥 从前往后 编码
  33. int keyIndex = 0;
  34. for(int i = 0;i<contentChars.length();i++){
  35. int c = contentChars.charAt(i);
  36. char randomChar = randomKeyChars.charAt(keyIndex);
  37. // 如果密钥循环一遍后 从0开始重新循环
  38. if (++keyIndex >= randomKeyChars.length())
  39. keyIndex = 0;
  40. c += randomChar - 32;
  41. c = c > 126 ? c - 94 : c;
  42. contentChars.setCharAt(i, (char)c);
  43. }
  44. // 第二步先用随机密钥 从后往前 编码
  45. keyIndex = 0;
  46. for (int i = contentChars.length() - 1; i >= 0; i--) {
  47. int c = contentChars.charAt(i);
  48. char randomChar = randomKeyChars.charAt(keyIndex);
  49. // 如果密钥循环一遍后 从0开始重新循环
  50. if (++keyIndex >= randomKeyChars.length())
  51. keyIndex = 0;
  52. c += randomChar - 32;
  53. c = c > 126 ? c - 94 : c;
  54. contentChars.setCharAt(i, (char)c);
  55. }
  56. // 将随机密钥附加到内容顶部
  57. contentChars.insert(0, randomKeyChars);
  58. // 在顶部附加随机密钥长度
  59. for (int i = 0; i < 32; i += 4) {
  60. int num = (_randomKeyLength >> i) & 0xF;
  61. char numChar = (char)(num + (num > 9 ? 55 : 48));
  62. contentChars.insert(0, numChar);
  63. }
  64. // 第三步再用私有密钥 从后往前 编码
  65. keyIndex = 0;
  66. for (int i = contentChars.length() - 1; i >=0 ; i--)
  67. {
  68. int c = contentChars.charAt(i);
  69. char privateChar = privateKeyChars.charAt(keyIndex);
  70. if (++keyIndex >= privateKeyChars.length())
  71. keyIndex = 0;
  72. c += privateChar - 32;
  73. c = c > 126 ? c - 94 : c;
  74. contentChars.setCharAt(i, (char)c);
  75. }
  76. // 第四步最后用公有密钥 从前往后 编码
  77. keyIndex = 0;
  78. for (int i = 0; i < contentChars.length(); i++)
  79. {
  80. int c = contentChars.charAt(i);
  81. char publicChar = publicKeyChars.charAt(keyIndex);
  82. // 如果密钥循环一遍后 从0开始重新循环
  83. if (++keyIndex >= publicKeyChars.length())
  84. keyIndex = 0;
  85. c += publicChar - 32;
  86. c = c > 126 ? c - 94 : c;
  87. contentChars.setCharAt(i, (char)c);
  88. }
  89. return contentChars.toString();
  90. }
  91. // 解密 JSON
  92. public String decodeJSON(String targetJson,String privateKey){
  93. String publicKeyChars = bufferWithString(_publicKey); //共有秘钥
  94. String privateKeyChars = bufferWithString(privateKey); //共有秘钥
  95. StringBuilder contentChars = new StringBuilder(bufferWithString(targetJson));
  96. // 解密共有密钥 从前往后 减编码
  97. int keyIndex = 0;
  98. for (int i = 0;i<contentChars.length();i++)
  99. {
  100. int t = contentChars.charAt(i);
  101. char publicChar = publicKeyChars.charAt(keyIndex);
  102. if (++keyIndex >= publicKeyChars.length())
  103. keyIndex = 0;
  104. t -= publicChar - 32;
  105. t = t < 32 ? t + 94 : t;
  106. contentChars.setCharAt(i, (char)t);
  107. }
  108. // 解密私有密钥 从后往前 减编码
  109. keyIndex = 0;
  110. for (int i = contentChars.length()-1; i >= 0; i--) {
  111. int t = contentChars.charAt(i);
  112. char privateChar = privateKeyChars.charAt(keyIndex);
  113. // 如果密钥循环一遍后 从0开始重新循环
  114. if (++keyIndex >= privateKeyChars.length()) {
  115. keyIndex = 0;
  116. }
  117. t -= privateChar - 32;
  118. t = t < 32 ? t + 94 : t;
  119. contentChars.setCharAt(i, (char)(t & 0x7F));
  120. }
  121. // 解密随机密钥
  122. int length = 0;
  123. for (int i = 0; i < 8; i++) {
  124. int t = contentChars.charAt(i) - 48;
  125. t = t < 10 ? t : t - 7;
  126. length += t << (28 - i * 4);
  127. }
  128. System.out.println("====+++++"+contentChars);
  129. System.out.println(length);
  130. StringBuffer randomKeyChars = new StringBuffer(contentChars.substring(8, length+8));
  131. contentChars.delete(0, length+8);
  132. // 第三步先用随机密钥 从后往前 减编码
  133. keyIndex = 0;
  134. for (int i = contentChars.length() - 1; i >= 0; i--) {
  135. int c = contentChars.charAt(i);
  136. char randomChar = randomKeyChars.charAt(keyIndex);
  137. // 如果密钥循环一遍后 从0开始重新循环
  138. if (++keyIndex >= randomKeyChars.length()) {
  139. keyIndex = 0;
  140. }
  141. c -= randomChar - 32;
  142. c = c < 32 ? c + 94 : c;
  143. contentChars.setCharAt(i, (char)(c & 0x7F));
  144. }
  145. // 第四步再用随机密钥 从前往后 减编码
  146. keyIndex = 0;
  147. for (int i = 0; i < contentChars.length(); i++) {
  148. int c = contentChars.charAt(i);
  149. char randomChar = randomKeyChars.charAt(keyIndex);
  150. // 如果密钥循环一遍后 从0开始重新循环
  151. if (++keyIndex >= randomKeyChars.length()) {
  152. keyIndex = 0;
  153. }
  154. c -= randomChar - 32;
  155. c = c < 32 ? c + 94 : c;
  156. contentChars.setCharAt(i, (char)(c & 0x7F));
  157. }
  158. return contentChars.toString();
  159. }
  160. //- 给中文 编码
  161. public static String bufferWithString(String string){
  162. StringBuffer buffer = new StringBuffer();
  163. for(int i = 0;i<string.length();i++){
  164. int chr = string.codePointAt(i);
  165. char chr1 = string.charAt(i);
  166. if (chr > 127) {
  167. buffer.append("\\u"); // 代表\
  168. // 2字节 16位 (1字节8位) 转16进制后为4字节字符串
  169. // 0xA5F8 A5F8
  170. // char >> 0 15
  171. for (int j = 0; j < 16; j+=4) {
  172. int k = chr >> (12 - j);
  173. int c = (k & 0xF);
  174. buffer.append(Integer.toHexString(c).toUpperCase());
  175. }
  176. } else {
  177. buffer.append(chr1);
  178. }
  179. }
  180. return buffer.toString();
  181. }
  182. // 生成随机数
  183. public static String getRandomKey(int length){
  184. StringBuffer strb = new StringBuffer();
  185. for(int i=0;i<length;i++ ) {
  186. strb.append((int)(10*(Math.random())));
  187. }
  188. return strb.toString();
  189. }
  190. }