请问,base64算法怎么实现的?

最好有源代码,在线等~~~~~~~~~~
---------------------------------------------------------------

#include

 1<stdio.h>   
 2#include <stdlib.h>   
 3#include <string.h>   
 4#include <conio.h> /* Windows special */   
 5char* alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345   
 66789+/=";   
 7char* encode(char data[], int length)   
 8{   
 9int i, index, val;   
10int quad, trip; /* bool type: 0 or 1 */   
11char* out;   
12out = malloc( (((length+2)/3)*4) * sizeof(char) );   
13for (i=0, index=0; i<length; &="" ((i+1)="" ((i+2)="" (int)="" (val="" 0x3f):="" 64)];="" <="" <<="8;" ?="" data[i+1]);="" data[i+2]);="" data[i]);="" i+="3," if="" index+="4)" length)="" out[index+3]="alphabet[(quad" quad="1;" trip="1;" val="" {="" }="" ¦="(0xFF">&gt;= 6;   
14out[index+2] = alphabet[(trip ? (val &amp; 0x3F): 64)];   
15val &gt;&gt;= 6;   
16out[index+1] = alphabet[val &amp; 0x3F];   
17val &gt;&gt;= 6;   
18out[index+0] = alphabet[val &amp; 0x3F];   
19  
20}   
21out[index] = '\0';   
22return out;   
23}   
24void init_codes(char codes[])   
25{   
26int i;   
27for (i = 0; i &lt; 256; i++) codes[i] = -1;   
28for (i = 'A'; i &lt;= 'Z'; i++) codes[i] = i - 'A';   
29for (i = 'a'; i &lt;= 'z'; i++) codes[i] = 26 + i - 'a';   
30for (i = '0'; i &lt;= '9'; i++) codes[i] = 52 + i - '0';   
31codes['+'] = 62;   
32codes['/'] = 63;   
33}   
34char* decode(char data[],int length)   
35{   
36int value, ix;   
37int shift = 0; /* # of excess bits stored in accum */   
38int accum = 0; /* excess bits */   
39int index = 0;   
40int len;   
41char codes[256];   
42char* out;   
43len = ((length + 3) / 4) * 3;   
44if (length&gt;0 &amp;&amp; data[length-1] == '=') --len;   
45if (length&gt;1 &amp;&amp; data[length-2] == '=') --len;   
46printf("%d\n",sizeof(char));   
47out = (char*)malloc(len * sizeof(char));   
48init_codes(codes);   
49for (ix=0; ix<length; &="" (="" *="" 0xff="" ];="" byte="" char="" data[ix]="" high="" if="" ignore="" ix++)="" of="" value="" {="">= 0 ) { /* skip over non-code */   
50accum &lt;&lt;= 6; /* bits shift up by 6 each time thru */   
51shift += 6; /* loop, with new bits being put in */   
52accum ¦= value; /* at the bottom. */   
53if ( shift &gt;= 8 ) { /* whenever there are 8 or more shifted   
54in, */   
55shift -= 8; /* write them out (from the top, leaving   
56any */   
57out[index++] = /* excess at the bottom for next iterati   
58on. */   
59((accum &gt;&gt; shift) &amp; 0xff);   
60}   
61}   
62}   
63out[index] = '\0';   
64if (index != len) printf("miscalculated data length!\n");   
65return out;   
66}   
67int main()   
68{   
69char* data1 = "Z3Vlc3Q6cGFzc3dvcmQ=";   
70char* data2 = "guest:password";   
71char* out1;   
72char* out2;   
73out1 = decode(data1,strlen(data1));   
74out2 = encode(data2,strlen(data2));   
75printf("[%s]\n[%s]\n",out1,out2);   
76getch(); /* Windows special */   
77}</length;></length;></conio.h></string.h></stdlib.h></stdio.h>
Published At
Categories with Web编程
comments powered by Disqus