package com.hotent.im.encrypt; public class Encrypt { private final int _randomKeyLength; private final String _publicKey; /** * 随机秘钥长度 */ private String _RANDOMKEYLENGTH = "16"; /** * 公有秘钥 */ private String _PUBLICKEY = "112233"; public Encrypt() { String randomKeyLength = _RANDOMKEYLENGTH; String publicKey = _PUBLICKEY; _publicKey = publicKey; System.out.println("_publicKey>>==--"+_publicKey); if("".equals(randomKeyLength)){ _randomKeyLength = 32; }else{ _randomKeyLength = Integer.parseInt(randomKeyLength); } } // 加密 JSON public String encodeJSON(String encodeJSON,String privateKey){ //获取随机秘钥 String randomKeyChars = getRandomKey(_randomKeyLength); String publicKeyChars = bufferWithString(_publicKey); String privateKeyChars = bufferWithString(privateKey); StringBuilder contentChars = new StringBuilder(bufferWithString(encodeJSON)); // 第一步先用随机密钥 从前往后 编码 int keyIndex = 0; for(int i = 0;i= randomKeyChars.length()) keyIndex = 0; c += randomChar - 32; c = c > 126 ? c - 94 : c; contentChars.setCharAt(i, (char)c); } // 第二步先用随机密钥 从后往前 编码 keyIndex = 0; for (int i = contentChars.length() - 1; i >= 0; i--) { int c = contentChars.charAt(i); char randomChar = randomKeyChars.charAt(keyIndex); // 如果密钥循环一遍后 从0开始重新循环 if (++keyIndex >= randomKeyChars.length()) keyIndex = 0; c += randomChar - 32; c = c > 126 ? c - 94 : c; contentChars.setCharAt(i, (char)c); } // 将随机密钥附加到内容顶部 contentChars.insert(0, randomKeyChars); // 在顶部附加随机密钥长度 for (int i = 0; i < 32; i += 4) { int num = (_randomKeyLength >> i) & 0xF; char numChar = (char)(num + (num > 9 ? 55 : 48)); contentChars.insert(0, numChar); } // 第三步再用私有密钥 从后往前 编码 keyIndex = 0; for (int i = contentChars.length() - 1; i >=0 ; i--) { int c = contentChars.charAt(i); char privateChar = privateKeyChars.charAt(keyIndex); if (++keyIndex >= privateKeyChars.length()) keyIndex = 0; c += privateChar - 32; c = c > 126 ? c - 94 : c; contentChars.setCharAt(i, (char)c); } // 第四步最后用公有密钥 从前往后 编码 keyIndex = 0; for (int i = 0; i < contentChars.length(); i++) { int c = contentChars.charAt(i); char publicChar = publicKeyChars.charAt(keyIndex); // 如果密钥循环一遍后 从0开始重新循环 if (++keyIndex >= publicKeyChars.length()) keyIndex = 0; c += publicChar - 32; c = c > 126 ? c - 94 : c; contentChars.setCharAt(i, (char)c); } return contentChars.toString(); } // 解密 JSON public String decodeJSON(String targetJson,String privateKey){ String publicKeyChars = bufferWithString(_publicKey); //共有秘钥 String privateKeyChars = bufferWithString(privateKey); //共有秘钥 StringBuilder contentChars = new StringBuilder(bufferWithString(targetJson)); // 解密共有密钥 从前往后 减编码 int keyIndex = 0; for (int i = 0;i= publicKeyChars.length()) keyIndex = 0; t -= publicChar - 32; t = t < 32 ? t + 94 : t; contentChars.setCharAt(i, (char)t); } // 解密私有密钥 从后往前 减编码 keyIndex = 0; for (int i = contentChars.length()-1; i >= 0; i--) { int t = contentChars.charAt(i); char privateChar = privateKeyChars.charAt(keyIndex); // 如果密钥循环一遍后 从0开始重新循环 if (++keyIndex >= privateKeyChars.length()) { keyIndex = 0; } t -= privateChar - 32; t = t < 32 ? t + 94 : t; contentChars.setCharAt(i, (char)(t & 0x7F)); } // 解密随机密钥 int length = 0; for (int i = 0; i < 8; i++) { int t = contentChars.charAt(i) - 48; t = t < 10 ? t : t - 7; length += t << (28 - i * 4); } System.out.println("====+++++"+contentChars); System.out.println(length); StringBuffer randomKeyChars = new StringBuffer(contentChars.substring(8, length+8)); contentChars.delete(0, length+8); // 第三步先用随机密钥 从后往前 减编码 keyIndex = 0; for (int i = contentChars.length() - 1; i >= 0; i--) { int c = contentChars.charAt(i); char randomChar = randomKeyChars.charAt(keyIndex); // 如果密钥循环一遍后 从0开始重新循环 if (++keyIndex >= randomKeyChars.length()) { keyIndex = 0; } c -= randomChar - 32; c = c < 32 ? c + 94 : c; contentChars.setCharAt(i, (char)(c & 0x7F)); } // 第四步再用随机密钥 从前往后 减编码 keyIndex = 0; for (int i = 0; i < contentChars.length(); i++) { int c = contentChars.charAt(i); char randomChar = randomKeyChars.charAt(keyIndex); // 如果密钥循环一遍后 从0开始重新循环 if (++keyIndex >= randomKeyChars.length()) { keyIndex = 0; } c -= randomChar - 32; c = c < 32 ? c + 94 : c; contentChars.setCharAt(i, (char)(c & 0x7F)); } return contentChars.toString(); } //- 给中文 编码 public static String bufferWithString(String string){ StringBuffer buffer = new StringBuffer(); for(int i = 0;i 127) { buffer.append("\\u"); // 代表\ // 2字节 16位 (1字节8位) 转16进制后为4字节字符串 // 0xA5F8 A5F8 // char >> 0 15 for (int j = 0; j < 16; j+=4) { int k = chr >> (12 - j); int c = (k & 0xF); buffer.append(Integer.toHexString(c).toUpperCase()); } } else { buffer.append(chr1); } } return buffer.toString(); } // 生成随机数 public static String getRandomKey(int length){ StringBuffer strb = new StringBuffer(); for(int i=0;i