當前位置:編程學習大全網 - 編程語言 - C語言編程:編寫壹個函數base64加密

C語言編程:編寫壹個函數base64加密

#include?<stdio.h>

#include?<stdlib.h>

#include?<string.h>

const?char?*chlist?=?"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int?encode_string(char*?str,?unsigned?int?length,?char*?stat)?{

char?s[103];

int?i,j;

unsigned?temp;

if(length?<=?0)?return?1;

if(length?>?100)?return?2;

str[length]?=?'\0';

strcpy(s,str);

while(strlen(s)?%?3)?strcat(s,"=");

for(i?=?0,j?=?0;?s[i];?i?+=?3,j?+=?4)?{

temp?=?s[i];

temp?=?(temp?<<?8)?+?s[i?+?1];

temp?=?(temp?<<?8)?+?s[i?+?2];

stat[j?+?3]?=?chlist[temp?&?0X3F];

temp?>>=?6;

stat[j?+?2]?=?chlist[temp?&?0X3F];

temp?>>=?6;

stat[j?+?1]?=?chlist[temp?&?0X3F];

temp?>>=?6;

stat[j?+?0]?=?chlist[temp?&?0X3F];

}

stat[j]?=?'\0';

return?0;

}

int?Index(char?ch)?{

int?i;

for(i?=?0;?chlist[i];?++i)?{

if(chlist[i]?==?ch)

return?i;

}

return?-1;

}

void?decode_string(char?*s,?char?*t)?{

unsigned?temp;

int?i,j,k,len?=?strlen(s);

if(len?%?4)?{

printf("無效數據。\n");

exit(2);

}

for(i?=?0,j?=?0;?i?<=?len;?i?+=?4,j?+=?3)?{

temp?=?0;

for(k?=?0;?k?<?4;?++k)

temp?=?(temp?<<?6)?+?Index(s[i?+?k]);

for(k?=?2;?k?>=?0;?--k)?{

t[j?+?k]?=?temp?&?0XFF;

temp?>>=?8;

}

}

t[j?+?k]?=?'\0';

}

int?main()?{

char?s[100]?=?"1a2a3s4dff5fj6u7M8B9P0O1U2";

char?t[150],u[100];

printf("s?=?%s\n",s);

encode_string(s,strlen(s),t);

printf("t?=?%s\n",t);

decode_string(t,u);

printf("u?=?%s\n",u);

return?0;

}

  • 上一篇:浜斿勾绱氳▓绠楁绶ㄧ▼鍒跺湒
  • 下一篇:RPA是什麽意思?什麽是RPA機器人?
  • copyright 2024編程學習大全網