LSX-blog

Feed Rss

【收录】base64

12.31.2011, 加解密, by .

名片
—————————————————————————————————————
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到。

标准的Base64
—————————————————————————————————————
标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。
为解决此问题,可采用一种用于URL的改进Base64编码,它不在末尾填充’=’号,并将标准Base64中的“+”和“/”分别改成了“*”和“-”,这样就免去了在URL编解码和数据库存储时所要作的转换,避免了编码信息长度在此过程中的增加,并统一了数据库、表单等处对象标识符的格式。
另有一种用于正则表达式的改进Base64变种,它将“+”和“/”改成了“!”和“-”,因为“+”,“*”以及前面在IRCu中用到的“[”和“]”在正则表达式中都可能具有特殊含义。
此外还有一些变种,它们将“+/”改为“_-”或“._”(用作编程语言中的标识符名称)或“.-”(用于XML中的Nmtoken)甚至“_:”(用于XML中的Name)。
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。
规则
关于这个编码的规则:
①.把3个字符变成4个字符..
②每76个字符加一个换行符..
③.最后的结束符也要处理..
这样说会不会太抽象了?不怕,我们来看一个例子:
转换前 aaaaaabb ccccdddd eeffffff
转换后 00aaaaaa 00bbcccc 00ddddee 00ffffff
应该很清楚了吧?上面的三个字节是原文,下面的四个字节是转换后的Base64编码,其前两位均为0。
转换后,我们用一个码表来得到我们想要的字符串(也就是最终的Base64编码),这个表是这样的:(摘自RFC2045)


转换表
Table 1: The Base64 Alphabet
Value Encoding Value Encoding Value Encoding Value Encoding
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y

索引 对应字符 索引 对应字符 索引 对应字符 索引 对应字符
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w
15 P 32 g 49 x
16 Q 33 h 50 y

举例

———————————————————————————————————————

让我们再来看一个实际的例子,加深印象!
转换前 10101101 10111010 01110110
转换后 00101011 00011011 00101001 00110110
十进制 43 27 41 54
对应码表中的值 r b p 2
所以上面的24位编码,编码后的Base64值为 rbp2
解码同理,把 rbq2 的二进制位连接上再重组得到三个8位值,得出原码。
(解码只是编码的逆过程,在此我就不多说了,另外有关MIME的RFC还是有很多的,如果需要详细情况请自行查找。)
用更接近于编程的思维来说,编码的过程是这样的:
第一个字符通过右移2位获得第一个目标字符的Base64表位置,根据这个数值取到表上相应的字符,就是第一个目标字符。
然后将第一个字符左移4位加上第二个字符右移4位,即获得第二个目标字符。
再将第二个字符左移2位加上第三个字符右移6位,获得第三个目标字符。
最后取第三个字符的右6位即获得第四个目标字符。
在以上的每一个步骤之后,再把结果与 0x3F 进行 AND 位操作,就可以得到编码后的字符了。
可是等等……聪明的你可能会问到,原文的字节数量应该是3的倍数啊,如果这个条件不能满足的话,那该怎么办呢?
我们的解决办法是这样的:原文的字节不够的地方可以用全0来补足,转换时Base64编码用=号来代替。这就是为什么有些Base64编码会以一个或两个等号结束的原因,但等号最多只有两个。因为:
余数 = 原文字节数 MOD 3
所以余数任何情况下都只可能是0,1,2这三个数中的一个。如果余数是0的话,就表示原文字节数正好是3的倍数(最理想的情况啦)。如果是1的话,为了让Base64编码是3的倍数,就要补2个等号;同理,如果是2的话,就要补1个等号。

在URL中的应用

