app开发历程————服务器端生成JSON格式数据,采用Unicode编码,隐藏中文
今天,问以前的同事,他们写接口按什么编码,怎么看到有\u的一些看不懂的内容,一问,原来是信息隐藏,防止信息泄漏。
然后在网上查了Java如何把中文转换成unicode编码,转自:http://blog.csdn.net/sunmenggmail/article/details/27539023
1 package mobi.chenwei.wing.util; 2 3 public class CharacterSetToolkit { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 String s = "天津"; 11 System.out.println("Original:\t\t" + s); 12 13 s = toEncodedUnicode(s, true); 14 System.out.println("to unicode:\t\t" + s); 15 16 17 s = fromEncodedUnicode(s.toCharArray(), 0, s.length()); 18 System.out.println("from unicode:\t" + s); 19 20 21 } 22 private static final char[] hexDigit = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, 23 ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ }; 24 25 private static char toHex(int nibble) { 26 return hexDigit[(nibble & 0xF)]; 27 } 28 /** 29 * 将字符串编码成 Unicode 形式的字符串. 如 "黄" to "\u9EC4" 30 * Converts unicodes to encoded \\uxxxx and escapes 31 * special characters with a preceding slash 32 * 33 * @param theString 34 * 待转换成Unicode编码的字符串。 35 * @param escapeSpace 36 * 是否忽略空格,为true时在空格后面是否加个反斜杠。 37 * @return 返回转换后Unicode编码的字符串。 38 */ 39 public static String toEncodedUnicode(String theString, boolean escapeSpace) { 40 int len = theString.length(); 41 int bufLen = len * 2; 42 if (bufLen < 0) { 43 bufLen = Integer.MAX_VALUE; 44 } 45 StringBuffer outBuffer = new StringBuffer(bufLen); 46 47 for (int x = 0; x < len; x++) { 48 char aChar = theString.charAt(x); 49 // Handle common case first, selecting largest block that 50 // avoids the specials below 51 if ((aChar > 61) && (aChar < 127)) { 52 if (aChar == ‘\\‘) { 53 outBuffer.append(‘\\‘); 54 outBuffer.append(‘\\‘); 55 continue; 56 } 57 outBuffer.append(aChar); 58 continue; 59 } 60 61 switch (aChar) { 62 case ‘ ‘: 63 if (x == 0 || escapeSpace) outBuffer.append(‘\\‘); 64 outBuffer.append(‘ ‘); 65 break; 66 case ‘\t‘: 67 outBuffer.append(‘\\‘); 68 outBuffer.append(‘t‘); 69 break; 70 case ‘\n‘: 71 outBuffer.append(‘\\‘); 72 outBuffer.append(‘n‘); 73 break; 74 case ‘\r‘: 75 outBuffer.append(‘\\‘); 76 outBuffer.append(‘r‘); 77 break; 78 case ‘\f‘: 79 outBuffer.append(‘\\‘); 80 outBuffer.append(‘f‘); 81 break; 82 case ‘=‘: // Fall through 83 case ‘:‘: // Fall through 84 case ‘#‘: // Fall through 85 case ‘!‘: 86 outBuffer.append(‘\\‘); 87 outBuffer.append(aChar); 88 break; 89 default: 90 if ((aChar < 0x0020) || (aChar > 0x007e)) { 91 // 每个unicode有16位,每四位对应的16进制从高位保存到低位 92 outBuffer.append(‘\\‘); 93 outBuffer.append(‘u‘); 94 outBuffer.append(toHex((aChar >> 12) & 0xF)); 95 outBuffer.append(toHex((aChar >> 8) & 0xF)); 96 outBuffer.append(toHex((aChar >> 4) & 0xF)); 97 outBuffer.append(toHex(aChar & 0xF)); 98 } else { 99 outBuffer.append(aChar); 100 } 101 } 102 } 103 return outBuffer.toString(); 104 } 105 106 /** 107 * 从 Unicode 形式的字符串转换成对应的编码的特殊字符串。 如 "\u9EC4" to "黄". 108 * Converts encoded \\uxxxx to unicode chars 109 * and changes special saved chars to their original forms 110 * 111 * @param in 112 * Unicode编码的字符数组。 113 * @param off 114 * 转换的起始偏移量。 115 * @param len 116 * 转换的字符长度。 117 * @param convtBuf 118 * 转换的缓存字符数组。 119 * @return 完成转换,返回编码前的特殊字符串。 120 */ 121 public static String fromEncodedUnicode(char[] in, int off, int len) { 122 char aChar; 123 char[] out = new char[len]; // 只短不长 124 int outLen = 0; 125 int end = off + len; 126 127 while (off < end) { 128 aChar = in[off++]; 129 if (aChar == ‘\\‘) { 130 aChar = in[off++]; 131 if (aChar == ‘u‘) { 132 // Read the xxxx 133 int value = 0; 134 for (int i = 0; i < 4; i++) { 135 aChar = in[off++]; 136 switch (aChar) { 137 case ‘0‘: 138 case ‘1‘: 139 case ‘2‘: 140 case ‘3‘: 141 case ‘4‘: 142 case ‘5‘: 143 case ‘6‘: 144 case ‘7‘: 145 case ‘8‘: 146 case ‘9‘: 147 value = (value << 4) + aChar - ‘0‘; 148 break; 149 case ‘a‘: 150 case ‘b‘: 151 case ‘c‘: 152 case ‘d‘: 153 case ‘e‘: 154 case ‘f‘: 155 value = (value << 4) + 10 + aChar - ‘a‘; 156 break; 157 case ‘A‘: 158 case ‘B‘: 159 case ‘C‘: 160 case ‘D‘: 161 case ‘E‘: 162 case ‘F‘: 163 value = (value << 4) + 10 + aChar - ‘A‘; 164 break; 165 default: 166 throw new IllegalArgumentException("Malformed \\uxxxx encoding."); 167 } 168 } 169 out[outLen++] = (char) value; 170 } else { 171 if (aChar == ‘t‘) { 172 aChar = ‘\t‘; 173 } else if (aChar == ‘r‘) { 174 aChar = ‘\r‘; 175 } else if (aChar == ‘n‘) { 176 aChar = ‘\n‘; 177 } else if (aChar == ‘f‘) { 178 aChar = ‘\f‘; 179 } 180 out[outLen++] = aChar; 181 } 182 } else { 183 out[outLen++] = (char) aChar; 184 } 185 } 186 return new String(out, 0, outLen); 187 } 188 }
运行结果:
Original: 天津
to unicode: \u5929\u6D25
from unicode: 天津
这时候,我们在写接口时,希望把中文进入这个方法中进行隐藏。
人生总是有许多事情是徒劳的,但是不要因为徒劳,而不去做这件事情,挖掘自己的潜力。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。