#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> /* Windows special */
char* alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345
6789+/=";
char* encode(char data[], int length)
{
int i, index, val;
int quad, trip; /* bool type: 0 or 1 */
char* out;
out = malloc( (((length+2)/3)*4) * sizeof(char) );
for (i=0, index=0; i<length; i+=3, index+=4) {
quad = 0;
trip = 0;
val = (0xFF & (int) data[i]);
val <<= 8;
if ((i+1) < length) {
val |= (0xFF & (int) data[i+1]);
trip = 1;
}
val <<= 8;
if ((i+2) < length) {
val |= (0xFF & (int) data[i+2]);
quad = 1;
}
out[index+3] = alphabet[(quad ? (val & 0x3F): 64)];
val >>= 6;
out[index+2] = alphabet[(trip ? (val & 0x3F): 64)];
val >>= 6;
out[index+1] = alphabet[val & 0x3F];
val >>= 6;
out[index+0] = alphabet[val & 0x3F]; }
out[index] = '\0';
return out;
}
void init_codes(char codes[])
{
int i;
for (i = 0; i < 256; i++) codes[i] = -1;
for (i = 'A'; i <= 'Z'; i++) codes[i] = i - 'A';
for (i = 'a'; i <= 'z'; i++) codes[i] = 26 + i - 'a';
for (i = '0'; i <= '9'; i++) codes[i] = 52 + i - '0';
codes['+'] = 62;
codes['/'] = 63;
}
char* decode(char data[],int length)
{
int value, ix;
int shift = 0; /* # of excess bits stored in accum */
int accum = 0; /* excess bits */
int index = 0;
int len;
char codes[256];
char* out;
len = ((length + 3) / 4) * 3;
if (length>0 && data[length-1] == '=') --len;
if (length>1 && data[length-2] == '=') --len;
printf("%d\n",sizeof(char));
out = (char*)malloc(len * sizeof(char));
init_codes(codes);
for (ix=0; ix<length; ix++) {
value = codes[ data[ix] & 0xFF ]; /* ignore high byte of char */
if ( value >= 0 ) { /* skip over non-code */
accum <<= 6; /* bits shift up by 6 each time thru */
shift += 6; /* loop, with new bits being put in */
accum |= value; /* at the bottom. */
if ( shift >= 8 ) { /* whenever there are 8 or more shifted
in, */
shift -= 8; /* write them out (from the top, leaving
any */
out[index++] = /* excess at the bottom for next iterati
on. */
((accum >> shift) & 0xff);
}
}
}
out[index] = '\0';
if (index != len) printf("miscalculated data length!\n");
return out;
}
int main()
{
char* data1 = "Z3Vlc3Q6cGFzc3dvcmQ=";
char* data2 = "guest:password";
char* out1;
char* out2;
out1 = decode(data1,strlen(data1));
out2 = encode(data2,strlen(data2));
printf("[%s]\n[%s]\n",out1,out2);
getch(); /* Windows special */
}
#include <stdlib.h>
#include <string.h>
#include <conio.h> /* Windows special */
char* alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345
6789+/=";
char* encode(char data[], int length)
{
int i, index, val;
int quad, trip; /* bool type: 0 or 1 */
char* out;
out = malloc( (((length+2)/3)*4) * sizeof(char) );
for (i=0, index=0; i<length; i+=3, index+=4) {
quad = 0;
trip = 0;
val = (0xFF & (int) data[i]);
val <<= 8;
if ((i+1) < length) {
val |= (0xFF & (int) data[i+1]);
trip = 1;
}
val <<= 8;
if ((i+2) < length) {
val |= (0xFF & (int) data[i+2]);
quad = 1;
}
out[index+3] = alphabet[(quad ? (val & 0x3F): 64)];
val >>= 6;
out[index+2] = alphabet[(trip ? (val & 0x3F): 64)];
val >>= 6;
out[index+1] = alphabet[val & 0x3F];
val >>= 6;
out[index+0] = alphabet[val & 0x3F]; }
out[index] = '\0';
return out;
}
void init_codes(char codes[])
{
int i;
for (i = 0; i < 256; i++) codes[i] = -1;
for (i = 'A'; i <= 'Z'; i++) codes[i] = i - 'A';
for (i = 'a'; i <= 'z'; i++) codes[i] = 26 + i - 'a';
for (i = '0'; i <= '9'; i++) codes[i] = 52 + i - '0';
codes['+'] = 62;
codes['/'] = 63;
}
char* decode(char data[],int length)
{
int value, ix;
int shift = 0; /* # of excess bits stored in accum */
int accum = 0; /* excess bits */
int index = 0;
int len;
char codes[256];
char* out;
len = ((length + 3) / 4) * 3;
if (length>0 && data[length-1] == '=') --len;
if (length>1 && data[length-2] == '=') --len;
printf("%d\n",sizeof(char));
out = (char*)malloc(len * sizeof(char));
init_codes(codes);
for (ix=0; ix<length; ix++) {
value = codes[ data[ix] & 0xFF ]; /* ignore high byte of char */
if ( value >= 0 ) { /* skip over non-code */
accum <<= 6; /* bits shift up by 6 each time thru */
shift += 6; /* loop, with new bits being put in */
accum |= value; /* at the bottom. */
if ( shift >= 8 ) { /* whenever there are 8 or more shifted
in, */
shift -= 8; /* write them out (from the top, leaving
any */
out[index++] = /* excess at the bottom for next iterati
on. */
((accum >> shift) & 0xff);
}
}
}
out[index] = '\0';
if (index != len) printf("miscalculated data length!\n");
return out;
}
int main()
{
char* data1 = "Z3Vlc3Q6cGFzc3dvcmQ=";
char* data2 = "guest:password";
char* out1;
char* out2;
out1 = decode(data1,strlen(data1));
out2 = encode(data2,strlen(data2));
printf("[%s]\n[%s]\n",out1,out2);
getch(); /* Windows special */
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货