—————————————————————————————————-

Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到。
然而,标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。
为解决此问题,可采用一种用于URL的改进Base64编码,它不在末尾填充’=’号,并将标准Base64中的“+”和“/”分别改成了“*”和“-”,这样就免去了在URL编解码和数据库存储时所要作的转换,避免了编码信息长度在此过程中的增加,并统一了数据库、表单等处对象标识符的格式。
另有一种用于正则表达式的改进Base64变种,它将“+”和“/”改成了“!”和“-”,因为“+”,“*”以及前面在IRCu中用到的“[”和“]”在正则表达式中都可能具有特殊含义。
此外还有一些变种,它们将“+/”改为“_-”或“._”(用作编程语言中的标识符名称)或“.-”(用于XML中的Nmtoken)甚至“_:”(用于XML中的Name)。

在下载软件中加密下载地址的原理

——————————————————————————————————-

先以“迅雷下载”为例: 很多下载类网站都提供“迅雷下载”的链接,其地址通常是加密的迅雷专用下载地址。
如thunder://QUFodHRwOi8vd3d3LmJhaWR1LmNvbS9pbWcvc3NsbTFfbG9nby5naWZaWg==
其实迅雷的“专用地址”也是用Base64加密的,其加密过程如下:
一、在地址的前后分别添加AA和ZZ
如www.baidu.com/img/sslm1_logo.gif变成
AAwww.baidu.com/img/sslm1_logo.gifZZ
二、对新的字符串进行Base64编码
如AAwww.baidu.com/img/sslm1_logo.gifZZ用Base64编码得到
QUF3d3cuYmFpZHUuY29tL2ltZy9zc2xtMV9sb2dvLmdpZlpa
三、在上面得到的字符串前加上“thunder://”就成了
thunder://QUF3d3cuYmFpZHUuY29tL2ltZy9zc2xtMV9sb2dvLmdpZlpa
另:
Flashget的与迅雷类似,只不过在第一步时加的“料”不同罢了,Flashget在地址前后加的“料”是[FLASHGET]
而QQ旋风的干脆不加料,直接就对地址进行Base64编码了

PHP中的解密下载地址的实现

————————————————————————————–

 

