BASE64演算法實現

2021-04-22 00:08:28 字數 1880 閱讀 2237

cstring base64encode(lpctstr lpszsrc)

;// 56 - 63

unsigned int itest;

lpctstr pinbuffer=lpszsrc;

int nsize = (int)_tcslen(lpszsrc);

char* poutbuffer=new char[nsize/3*4+5];

zeromemory(poutbuffer,nsize/3*4+5);

for(uint i=0;i<_tcslen(lpszsrc) / 3;i++)

//設定尾部

switch (_tcslen(lpszsrc) % 3)

poutbuffer-=nsize/3*4;

cstring strencode=poutbuffer;

delete poutbuffer;

poutbuffer=null;

return strencode;

}cstring base64decode(lpctstr lpszsrc)

;const int nsrccount=(int)_tcslen(lpszsrc);

int nsize=nsrccount/4*3;

if(lpszsrc[nsrccount-1]=='=')

nsize--;

if(lpszsrc[nsrccount-2]=='=')

nsize--;

char* poutbuffer=new char[nsize+3];

zeromemory(poutbuffer,nsize+3);

lpctstr pinbuffer=lpszsrc;

uint itest,ipack;

for(int i=0;i> 8;

poutbuffer[1] = ipack;

ipack = ipack >> 8;

poutbuffer[0] = ipack;

//準備寫入後3位

poutbuffer+= 3; ipack = 0;

}switch(nsize%3)

itest = base64_decode_table[*pinbuffer++]; // read from inputbuffer.

if (itest != 0xff)

ipack = ipack >> 4;

poutbuffer[0] = ipack;

poutbuffer++;

break;

case 2:

itest = base64_decode_table[*pinbuffer++]; // read from inputbuffer.

if (itest != 0xff)

itest = base64_decode_table[*pinbuffer++]; // read from inputbuffer.

if (itest != 0xff)

itest = base64_decode_table[*pinbuffer++]; // read from inputbuffer.

if (itest != 0xff)

ipack = ipack >> 2;

poutbuffer[1] = ipack;

ipack = ipack >> 8;

poutbuffer[0] = ipack;

poutbuffer+=2;

break;

default:

break;

}poutbuffer-=nsize;

cstring strdecode=poutbuffer;

delete poutbuffer;

return strdecode;

}

用C 實現Base64演算法

using system using system.text namespace common catch return encode base64加密,採用utf8編碼方式加密 待加密的明文 加密後的字串 public static string encodebase64 string sourc...

Base64編碼演算法實現 2

演算法詳解 base64編碼要求把3個8位位元組 3 8 24 轉化為4個6位的位元組 4 6 24 之後在6位的前面補兩個0,形成8位乙個位元組的形式。具體轉化形式間下圖 字串 張3 11010101 11000101 00110011 00110101 00011100 00010100 001...

用C 實現Base64演算法

using system using system.text namespace common catch return encode base64加密,採用utf8編碼方式加密 待加密的明文 加密後的字串 public static string encodebase64 string sourc...