首页  编辑  

base64编码

Tags: /超级猛料/Format.格式,单位/Encode.编码、格式/   Date Created:

function StrToBase64(const Str: string): string;  

var  

I, J, K, Len, Len1: Integer;  

B3: array[0..2] of Byte;  

B4: array[0..3] of Byte;  

begin  

if Str = '' then  

begin  

   Result := '';  

   Exit;  

end;  

Len := Length(Str);  

Len1 := ((Len + 2) div 3) shl 2;  

SetString(Result, nil, Len1);  

I := 1;  

J := 1;  

while I <= Len do  

begin  

   for K := 0 to 2 do  

     if K + I > Len then B3[K] := 0  

     else B3[K] := Ord(Str[K + I]);  

   B4[0] := B3[0] shr 2;  

   B4[1] := ((B3[0] shl 4) or (B3[1] shr 4)) and 63;  

   B4[2] := ((B3[1] shl 2) or (B3[2] shr 6)) and 63;  

   B4[3] := B3[2] and 63;  

   for K := 0 to 3 do  

   begin  

     case B4[K] of  

       0..25: Result[J] := Chr(B4[K] + 65);   // 'A'..'Z'  

       26..51: Result[J] := Chr(B4[K] + 71);  // 'a'..'z'(B4[K]-26+97)  

       62: Result[J] := '+';  

       63: Result[J] := '/';  

     else Result[J] := Chr(B4[K] - 4);        // '0'..'9'(B4[K]-52+48)  

     end;  

//      Result[J] := Base64_Chars[B4[K] + 1];  

     Inc(J);  

   end;  

   Inc(I, 3);  

end;  

K := 3 - Len mod 3 - 1;  

if K <> 2 then  

   for I := Len1 - K to Len1 do  

     Result[I] := '=';  

end;  

 

function Base64ToStr(const Base64: string): string;  

var  

I, J, K, Len, Len1: Integer;  

B4: array[0..3] of Byte;  

begin  

if Base64 = '' then  

begin  

   Result := '';  

   Exit;  

end;  

Len := Length(Base64);  

if Len and 3 <> 0 then  

   raise Exception.Create('Invalid Base64 length');  

Len1 := (Len shr 2) * 3;  

SetString(Result, nil, Len1);  

I := 1;  

J := 1;  

while I <= Len do  

begin  

   for K := 0 to 3 do  

   begin  

     case Base64[I] of  

       'A'..'Z': B4[K] := Ord(Base64[I]) - 65;  

       'a'..'z': B4[K] := Ord(Base64[I]) - 71;  

       '0'..'9': B4[K] := Ord(Base64[I]) + 4;  

       '+': B4[K] := 62;  

       '/': B4[K] := 63;  

       '=': B4[K] := 0;  

     else raise Exception.CreateFmt('#%d: Invalid char in Base64', [Ord(Base64[I])]);  

     end;  

     Inc(I);  

   end;  

   Result[J] := Chr((B4[0] shl 2) or (B4[1] shr 4));  

   Result[J + 1] := Chr((B4[1] shl 4) or (B4[2] shr 2));  

   Result[J + 2] := Chr((B4[2] shl 6) or B4[3]);  

   Inc(J, 3);  

end;  

 

I := Pos('=', Base64);  

if I <> 0 then  

begin  

   I := Len - I + 1;  

   Delete(Result, Len1 - I + 1, I);  

end;  

 

end;

----------------------------------------

const BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

function FindInTable(CSource:char):integer;

begin

 result:=Pos(string(CSource),BaseTable)-1;

end;

////

function DecodeBase64(Source:string):string;  //base64 编码

var

 SrcLen,Times,i:integer;

 x1,x2,x3,x4,xt:byte;

begin

 result:='';

 SrcLen:=Length(Source);

 Times:=SrcLen div 4;

 for i:=0 to Times-1 do

 begin

   x1:=FindInTable(Source[1+i*4]);

   x2:=FindInTable(Source[2+i*4]);

   x3:=FindInTable(Source[3+i*4]);

   x4:=FindInTable(Source[4+i*4]);

   x1:=x1 shl 2;

   xt:=x2 shr 4;

   x1:=x1 or xt;

   x2:=x2 shl 4;

   result:=result+chr(x1);

   if x3= 64 then break;

   xt:=x3 shr 2;

   x2:=x2 or xt;

   x3:=x3 shl 6;

   result:=result+chr(x2);

   if x4=64 then break;

   x3:=x3 or x4;

   result:=result+chr(x3);

 end;

end;

/////

function EncodeBase64(Source:string):string;   //base64 解码

var

 Times,LenSrc,i:integer;

 x1,x2,x3,x4:char;

 xt:byte;

begin

 result:='';

 LenSrc:=length(Source);

 if LenSrc mod 3 =0 then Times:=LenSrc div 3

 else Times:=LenSrc div 3 + 1;

 for i:=0 to times-1 do

 begin

   if LenSrc >= (3+i*3) then

   begin

     x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];

     xt:=(ord(Source[1+i*3]) shl 4) and 48;

     xt:=xt or (ord(Source[2+i*3]) shr 4);

     x2:=BaseTable[xt+1];

     xt:=(Ord(Source[2+i*3]) shl 2) and 60;

     xt:=xt or (ord(Source[3+i*3]) shr 6);

     x3:=BaseTable[xt+1];

     xt:=(ord(Source[3+i*3]) and 63);

     x4:=BaseTable[xt+1];

   end

   else if LenSrc>=(2+i*3) then

   begin

     x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];

     xt:=(ord(Source[1+i*3]) shl 4) and 48;

     xt:=xt or (ord(Source[2+i*3]) shr 4);

     x2:=BaseTable[xt+1];

     xt:=(ord(Source[2+i*3]) shl 2) and 60;

     x3:=BaseTable[xt+1];

     x4:='=';

   end else

   begin

     x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];

     xt:=(ord(Source[1+i*3]) shl 4) and 48;

     x2:=BaseTable[xt+1];

     x3:='=';

     x4:='=';

   end;

   result:=result+x1+x2+x3+x4;

 end;

end;

*********************************

unsigned char *base64_encode(const char *str,int length,int *ret_lengt

h)

{

static char 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', '+', '/',

'\0'

};

static char base64_pad = '=';

//const char *current = str;

unsigned const char *current = (unsigned const char*)str;  

int i = 0;

unsigned char *result = (unsigned char *)malloc(((length + 3 - lengt

h % 3) * 4 / 3 + 1) * sizeof(char));

while (length > 2) { /* keep going until we have less than 24 bits *

/

/

result[i++] = base64_table[current[0] >> 2];

result[i++] = base64_table[((current[0] & 0x03) << 4) + (current[1

] >> 4)];

result[i++] = base64_table[((current[1] & 0x0f) << 2) + (current[2

] >> 6)];

result[i++] = base64_table[current[2] & 0x3f];

current += 3;

length -= 3; /* we just handle 3 octets of data */

}

/* now deal with the tail end of things */

if (length != 0) {

result[i++] = base64_table[current[0] >> 2];

if (length > 1) {

result[i++] = base64_table[((current[0] & 0x03 ) << 4) + (curren

t[1] >> 4)];

result[i++] = base64_table[(current[1] & 0x0f) << 2];

result[i++] = base64_pad;

}

else {

result[i++] = base64_table[(current[0] & 0x03) << 4];

result[i++] = base64_pad;

result[i++] = base64_pad;

}

}

if(ret_length) {

*ret_length = i;

}

result[i] = '\0'; // printf("%s\n",result);

return result;

}