[下列代码仅在GBK中实现,UTF8代码请把 if($button==”迅雷地址->普通地址”) echo substr(base64_decode(str_ireplace(“thunder://”,””,$txt1)),2,-2); 这句改为if($button==”迅雷地址->普通地址”) echo substr(mb_convert_encoding(base64_decode(str_ireplace(“thunder://”,””,$txt1))),2,-2); 并把charset=gb2312改为charset=utf-8]

1
2
3
4
5
6
<!--?php 
  $txt1=trim($_POST['text1']);
  $txt2=trim($_POST['text2']);
  $txt3=trim($_POST['text3']);
  $button=$_POST['button'];
    ?-->

迅雷转换

转换地址:   

转换后的:    迅雷地址”) echo “thunder://”.base64_encode(“AA”.$txt1.”ZZ”);   if($button==”迅雷地址->普通地址”) echo substr(base64_decode(str_ireplace(“thunder://”,””,$txt1)),2,-2);   ?>” style=”width:516px;” />

  

FlashGet转换

FlashGet地址:   

转换后 地址:    FlashGet地址”) echo “flashget://”.base64_encode($txt2);   if($button==”FlashGet地址->普通地址”) echo str_ireplace(“[FLASHGET]”,””,base64_decode(str_ireplace(“flashget://”,””,$txt2)));   ?>” style=”width:516px;” />

  

QQ旋风转换

QQ旋风地址:   

转换后 地址:    QQ旋风”) echo “qqdl://”.base64_encode($txt3);   if($button==”QQ旋风->普通地址”) echo base64_decode(str_ireplace(“qqdl://”,””,$txt3));   ?>” style=”width:516px;” />

  

1
 

JS版的Base64编码函数定义
——————————————————————————————————

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var base64EncodeChars = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/;
var base64DecodeChars = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
function base64encode(str)
{
var returnVal, i, len;
var c1, c2, c3;
len = str.length;
i = 0;
returnVal = “”;
while(i &lt; len)
{
c1 = str.charCodeAt(i++) &amp; 0xff;
if(i == len)
{
returnVal += base64EncodeChars.charAt(c1 &gt;&gt; 2);
returnVal += base64EncodeChars.charAt((c1 &amp; 0×3) &lt;&lt; 4);
returnVal +===;
break;
}
c2 = str.charCodeAt(i++);
if(i == len)
{
returnVal += base64EncodeChars.charAt(c1 &gt;&gt; 2);
returnVal += base64EncodeChars.charAt(((c1 &amp; 0×3)&lt;&lt; 4) | ((c2 &amp; 0xF0) &gt;&gt; 4));
returnVal += base64EncodeChars.charAt((c2 &amp; 0xF) &lt;&lt; 2);
returnVal +==;
break;
}
c3 = str.charCodeAt(i++);
returnVal += base64EncodeChars.charAt(c1 &gt;&gt; 2);
returnVal += base64EncodeChars.charAt(((c1 &amp; 0×3)&lt;&lt; 4) | ((c2 &amp; 0xF0) &gt;&gt; 4));
returnVal += base64EncodeChars.charAt(((c2 &amp; 0xF) &lt;&lt; 2) | ((c3 &amp; 0xC0) &gt;&gt;6));
returnVal += base64EncodeChars.charAt(c3 &amp; 0x3F);
}
return returnVal;
}
function base64decode(str)
{
var c1, c2, c3, c4;
var i, len, returnVal;
len = str.length;
i = 0;
returnVal = “”;
while(i &lt; len)
{
/* c1 */
do
{
c1 = base64DecodeChars[str.charCodeAt(i++) &amp; 0xff];
} while(i &lt; len &amp;&amp; c1 == -1);
if(c1 == -1)
break;
/* c2 */
do
{
c2 = base64DecodeChars[str.charCodeAt(i++) &amp; 0xff];
} while(i &lt; len &amp;&amp; c2 == -1);
if(c2 == -1)
break;
returnVal += String.fromCharCode((c1 &lt;&lt; 2) | ((c2 &amp; 0×30) &gt;&gt; 4));
/* c3 */
do
{
c3 = str.charCodeAt(i++) &amp; 0xff;
if(c3 == 61)
return returnVal;
c3 = base64DecodeChars[c3];
} while(i &lt; len &amp;&amp; c3 == -1);
if(c3 == -1)
break;
returnVal += String.fromCharCode(((c2 &amp; 0XF) &lt;&lt; 4) | ((c3 &amp; 0x3C) &gt;&gt; 2));
/* c4 */
do
{
c4 = str.charCodeAt(i++) &amp; 0xff;
if(c4 == 61)
return returnVal;
c4 = base64DecodeChars[c4];
} while(i &lt; len &amp;&amp; c4 == -1);
if(c4 == -1)
break;
returnVal += String.fromCharCode(((c3 &amp; 0×03) &lt;&lt; 6) | c4);
}
return returnVal;
}

AS3版的Base64
———————————————————————————————————————- package crypto{
import flash.utils.ByteArray;
public class Base64 {
private static const BASE64_CHARS:String = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=”;
public static function encode(data:String):String {
// Convert string to ByteArray
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes(data);
// Return encoded ByteArray
return encodeByteArray(bytes);
}
public static function encodeByteArray(data:ByteArray):String {
// Initialise output
var output:String = “”;
// Create data and output buffers
var dataBuffer:Array;
var outputBuffer:Array = new Array(4);
// Rewind ByteArray
data.position = 0;
// while there are still bytes to be processed
while (data.bytesAvailable > 0) {
// Create new data buffer and populate next 3 bytes from data
dataBuffer = new Array();
for (var i:uint = 0; i < 3 && data.bytesAvailable > 0; i++) {
dataBuffer= data.readUnsignedByte();
}
// Convert to data buffer Base64 character positions and
// store in output buffer
outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2;
outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4);
outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6);
outputBuffer[3] = dataBuffer[2] & 0x3f;
// If data buffer was short (i.e not 3 characters) then set
// end character indexes in data buffer to index of ‘=’ symbol.
// This is necessary because Base64 data is always a multiple of
// 4 bytes and is basses with ‘=’ symbols.
for (var j:uint = dataBuffer.length; j < 3; j++) {
outputBuffer[j + 1] = 64;
}
// Loop through output buffer and add Base64 characters to
// encoded data string for each character.
for (var k:uint = 0; k < outputBuffer.length; k++) {
output += BASE64_CHARS.charAt(outputBuffer[k]);
}
}
// Return encoded data
return output;
}
public static function decode(data:String):String {
// Decode data to ByteArray
var bytes:ByteArray = decodeToByteArray(data);
// Convert to string and return
return bytes.readUTFBytes(bytes.length);
}
public static function decodeToByteArray(data:String):ByteArray {
// Initialise output ByteArray for decoded data
var output:ByteArray = new ByteArray();
// Create data and output buffers
var dataBuffer:Array = new Array(4);
var outputBuffer:Array = new Array(3);
// While there are data bytes left to be processed
for (var i:uint = 0; i < data.length; i += 4) {
// Populate data buffer with position of Base64 characters for
// next 4 bytes from encoded data
for (var j:uint = 0; j < 4 && i + j < data.length; j++) {
dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j));
}
// Decode data buffer back into bytes
outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 0x30) >> 4);
outputBuffer[1] = ((dataBuffer[1] & 0x0f) << 4) + ((dataBuffer[2] & 0x3c) >> 2);
outputBuffer[2] = ((dataBuffer[2] & 0x03) << 6) + dataBuffer[3];
// Add all non-padded bytes in output buffer to decoded data
for (var k:uint = 0; k < outputBuffer.length; k++) {
if (dataBuffer[k+1] == 64) break;
output.writeByte(outputBuffer[k]);
}
}
// Rewind decoded data ByteArray
output.position = 0;
// Return decoded data
return output;
}
public function Base64() {
throw new Error(“Base64 class is static container only”);
}
}
}

