标签:des c style class blog code
基本概念 |
---|
一个例子 |
Man is distinguished, not only by his reason,
but by this singular passion from
other animals, which is a lust of the mind, that by a perseverance
of delight
in the continued and indefatigable generation of knowledge, exceeds
the short
vehemence of any carnal pleasure.
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
转换方法 |
补零处理 |
填充 |
实现(示例) |
编码 | 字符 | 编码 | 字符 | 编码 | 字符 | 编码 | 字符 | |||
---|---|---|---|---|---|---|---|---|---|---|
0 | A |
16 | Q |
32 | g |
48 | w | |||
1 | B |
17 | R |
33 | h |
49 | x | |||
2 | C |
18 | S |
34 | i |
50 | y | |||
3 | D |
19 | T |
35 | j |
51 | z | |||
4 | E |
20 | U |
36 | k |
52 | 0 | |||
5 | F |
21 | V |
37 | l |
53 | 1 | |||
6 | G |
22 | W |
38 | m |
54 | 2 | |||
7 | H |
23 | X |
39 | n |
55 | 3 | |||
8 | I |
24 | Y |
40 | o |
56 | 4 | |||
9 | J |
25 | Z |
41 | p |
57 | 5 | |||
10 | K |
26 | a |
42 | q |
58 | 6 | |||
11 | L |
27 | b |
43 | r |
59 | 7 | |||
12 | M |
28 | c |
44 | s |
60 | 8 | |||
13 | N |
29 | d |
45 | t |
61 | 9 | |||
14 | O |
30 | e |
46 | u |
62 | + | |||
15 | P |
31 | f |
47 | v |
63 | / |
1 public String encode(String inputStr, String charset, boolean padding) 2 throws UnsupportedEncodingException { 3 String encodeStr = null; 4 5 byte[] bytes = inputStr.getBytes(charset); 6 encodeStr = encode(bytes, padding); 7 8 return encodeStr; 9 }
encode()方法的核心代码是:
1 for (int i = 0; i < groups; i++) { 2 byte_1 = bytes[3*i] & 0xFF; 3 byte_2 = bytes[3*i+1] & 0xFF; 4 byte_3 = bytes[3*i+2] & 0xFF; 5 6 group_6bit_1 = byte_1 >>> 2; 7 group_6bit_2 = (byte_1 & 0x03) << 4 | byte_2 >>> 4; 8 group_6bit_3 = (byte_2 & 0x0F) << 2 | byte_3 >>> 6; 9 group_6bit_4 = byte_3 & 0x3F; 10 11 sb.append(CHARSET[group_6bit_1]) 12 .append(CHARSET[group_6bit_2]) 13 .append(CHARSET[group_6bit_3]) 14 .append(CHARSET[group_6bit_4]); 15 }
1 if (tail == 1) { 2 byte_1 = bytes[bytes.length-1] & 0xFF; 3 4 group_6bit_1 = byte_1 >>> 2; 5 group_6bit_2 = (byte_1 & 0x03) << 4; 6 7 sb.append(CHARSET[group_6bit_1]) 8 .append(CHARSET[group_6bit_2]); 9 10 if (padding) { 11 sb.append(‘=‘).append(‘=‘); 12 } 13 } else if (tail == 2) { 14 byte_1 = bytes[bytes.length-2] & 0xFF; 15 byte_2 = bytes[bytes.length-1] & 0xFF; 16 17 group_6bit_1 = byte_1 >>> 2; 18 group_6bit_2 = (byte_1 & 0x03) << 4 | byte_2 >>> 4; 19 group_6bit_3 = (byte_2 & 0x0F) << 2; 20 21 sb.append(CHARSET[group_6bit_1]) 22 .append(CHARSET[group_6bit_2]) 23 .append(CHARSET[group_6bit_3]); 24 25 if (padding) { 26 sb.append(‘=‘); 27 } 28 }
decode过程是类似的,具体请自行查阅完整代码。
引申话题:利用Base64加密解密 |
package base64; import java.io.UnsupportedEncodingException; /** * This class provides a simple implementation of Base64 encoding and decoding. * * @author QiaoMingkui * */ public class Base64 { /* * charset */ private static final char[] CHARSET = { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘+‘, ‘/‘ }; /* * charset used to decode. */ private static final int[] DECODE_CHARSET = new int[128]; static { for (int i=0; i<64; i++) { DECODE_CHARSET[CHARSET[i]] = i; } } /** * A convenient method for encoding Java String, * it uses encode(byte[], boolean) to encode byte array. * * @param inputStr a string to be encoded. * @param charset charset name ("GBK" for example) that is used to convert inputStr into byte array. * @param padding whether using padding characters "=" * @return encoded string * @throws UnsupportedEncodingException if charset is unsupported */ public String encode(String inputStr, String charset, boolean padding) throws UnsupportedEncodingException { String encodeStr = null; byte[] bytes = inputStr.getBytes(charset); encodeStr = encode(bytes, padding); return encodeStr; } /** * Using Base64 to encode bytes. * * @param bytes byte array to be encoded * @param padding whether using padding characters "=" * @return encoded string */ public String encode(byte[] bytes, boolean padding) { // 4 6-bit groups int group_6bit_1, group_6bit_2, group_6bit_3, group_6bit_4; // bytes of a group int byte_1, byte_2, byte_3; // number of 3-byte groups int groups = bytes.length / 3; // at last, there might be 0, 1, or 2 byte(s) remained, // which needs to be encoded individually. int tail = bytes.length % 3; StringBuilder sb = new StringBuilder(groups * 4 + 4); // handle each 3-byte group for (int i = 0; i < groups; i++) { byte_1 = bytes[3*i] & 0xFF; byte_2 = bytes[3*i+1] & 0xFF; byte_3 = bytes[3*i+2] & 0xFF; group_6bit_1 = byte_1 >>> 2; group_6bit_2 = (byte_1 & 0x03) << 4 | byte_2 >>> 4; group_6bit_3 = (byte_2 & 0x0F) << 2 | byte_3 >>> 6; group_6bit_4 = byte_3 & 0x3F; sb.append(CHARSET[group_6bit_1]) .append(CHARSET[group_6bit_2]) .append(CHARSET[group_6bit_3]) .append(CHARSET[group_6bit_4]); } // handle last 1 or 2 byte(s) if (tail == 1) { byte_1 = bytes[bytes.length-1] & 0xFF; group_6bit_1 = byte_1 >>> 2; group_6bit_2 = (byte_1 & 0x03) << 4; sb.append(CHARSET[group_6bit_1]) .append(CHARSET[group_6bit_2]); if (padding) { sb.append(‘=‘).append(‘=‘); } } else if (tail == 2) { byte_1 = bytes[bytes.length-2] & 0xFF; byte_2 = bytes[bytes.length-1] & 0xFF; group_6bit_1 = byte_1 >>> 2; group_6bit_2 = (byte_1 & 0x03) << 4 | byte_2 >>> 4; group_6bit_3 = (byte_2 & 0x0F) << 2; sb.append(CHARSET[group_6bit_1]) .append(CHARSET[group_6bit_2]) .append(CHARSET[group_6bit_3]); if (padding) { sb.append(‘=‘); } } return sb.toString(); } /** * Decode a Base64 string to bytes (byte array). * * @param code Base64 string to be decoded * @return byte array */ public byte[] decode(String code) { char[] chars = code.toCharArray(); int group_6bit_1, group_6bit_2, group_6bit_3, group_6bit_4; int byte_1, byte_2, byte_3; int len = chars.length; // ignore last ‘=‘s if (chars[chars.length - 1] == ‘=‘) { len--; } if (chars[chars.length - 2] == ‘=‘) { len--; } int groups = len / 4; int tail = len % 4; // each group of characters (4 characters) will be converted into 3 bytes, // and last 2 or 3 characters will be converted into 1 or 2 byte(s). byte[] bytes = new byte[groups * 3 + (tail > 0 ? tail - 1 : 0)]; int byteIdx = 0; // decode each group for (int i=0; i<groups; i++) { group_6bit_1 = DECODE_CHARSET[chars[4*i]]; group_6bit_2 = DECODE_CHARSET[chars[4*i + 1]]; group_6bit_3 = DECODE_CHARSET[chars[4*i + 2]]; group_6bit_4 = DECODE_CHARSET[chars[4*i + 3]]; byte_1 = group_6bit_1 << 2 | group_6bit_2 >>> 4; byte_2 = (group_6bit_2 & 0x0F) << 4 | group_6bit_3 >>> 2; byte_3 = (group_6bit_3 & 0x03) << 6 | group_6bit_4; bytes[byteIdx++] = (byte) byte_1; bytes[byteIdx++] = (byte) byte_2; bytes[byteIdx++] = (byte) byte_3; } // decode last 2 or 3 characters if (tail == 2) { group_6bit_1 = DECODE_CHARSET[chars[len - 2]]; group_6bit_2 = DECODE_CHARSET[chars[len - 1]]; byte_1 = group_6bit_1 << 2 | group_6bit_2 >>> 4; bytes[byteIdx] = (byte) byte_1; } else if (tail == 3) { group_6bit_1 = DECODE_CHARSET[chars[len - 3]]; group_6bit_2 = DECODE_CHARSET[chars[len - 2]]; group_6bit_3 = DECODE_CHARSET[chars[len - 1]]; byte_1 = group_6bit_1 << 2 | group_6bit_2 >>> 4; byte_2 = (group_6bit_2 & 0x0F) << 4 | group_6bit_3 >>> 2; bytes[byteIdx++] = (byte) byte_1; bytes[byteIdx] = (byte) byte_2; } return bytes; } /** * Test. * @param args */ public static void main(String[] args) { Base64 base64 = new Base64(); String str = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."; System.out.println(str); try { String encodeStr = base64.encode(str, "GBK", false); System.out.println(encodeStr); byte[] decodeBytes = base64.decode(encodeStr); String decodeStr = new String(decodeBytes, "GBK"); System.out.println(decodeStr); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
标签:des c style class blog code
原文地址:http://www.cnblogs.com/antineutrino/p/3756106.html