`
阅读更多

一、Base64简介:

Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式。 如果剩下的字符不足3个字节,则用0填充,输出字符使用‘=’,因此编码后输出的文本末尾可能会出现1或2个‘=’。

为了保证所输出的编码位可读字符,Base64制定了一个编码表,以便进行统一转换。编码表的大小为2^6=64,这也是Base64名称的由来。

码值

字符

码值

字符

码值

字符

码值

字符

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

/

参考自:http://tool.chinaz.com/Tools/Base64.aspx

 

二、Java版本:

import java.io.UnsupportedEncodingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64 {
	// 加密
	public static String encode(String str) {
		byte[] b = null;
		String s = null;
		try {
			b = str.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (b != null) {
			s = new BASE64Encoder().encode(b);
			//据RFC 822规定,每76个字符,还需要加上一个回车换行去掉换行符
			s = s.replaceAll("[\\s*\t\n\r]", "");  
		}
		return s;
	}

	// 解密
	public static String decode(String s) {
		byte[] b = null;
		String result = null;
		if (s != null) {
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				b = decoder.decodeBuffer(s);
				result = new String(b, "utf-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	public static void main(String[] args){
		String s = "你好,The Word!";
		//System.out.println(s.length());
		String enStr = encode(s);
		String deStr = decode(enStr);
		System.out.println("原始数据:"+s+"\n加密数据:"+enStr+"\n解密数据:"+deStr);
	}
} 

三、JS版本:

先引入base64.js:

var Base64 = {  
    // 转码表  
table : [  
        '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', '+', '/' 
],  
UTF16ToUTF8 : function(str) {  
    var res = [], len = str.length;  
    for (var i = 0; i < len; i++) {  
        var code = str.charCodeAt(i);  
        if (code > 0x0000 && code <= 0x007F) {  
            // 单字节,这里并不考虑0x0000,因为它是空字节  
            // U+00000000 – U+0000007F  0xxxxxxx  
            res.push(str.charAt(i));  
        } else if (code >= 0x0080 && code <= 0x07FF) {  
            // 双字节  
            // U+00000080 – U+000007FF  110xxxxx 10xxxxxx  
            // 110xxxxx  
            var byte1 = 0xC0 | ((code >> 6) & 0x1F);  
            // 10xxxxxx  
            var byte2 = 0x80 | (code & 0x3F);  
            res.push(  
                String.fromCharCode(byte1),   
                String.fromCharCode(byte2)  
            );  
        } else if (code >= 0x0800 && code <= 0xFFFF) {  
            // 三字节  
            // U+00000800 – U+0000FFFF  1110xxxx 10xxxxxx 10xxxxxx  
            // 1110xxxx  
            var byte1 = 0xE0 | ((code >> 12) & 0x0F);  
            // 10xxxxxx  
            var byte2 = 0x80 | ((code >> 6) & 0x3F);  
            // 10xxxxxx  
            var byte3 = 0x80 | (code & 0x3F);  
            res.push(  
                String.fromCharCode(byte1),   
                String.fromCharCode(byte2),   
                String.fromCharCode(byte3)  
            );  
        } else if (code >= 0x00010000 && code <= 0x001FFFFF) {  
            // 四字节  
            // U+00010000 – U+001FFFFF  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx  
        } else if (code >= 0x00200000 && code <= 0x03FFFFFF) {  
            // 五字节  
            // U+00200000 – U+03FFFFFF  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx  
        } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ {  
            // 六字节  
            // U+04000000 – U+7FFFFFFF  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx  
            }  
        }  
 
        return res.join('');  
},  
UTF8ToUTF16 : function(str) {  
    var res = [], len = str.length;  
    var i = 0;  
    for (var i = 0; i < len; i++) {  
        var code = str.charCodeAt(i);  
        // 对第一个字节进行判断  
        if (((code >> 7) & 0xFF) == 0x0) {  
            // 单字节  
            // 0xxxxxxx  
            res.push(str.charAt(i));  
        } else if (((code >> 5) & 0xFF) == 0x6) {  
            // 双字节  
            // 110xxxxx 10xxxxxx  
            var code2 = str.charCodeAt(++i);  
            var byte1 = (code & 0x1F) << 6;  
            var byte2 = code2 & 0x3F;  
            var utf16 = byte1 | byte2;  
            res.push(String.fromCharCode(utf16));  
        } else if (((code >> 4) & 0xFF) == 0xE) {  
            // 三字节  
            // 1110xxxx 10xxxxxx 10xxxxxx  
            var code2 = str.charCodeAt(++i);  
            var code3 = str.charCodeAt(++i);  
            var byte1 = (code << 4) | ((code2 >> 2) & 0x0F);  
            var byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);  
            utf16 = ((byte1 & 0x00FF) << 8) | byte2 ; 
            res.push(String.fromCharCode(utf16));  
        } else if (((code >> 3) & 0xFF) == 0x1E) {  
            // 四字节  
            // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx  
        } else if (((code >> 2) & 0xFF) == 0x3E) {  
            // 五字节  
            // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx  
        } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ {  
            // 六字节  
            // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx  
            }  
        }  
 
        return res.join('');  
}, 
code:function(str){
	var index = 0;
	for(var i=0;i<this.table.length;i++){
		if(str==this.table[i]){
			index = i;
			break;
		}
	}
	return index;
},
encode : function(str) {  
    if (!str) {  
        return '';  
    }  
    var utf8    = this.UTF16ToUTF8(str); // 转成UTF8  
    var i = 0; // 遍历索引  
    var len = utf8.length;  
    var res = [];  
    while (i < len) {  
        var c1 = utf8.charCodeAt(i++) & 0xFF;  
        res.push(this.table[c1 >> 2]);  
        // 需要补2个=  
        if (i == len) {  
            res.push(this.table[(c1 & 0x3) << 4]);  
            res.push('==');  
            break;  
        }  
        var c2 = utf8.charCodeAt(i++);  
        // 需要补1个=  
        if (i == len) {  
            res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);  
            res.push(this.table[(c2 & 0x0F) << 2]);  
            res.push('=');  
                break;  
            }  
            var c3 = utf8.charCodeAt(i++);  
            res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);  
            res.push(this.table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);  
            res.push(this.table[c3 & 0x3F]);  
        }  
 
        return res.join('');  
},  
decode : function(str) {  
    if (!str) {  
        return '';  
        }  
 
        var len = str.length;  
        var i   = 0;  
        var res = [];  
 
        while (i < len) { 
			/*
			//火狐能用,IE8不能用 
			alert("下标:"+this.code(str.charAt(i++)));
	        code1 = this.table.indexOf(str.charAt(i++)); 
	        code2 = this.table.indexOf(str.charAt(i++));  
	        code3 = this.table.indexOf(str.charAt(i++));  
	        code4 = this.table.indexOf(str.charAt(i++));  
			*/
	    	//IE、火狐都能用
			code1 = this.code(str.charAt(i++));  
			code2 = this.code(str.charAt(i++));  
			code3 = this.code(str.charAt(i++));  
			code4 = this.code(str.charAt(i++));
			c1 = (code1 << 2) | (code2 >> 4);  
			c2 = ((code2 & 0xF) << 4) | (code3 >> 2);  
			c3 = ((code3 & 0x3) << 6) | code4;  
			res.push(String.fromCharCode(c1));  
			if (code3 != 64) {  
			    res.push(String.fromCharCode(c2));  
			}  
			if (code4 != 64) {  
			    res.push(String.fromCharCode(c3));  
			}  
        }  
        return this.UTF8ToUTF16(res.join(''));  
    }  
};

页面测试:

window.onload=function(){
	var source = "你好 The Word!";
	var e64 = Base64.encode(source);  
	var d64 = Base64.decode(e64);
	document.getElementById("p1").innerHTML="原始数据:"+source;		
	document.getElementById("p2").innerHTML="加密:"+e64;
	document.getElementById("p3").innerHTML="解密:"+d64;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics