123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- 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<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 > 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<contentChars.length();i++)
- {
-
- int t = contentChars.charAt(i);
- char publicChar = publicKeyChars.charAt(keyIndex);
- if (++keyIndex >= 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<string.length();i++){
- int chr = string.codePointAt(i);
- char chr1 = string.charAt(i);
-
- if (chr > 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<length;i++ ) {
- strb.append((int)(10*(Math.random())));
- }
- return strb.toString();
- }
- }
|