JS实现BASE64加密、解密算法

———————————————————————————————–

此算法只适用于加密ASCII映射表中的字符组成的字符串,不能处理中文字符串等:
脚本如下:
<script language=javascript>
var base64EncodeChars = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
var base64DecodeChars = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
function base64encode(str) {
var out, i, len;
var c1, c2, c3;
len = str.length;
i = 0;
out = “”;
while(i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt((c1 & 0x3) << 4);
out += “==”;
break;
}
c2 = str.charCodeAt(i++);
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt((c2 & 0xF) << 2);
out += “=”;
break;
}
c3 = str.charCodeAt(i++);
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
out += base64EncodeChars.charAt(c3 & 0x3F);
}
return out;
}
function base64decode(str) {
var c1, c2, c3, c4;
var i, len, out;
len = str.length;
i = 0;
out = “”;
while(i < len) {
/* c1 */
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while(i < len && c1 == -1);
if(c1 == -1)
break;
/* c2 */
do {
c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while(i < len && c2 == -1);
if(c2 == -1)
break;
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
/* c3 */
do {
c3 = str.charCodeAt(i++) & 0xff;
if(c3 == 61)
return out;
c3 = base64DecodeChars[c3];
} while(i < len && c3 == -1);
if(c3 == -1)
break;
out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
/* c4 */
do {
c4 = str.charCodeAt(i++) & 0xff;
if(c4 == 61)
return out;
c4 = base64DecodeChars[c4];
} while(i < len && c4 == -1);
if(c4 == -1)
break;
out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
}
return out;
}
</script>
================================================================================
================================================================================
================================================================================
以下代码可以加密或处理UTF字符串:
以及实例:
<table width=”773″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″>
<tr>
<td>
<script language=javascript>
var base64EncodeChars = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
var base64DecodeChars = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
function base64encode(str) {
var out, i, len;
var c1, c2, c3;
len = str.length;
i = 0;
out = “”;
while(i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt((c1 & 0x3) << 4);
out += “==”;
break;
}
c2 = str.charCodeAt(i++);
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt((c2 & 0xF) << 2);
out += “=”;
break;
}
c3 = str.charCodeAt(i++);
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
out += base64EncodeChars.charAt(c3 & 0x3F);
}
return out;
}
function base64decode(str) {
var c1, c2, c3, c4;
var i, len, out;
len = str.length;
i = 0;
out = “”;
while(i < len) {
/* c1 */
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while(i < len && c1 == -1);
if(c1 == -1)
break;
/* c2 */
do {
c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while(i < len && c2 == -1);
if(c2 == -1)
break;
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
/* c3 */
do {
c3 = str.charCodeAt(i++) & 0xff;
if(c3 == 61)
return out;
c3 = base64DecodeChars[c3];
} while(i < len && c3 == -1);
if(c3 == -1)
break;
out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
/* c4 */
do {
c4 = str.charCodeAt(i++) & 0xff;
if(c4 == 61)
return out;
c4 = base64DecodeChars[c4];
} while(i < len && c4 == -1);
if(c4 == -1)
break;
out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
}
return out;
}
function utf16to8(str) {
var out, i, len, c;
out = “”;
len = str.length;
for(i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
function utf8to16(str) {
var out, i, len, c;
var char2, char3;
out = “”;
len = str.length;
i = 0;
while(i < len) {
c = str.charCodeAt(i++);
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += str.charAt(i-1);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
function doit() {
var f = document.f
f.output.value = base64encode(f.source.value)
f.decode.value = base64decode(f.output.value)
}
</script>
<H1 align=”center”>Base64编码加密
</H1>
<FORM NAME=”f”>
原码<BR>
<TEXTAREA NAME=”source” ROWS=4 COLS=60 WRAP=”soft”></TEXTAREA><BR><BR>
Base64 encode<BR>
<TEXTAREA NAME=”output” ROWS=4 COLS=60 WRAP=”soft”></TEXTAREA><BR><BR>
Base64 decode<BR>
<TEXTAREA NAME=”decode” ROWS=4 COLS=60 WRAP=”soft”></TEXTAREA><BR><BR>
<INPUT TYPE=BUTTON VALUE=”转换” ONCLICK=”doit()”>
</FORM>
</td>
</tr>
</table>

C#实现BASE64加密、解密算法

—————————————————————————————–

方法一: /// <summary>
/// Base64加密
/// </summary>
/// <param name=”Message”></param>
/// <returns></returns>
public string Base64Code(string Message)
{
char[] Base64Code = new char[]{‘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′,’+’,’/’,’=’};
byte empty = (byte)0;
System.Collections.ArrayList byteMessage = new System.Collections.ArrayList(System.Text.Encoding.Default.GetBytes(Message));
System.Text.StringBuilder outmessage;
int messageLen = byteMessage.Count;
//将字符分成3个字节一组,如果不足,则以0补齐
int page = messageLen / 3;
int use = 0;
if ((use = messageLen % 3) > 0)
{
for (int i = 0; i < 3 – use; i++)
byteMessage.Add(empty);
page++;
}
//将3个字节的每组字符转换成4个字节一组的。3个一组,一组一组变成4个字节一组
//方法是:转换成ASCII码,按顺序排列24 位数据,再把这24位数据分成4组,即每组6位。再在每组的的最高位前补两个0凑足一个字节。
outmessage = new System.Text.StringBuilder(page * 4);
for (int i = 0; i < page; i++)
{
//取一组3个字节的组
byte[] instr = new byte[3];
instr[0] = (byte)byteMessage[i * 3];
instr[1] = (byte)byteMessage[i * 3 + 1];
instr[2] = (byte)byteMessage[i * 3 + 2];
//六个位为一组,补0变成4个字节
int[] outstr = new int[4];
//第一个输出字节:取第一输入字节的前6位,并且在高位补0,使其变成8位(一个字节)
outstr[0] = instr[0] >> 2;
//第二个输出字节:取第一输入字节的后2位和第二个输入字节的前4位(共6位),并且在高位补0,使其变成8位(一个字节)
outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4);
//第三个输出字节:取第二输入字节的后4位和第三个输入字节的前2位(共6位),并且在高位补0,使其变成8位(一个字节)
if (!instr[1].Equals(empty))
outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6);
else
outstr[2] = 64;
//第四个输出字节:取第三输入字节的后6位,并且在高位补0,使其变成8位(一个字节)
if (!instr[2].Equals(empty))
outstr[3] = (instr[2] & 0x3f);
else
outstr[3] = 64;
outmessage.Append(Base64Code[outstr[0]]);
outmessage.Append(Base64Code[outstr[1]]);
outmessage.Append(Base64Code[outstr[2]]);
outmessage.Append(Base64Code[outstr[3]]);
}
return outmessage.ToString();
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name=”Message”></param>
/// <returns></returns>
public string Base64Decode(string Message)
{
if ((Message.Length % 4) != 0)
{
throw new ArgumentException(“不是正确的BASE64编码,请检查。”, “Message”);
}
if (!System.Text.RegularExpressions.Regex.IsMatch(Message, “^[A-Z0-9/+=]*$”, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
throw new ArgumentException(“包含不正确的BASE64编码,请检查。”, “Message”);
}
string Base64Code = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=”;
int page = Message.Length / 4;
System.Collections.ArrayList outMessage = new System.Collections.ArrayList(page * 3);
char[] message = Message.ToCharArray();
for (int i = 0; i < page; i++)
{
byte[] instr = new byte[4];
instr[0] = (byte)Base64Code.IndexOf(message[i * 4]);
instr[1] = (byte)Base64Code.IndexOf(message[i * 4 + 1]);
instr[2] = (byte)Base64Code.IndexOf(message[i * 4 + 2]);
instr[3] = (byte)Base64Code.IndexOf(message[i * 4 + 3]);
byte[] outstr = new byte[3];
outstr[0] = (byte)((instr[0] << 2) ^ ((instr[1] & 0x30) >> 4));
if (instr[2] != 64)
{
outstr[1] = (byte)((instr[1] << 4) ^ ((instr[2] & 0x3c) >> 2));
}
else
{
outstr[2] = 0;
}
if (instr[3] != 64)
{
outstr[2] = (byte)((instr[2] << 6) ^ instr[3]);
}
else
{
outstr[2] = 0;
}
outMessage.Add(outstr[0]);
if (outstr[1] != 0)
outMessage.Add(outstr[1]);
if (outstr[2] != 0)
outMessage.Add(outstr[2]);
}
byte[] outbyte = (byte[])outMessage.ToArray(Type.GetType(“System.Byte”));
return System.Text.Encoding.Default.GetString(outbyte);
}
直接使用.NET中的的库类函数
方法二:
/// <summary>
/// Base64加密
/// </summary>
/// <param name=”Message”></param>
/// <returns></returns>
public string Base64Code(string Message)
{
byte[] bytes = Encoding.Default.GetBytes(Message);
return Convert.ToBase64String(bytes);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name=”Message”></param>
/// <returns></returns>
public string Base64Decode(string Message)
{
byte[] bytes = Convert.FromBase64String(Message);
return Encoding.Default.GetString(bytes);
}

Base64-MIME

———————————————————————————

在MIME格式的电子邮件中,base64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本。使用时,在传输编码方式中指定base64。使用的字符包括大小写字母各26个,加上10个数字,和加号“+”,斜杠“/”,一共64个字符,等号“=”用来作为后缀用途。
完整的base64定义可见 RFC1421和 RFC2045。编码后的数据比原始数据略长,为原来的4/3。在电子邮件中,根据RFC822规定,每76个字符,还需要加上一个回车换行。可以估算编码后数据长度大约为原长的135.1%。
转换的时候,将三个byte的数据,先后放入一个24bit的缓冲区中,先来的byte占高位。数据不足3byte的话,于缓冲区中剩下的Bit用0补足。然后,每次取出6个bit,按照其值选择ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作为编码后的输出。不断进行,直到全部输入数据转换完成。
如果最后剩下两个输入数据,在编码结果后加1个“=”;如果最后剩下一个输入数据,编码结果后加2个“=”;如果没有剩下任何数据,就什么都不要加,这样才可以保证资料还原的正确性。
举例来说,一段引用自Thomas Hobbes’s Leviathan的文句:
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.
经过base64编码之后变成:
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=

【收录】base64 有 85 条回应

  1. This article is actually a good one it helps new web visitors, who are wishing
    in favor of blogging.

    回复
  2. I think the admin of this website is really working
    hard for his web site, since here every stuff is
    quality based material.

    回复
  3. No matter if some one searches for his vital thing, so he/she needs to be available that in detail, thus that thing is maintained over here.

    回复
  4. It’s an awesome paragraph in favor of all the web viewers; they will take benefit from it I
    am sure.

    回复
  5. You could definitely see your expertise in the article you write.

    The sector hopes for more passionate writers such as
    you who aren’t afraid to mention how they believe. All the time go
    after your heart.

    回复
  6. This design is steller! You most certainly know how
    to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Great job. I really loved what you had to say, and more than that, how
    you presented it. Too cool!

    回复
  7. naturally like your web-site however you need to test the spelling on quite a few of your posts.
    A number of them are rife with spelling issues and I in finding it very bothersome to inform the reality then again I will certainly
    come back again.

    回复
  8. Excellent beat ! I would like to apprentice whilst you amend your
    site, how can i subscribe for a weblog website? The account helped me a applicable deal.
    I were a little bit familiar of this your broadcast offered vivid transparent idea

    回复
  9. What’s up to all, how is everything, I think every one is getting more from this web site,
    and your views are good in support of new people.

    回复
  10. Nice post. I used to be checking constantly this blog and I’m
    impressed! Very helpful info specially the last phase 🙂 I care for such information much.
    I used to be seeking this particular information for a long time.
    Thank you and good luck.

    回复
  11. If you desire to obtain a good deal from this paragraph then you
    have to apply these techniques to your won webpage.

    回复
  12. Fine way of describing, and good post to take facts about my presentation focus, which i am
    going to convey in institution of higher education.

    回复
  13. Painter And Decorator Gloucester
    38 Bruton Way
    Gloucester GL1 1DA, United Kingdom
    01452 223268

    回复
  14. [url=https://loansonlinenb.com/]payday loans online no credit check[/url]

    回复
  15. Quality posts is the secret to interest the people to go to see the
    website, that’s what this site is providing.

    回复
  16. [url=http://tadalafil365.com/]buy tadalafil 20mg[/url] [url=http://indocinonline.com/]indocin online[/url] [url=http://valtrex1.com/]valtrex for sale online[/url] [url=http://femaleviagra50.com/]female viagra[/url] [url=http://genericcolchicine.com/]colchicine[/url] [url=http://sildenafilx.com/]sildenafil price[/url]

    回复
  17. [url=https://diflucanrx.com/]buy fluconazole no rx[/url] [url=https://triamterenebenzathiazide.com/]buy triamterene[/url] [url=https://dipyridamol.com/]buy dipyridamole[/url] [url=https://prednisonegen.com/]buy prednisone[/url] [url=https://yasmindrospirenone.com/]buy yasmin[/url] [url=https://trentalpentoxifylline.com/]trental price[/url] [url=https://allopurinolmed.com/]allopurinol 100 mg tablet[/url] [url=https://amoxicillincap.com/]amoxicillin 875 mg[/url]

    回复
  18. Hello friends, how is all, and what you desire to say about this post, in my view its really remarkable for me.|

    回复
  19. Ꮋеllo! I know this is kinda off topic but I was wondering ѡhich
    bllog platform are you using for this site? I’m ցetting tired of WordPress Ƅecause Ӏ’ᴠe
    had probⅼems wifh hackers and І’m looking at options f᧐r anotһer platform.
    I wоuld bе great іf you could oint me iin the direction of ɑ
    good platform.

    回复
  20. n coronavirus cure [url=http://chloroquine-phosphate-250mg.com/#]chloroquine online[/url] n coronavirus cure chloroquine 250mg – coronavirus treatable coronavirus 50 million

    回复
  21. cbd tincture reviews [url=http://cbd-7.com/#]cbd oil for pain management[/url] cbd oil for pain management buy cbd oil –
    cbd sweets cbd dosage for sleep

    回复
  22. erection viagra gel [url=http://viagrandr.com/#]how much will generic
    viagra cost[/url] viagra coupon cvs important topics viagra
    pills – viagra for daily use reviews similar threads viagra
    20mg side effects legend

    回复
  23. viagra for sale [url=http://viagrawithoutdoctorpres.net/#]viagra without a doctor
    prescription usa [/url] viagra canadian pharmacy
    no prescription there have been viagra without doctors prescription – viagra samples viagra canadian pharmacy online members

    回复
  24. Ridiculous story there. What occurred after?

    Good luck!

    回复
  25. Quality articles is the secret to be a focus for the people to pay a quick visit the web site, that’s what this site is providing.

    回复
  26. Everyone loves what you guys tend to be up too. Such clever work and coverage!
    Keep up the terrific works guys I’ve included you guys
    to blogroll.

    回复
  27. you’re in reality a excellent webmaster. The site loading pace
    is incredible. It seems that you’re doing any distinctive
    trick. Moreover, The contents are masterwork. you have done a excellent activity in this topic!

    回复
  28. Right here is the right blog for anybody who wants to understand this topic.
    You understand so much its almost tough to argue with you (not that I really would want to…HaHa).

    You certainly put a fresh spin on a topic that’s been discussed for decades.
    Great stuff, just wonderful!

    回复
  29. Yes! Finally someone writes about sling tv best package 2020.

    回复
  30. Superb blog! Do you have any tips for aspiring writers?
    I’m planning to start my own site soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed ..
    Any ideas? Thank you!

    回复
  31. I am curious to find out what blog system you’re using?

    I’m having some minor security problems with my
    latest blog and I would like to find something more safe.
    Do you have any solutions?

    回复
  32. Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year old daughter and said
    “You can hear the ocean if you put this to your ear.” She put the
    shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is
    totally off topic but I had to tell someone!

    回复
  33. Definitely imagine that that you said. Your favourite justification seemed to be on the net the simplest thing to
    take into account of. I say to you, I certainly get annoyed even as other people consider worries that
    they just don’t recognize about. You managed to hit the nail
    upon the highest and also defined out the whole thing
    with no need side effect , other folks can take a signal.
    Will likely be again to get more. Thank you

    回复
  34. Wonderful blog! I found it while searching on Yahoo News.

    Do you have any tips on how to get listed
    in Yahoo News? I’ve been trying for a while but I never
    seem to get there! Cheers

    回复
  35. You actually make it seem so easy with your presentation but I find this matter
    to be really something that I think I would never understand.

    It seems too complex and very broad for me. I am looking forward
    for your next post, I’ll try to get the hang of it!

    回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注