C# 实现Base64的编码\解码

编码类

using System;
namespace WebApplication1
{
///

1<summary>   
2/// Base64Encoder 的摘要说明   
3/// 说明:Base编码类   
4/// 写作者:Quentin   
5/// </summary>

public class Base64Encoder
{
byte[] source;
int length,length2;
int blockCount;
int paddingCount;

public Base64Encoder()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public Base64Encoder(byte[] input)
{
source=input;
length=input.Length;
if((length % 3)==0)
{
paddingCount=0;
blockCount=length/3;
}
else
{
paddingCount=3-(length % 3);
blockCount=(length+paddingCount) / 3;
}
length2=length+paddingCount;
}

public char[] GetEncoded()
{
byte[] source2;
source2=new byte[length2];

for (int x=0; x

  1<length2;x++) &="" (int="" (x<length)="" 252)="" b1="source2[x*3];" b1,="" b2="source2[x*3+1];" b2,="" b3="source2[x*3+2];" b3;="" buffer="new" byte="" byte[]="" byte[blockcount*4];="" char[]="" char[blockcount*4];="" else="" for="" if="" result="new" source2[x]="0;" temp,="" temp1="(byte)((b1" temp1,="" temp2,="" temp3,="" temp4;="" x="0;x&lt;blockCount;x++)" {="" }="">&gt;2); 
  2
  3temp=(byte)((b1 &amp; 3)&lt;&lt;4);   
  4temp2=(byte)((b2 &amp; 240)&gt;&gt;4);   
  5temp2+=temp; 
  6
  7temp=(byte)((b2 &amp; 15)&lt;&lt;2);   
  8temp3=(byte)((b3 &amp; 192)&gt;&gt;6);   
  9temp3+=temp; 
 10
 11temp4=(byte)(b3 &amp; 63); 
 12
 13buffer[x*4]=temp1;   
 14buffer[x*4+1]=temp2;   
 15buffer[x*4+2]=temp3;   
 16buffer[x*4+3]=temp4; 
 17
 18} 
 19
 20for (int x=0; x<blockcount*4;x++) '0','1','2','3','4','5','6','7','8','9','+','="" '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',="" '};="" (paddingcount)="" 0:break;="" 1:result[blockcount*4-1]="=" 2:result[blockcount*4-1]="=" ;="" ;break;="" b)="" break;="" case="" char="" char[64]="" char[]="" default:break;="" if((b="" lookuptable="new" private="" result;="" result[blockcount*4-2]="=" result[x]="sixbit2char(buffer[x]);" return="" sixbit2char(byte="" switch="" {="" }="">=0) &amp;&amp;(b&lt;=63))   
 21{   
 22return lookupTable[(int)b];   
 23}   
 24else   
 25{ 
 26
 27return ' ';   
 28}   
 29} 
 30
 31}   
 32} 
 33
 34  
 35解码类 
 36
 37using System; 
 38
 39namespace WebApplication1   
 40{   
 41/// <summary>   
 42/// Base64Decoder 的摘要说明。   
 43/// 说明:Base编码类   
 44/// 写作者:Quentin   
 45/// </summary>   
 46public class Base64Decoder   
 47{   
 48char[] source;   
 49int length, length2, length3;   
 50int blockCount;   
 51int paddingCount; 
 52
 53public Base64Decoder()   
 54{   
 55//   
 56// TODO: 在此处添加构造函数逻辑   
 57//   
 58} 
 59
 60public Base64Decoder(char[] input)   
 61{   
 62int temp=0;   
 63source=input;   
 64length=input.Length   
 65  
 66for (int x=0;x&lt;2;x++)   
 67{   
 68if(input[length-x-1]=='=')   
 69temp++;   
 70}   
 71paddingCount=temp; 
 72
 73blockCount=length/4;   
 74length2=blockCount*3;   
 75} 
 76
 77public byte[] GetDecoded()   
 78{   
 79byte[] buffer=new byte[length];   
 80byte[] buffer2=new byte[length2]; 
 81
 82for(int x=0;x<length;x++) &="" 48)="" b="(byte)(temp1&lt;&lt;2);" b,="" b1="(byte)((temp2" b1,b2,b3;="" buffer[x]="char2sixbit(source[x]);" byte="" for(int="" temp1="buffer[x*4];" temp1,="" temp2="buffer[x*4+1];" temp2,="" temp3="buffer[x*4+2];" temp3,="" temp4="buffer[x*4+3];" temp4;="" x="0;x&lt;blockCount;x++)" {="" }="">&gt;4);   
 83b1+=b; 
 84
 85b=(byte)((temp2 &amp; 15)&lt;&lt;4);   
 86b2=(byte)((temp3 &amp; 60)&gt;&gt;2);   
 87b2+=b; 
 88
 89b=(byte)((temp3 &amp; 3)&lt;&lt;6);   
 90b3=temp4;   
 91b3+=b; 
 92
 93buffer2[x*3]=b1;   
 94buffer2[x*3+1]=b2;   
 95buffer2[x*3+2]=b3;   
 96} 
 97
 98length3=length2-paddingCount;   
 99byte[] result=new byte[length3]; 
100
101for(int x=0;x&lt;length3;x++)   
102{   
103result[x]=buffer2[x];   
104} 
105
106return result;   
107} 
108
109private byte char2sixbit(char c)   
110{   
111char[] lookupTable=new char[64]   
112{ 
113
114'A','B','C','D','E','F','G','H','I','J','K','L','M','N',   
115'O','P','Q','R','S','T','U','V','W','X','Y', 'Z',   
116'a','b','c','d','e','f','g','h','i','j','k','l','m','n',   
117'o','p','q','r','s','t','u','v','w','x','y','z',   
118'0','1','2','3','4','5','6','7','8','9','+','/'};   
119if(c=='=')   
120return 0;   
121else   
122{   
123for (int x=0;x&lt;64;x++)   
124{   
125if (lookupTable[x]==c)   
126return (byte)x;   
127} 
128
129return 0;   
130} 
131
132} 
133
134}   
135}</length;x++)></blockcount*4;x++)></length2;x++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus