Base64 Encode Decode Algorithm
2009-12-29 17:05
260 查看
I wrote this code about five years ago. I used it to encode query strings in web urls so that some sensitive information(IDs, keys) are not presented to the user directly. Then the query string is decoded to retrieved parameters. Since the output of Convert.ToBase64String in .Net framework is not suitable for URLs, I reimplemented the algorithm. You can find more description about Base64 in WikiPedia.
The Code
/// <summary>
/// Base64 Encode Decode Methods
/// </summary>
public class Base64 {
/// <summary>
/// Alphabet used for encoded string representation.
/// It should contain exactly 64 characters.
/// This can be changed to other text, but must be the same in encode/decode.
/// </summary>
public static string Alphabet
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz[]";
public static char Pad = '!';
public static string EncodeText(string text){
return EncodeText(text,Encoding.UTF8);
}
public static string DecodeText(string code){
return DecodeText(code,Encoding.UTF8);
}
public static string EncodeText(string text,Encoding encoding){
return Encode(encoding.GetBytes(text));
}
public static string DecodeText(string code,Encoding encoding){
return encoding.GetString(Decode(code));
}
public static string Encode(byte[] _data){
int n = _data.Length;
if(n==0)return"";
int r = n % 3;
int p = 3 - r;
if(p==3)p=0;
int k = n + p;
byte[] data = new byte[k];
_data.CopyTo(data,0);
int m = (k/3)*4;
int[] code = new int[m];
/* 6 2 4 4 2 6
* data|###### ##|#### ####|## ######|
* code|######|## ####|#### ##|######|
*
* code[0]=data[0]>>2;
* code[1]=data[0]<<4&X | data[1]>>4; X = 0b00110000 = 48;
* code[2]=data[1]<<2&Y | data[2]>>6; Y = 0b00111100 = 60;
* code[3]=data[2]&Z; Z = 0b00111111 = 63;
*/
for(int i=0;i<k/3;i++){
code[i*4+0] = data[i*3+0]>>2;
code[i*4+1] = data[i*3+0]<<4 & 48 | data[i*3+1]>>4;
code[i*4+2] = data[i*3+1]<<2 & 60 | data[i*3+2]>>6;
code[i*4+3] = data[i*3+2] & 63;
}
StringBuilder codestring = new StringBuilder(new string(Pad,m));
for(int i=0;i<m-p;i++){
codestring[i] = Alphabet[code[i]];
}
return codestring.ToString();
}
public static byte[] Decode(string _code){
int n = _code.Length;
byte[] code = new byte
;
for(int i=0;i<n;i++){
code[i] = LetterIndex(_code[i]);
}
int p = _code.IndexOf(Pad);
if(p==-1){
p = 0;
}else{
p = n - p;
}
int m = (n/4)*3;
byte[] data = new byte[m];
/* 6 2 4 4 2 6
* data|###### ##|#### ####|## ######|
* code|######|## ####|#### ##|######|
*
* data[0]=code[0]<<2 | code[1]>>4;
* data[1]=code[1]<<4 | code[2]>>2;
* data[2]=code[2]<<6 | code[3];
*/
for(int i=0;i<n/4;i++){
data[i*3+0]=(byte)(code[i*4+0]<<2 | code[i*4+1]>>4);
data[i*3+1]=(byte)(code[i*4+1]<<4 | code[i*4+2]>>2);
data[i*3+2]=(byte)(code[i*4+2]<<6 | code[i*4+3]);
}
byte[] _data = new byte[m-p];
for(int i=0;i<_data.Length;i++){
_data[i] = data[i];
}
return _data;
}
static byte LetterIndex(char letter){
int index = Alphabet.IndexOf(letter);
if(index == -1){
index = 0;
}
return (byte)index;
}
}
The Code
/// <summary>
/// Base64 Encode Decode Methods
/// </summary>
public class Base64 {
/// <summary>
/// Alphabet used for encoded string representation.
/// It should contain exactly 64 characters.
/// This can be changed to other text, but must be the same in encode/decode.
/// </summary>
public static string Alphabet
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz[]";
public static char Pad = '!';
public static string EncodeText(string text){
return EncodeText(text,Encoding.UTF8);
}
public static string DecodeText(string code){
return DecodeText(code,Encoding.UTF8);
}
public static string EncodeText(string text,Encoding encoding){
return Encode(encoding.GetBytes(text));
}
public static string DecodeText(string code,Encoding encoding){
return encoding.GetString(Decode(code));
}
public static string Encode(byte[] _data){
int n = _data.Length;
if(n==0)return"";
int r = n % 3;
int p = 3 - r;
if(p==3)p=0;
int k = n + p;
byte[] data = new byte[k];
_data.CopyTo(data,0);
int m = (k/3)*4;
int[] code = new int[m];
/* 6 2 4 4 2 6
* data|###### ##|#### ####|## ######|
* code|######|## ####|#### ##|######|
*
* code[0]=data[0]>>2;
* code[1]=data[0]<<4&X | data[1]>>4; X = 0b00110000 = 48;
* code[2]=data[1]<<2&Y | data[2]>>6; Y = 0b00111100 = 60;
* code[3]=data[2]&Z; Z = 0b00111111 = 63;
*/
for(int i=0;i<k/3;i++){
code[i*4+0] = data[i*3+0]>>2;
code[i*4+1] = data[i*3+0]<<4 & 48 | data[i*3+1]>>4;
code[i*4+2] = data[i*3+1]<<2 & 60 | data[i*3+2]>>6;
code[i*4+3] = data[i*3+2] & 63;
}
StringBuilder codestring = new StringBuilder(new string(Pad,m));
for(int i=0;i<m-p;i++){
codestring[i] = Alphabet[code[i]];
}
return codestring.ToString();
}
public static byte[] Decode(string _code){
int n = _code.Length;
byte[] code = new byte
;
for(int i=0;i<n;i++){
code[i] = LetterIndex(_code[i]);
}
int p = _code.IndexOf(Pad);
if(p==-1){
p = 0;
}else{
p = n - p;
}
int m = (n/4)*3;
byte[] data = new byte[m];
/* 6 2 4 4 2 6
* data|###### ##|#### ####|## ######|
* code|######|## ####|#### ##|######|
*
* data[0]=code[0]<<2 | code[1]>>4;
* data[1]=code[1]<<4 | code[2]>>2;
* data[2]=code[2]<<6 | code[3];
*/
for(int i=0;i<n/4;i++){
data[i*3+0]=(byte)(code[i*4+0]<<2 | code[i*4+1]>>4);
data[i*3+1]=(byte)(code[i*4+1]<<4 | code[i*4+2]>>2);
data[i*3+2]=(byte)(code[i*4+2]<<6 | code[i*4+3]);
}
byte[] _data = new byte[m-p];
for(int i=0;i<_data.Length;i++){
_data[i] = data[i];
}
return _data;
}
static byte LetterIndex(char letter){
int index = Alphabet.IndexOf(letter);
if(index == -1){
index = 0;
}
return (byte)index;
}
}
相关文章推荐
- How to encode decode String in Java base64 Encoding
- 基于VBS、ASP环境下的BASE64 Encode 和 Decode
- php base64_decode 与base64_encode
- base64_encode和base64_decode的JAVA实现
- Base64 encode/decode large file
- iOS 7: Base64 Encode and Decode NSData and NSString Objects
- Java如何进行Base64的编码(Encode)与解码(Decode)?
- JS 实现BASE64_ENCODE 和 BASE64_DECODE
- js加密解密之base64encode,base64decode
- 動網中用到的幾個Function和一個JS[base64encode,base64decode,md5,sendmail,js]
- [VB.Net]MD5/MD160/SHA1/SHA256/SHA384/SHA512/Base64-Encode/Base64-Decode
- php中base64_decode与base64_encode加密解密函数实例
- Base64 encode/decode large file
- Base64 Encode/Decode C/C++ 实现
- 字符串加密,解密,利用 sun.misc.BASE64Decode类的 encode(byte)编码和decodeBuffer(str)解码
- php使用base64_encode和base64_decode对数据进行编码和解码
- JS 实现BASE64_ENCODE和BASE64_DECODE(实例代码)
- php中base64_decode与base64_encode加密解密函数实例
- bin2hex,base64_encode,base_decode
- perl和js对web请求的base64 encode/decode