[crypto]-50-base64_encode和base64_decode的C语言实现
base64_encode和base64_decode的C語言實現
方法一(笨方法):
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";char * base64_encode( const unsigned char * bindata, char * base64, int binlength ) {int i, j;unsigned char current;for ( i = 0, j = 0 ; i < binlength ; i += 3 ){current = (bindata[i] >> 2) ;current &= (unsigned char)0x3F;base64[j++] = base64char[(int)current];current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;if ( i + 1 >= binlength ){base64[j++] = base64char[(int)current];base64[j++] = '=';base64[j++] = '=';break;}current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );base64[j++] = base64char[(int)current];current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;if ( i + 2 >= binlength ){base64[j++] = base64char[(int)current];base64[j++] = '=';break;}current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );base64[j++] = base64char[(int)current];current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;base64[j++] = base64char[(int)current];}base64[j] = '\0';return base64; }int base64_decode( const char * base64, unsigned char * bindata ) {int i, j;unsigned char k;unsigned char temp[4];for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 ){memset( temp, 0xFF, sizeof(temp) );for ( k = 0 ; k < 64 ; k ++ ){if ( base64char[k] == base64[i] )temp[0]= k;}for ( k = 0 ; k < 64 ; k ++ ){if ( base64char[k] == base64[i+1] )temp[1]= k;}for ( k = 0 ; k < 64 ; k ++ ){if ( base64char[k] == base64[i+2] )temp[2]= k;}for ( k = 0 ; k < 64 ; k ++ ){if ( base64char[k] == base64[i+3] )temp[3]= k;}bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |((unsigned char)((unsigned char)(temp[1]>>4)&0x03));if ( base64[i+2] == '=' )break;bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));if ( base64[i+3] == '=' )break;bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |((unsigned char)(temp[3]&0x3F));}return j; }方法二:
static const unsigned char base64_suffix_map[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,62, // '+'0, 0, 0,63, // '/'52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'0, 0, 0, 0, 0, 0, 0,0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'0, 0, 0, 0, 0, 0,26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z' };int base64_decode(char *indata, uint32_t inlen, char *outdata, uint32_t *outlen) {int ret = 0;int t = 0, x = 0, y = 0, i = 0;unsigned char c = 0;int g = 3;if (indata == NULL || inlen <= 0 || outdata == NULL || outlen == NULL) {return ret = -1;}if (inlen % 4 != 0) {return ret = -1;}while (indata[x] != 0) {c = base64_suffix_map[(uint8_t)indata[x++]];if (c == 255) return -1;if (c == 253) continue;if (c == 254) { c = 0; g--; }t = (t<<6) | c;if (++y == 4) {outdata[i++] = (unsigned char)((t>>16)&0xff);if (g > 1) outdata[i++] = (unsigned char)((t>>8)&0xff);if (g > 2) outdata[i++] = (unsigned char)(t&0xff);y = t = 0;}}if (outlen != NULL) {*outlen = i;}return ret; }相關推薦:
?????????[crypto]-01-對稱加解密AES原理概念詳解
?????????[crypto]-02-非對稱加解密RSA原理概念詳解
?????????[crypto]-03-數字摘要HASH原理概念詳解
?????????[crypto]-04-國產密碼算法(國密算法sm2/sm3/sm4)介紹
?????????[crypto]-05-轉載:PKCS #1 RSA Encryption Version 1.5介紹
?????????[crypto]-05.1-PKCS PKCS#1 PKCS#7 PKCS#11的介紹
?????????[crypto]-06-CA證書介紹和使用方法
?????????[crypto]-30-The Armv8 Cryptographic Extension在linux中的應用
?????????[crypto]-31-crypto engion的學習和總結
?????????[crypto]-50-base64_encode和base64_decode的C語言實現
?????????[crypto]-51-RSA私鑰pem轉換成der, 在將der解析出n e d p q dp dq qp
?????????[crypto]-52-python3中rsa(簽名驗簽加密解密)aes(ecb cbc ctr)hmac的使用,以及unittest測試用
?????????[crypto]-53-openssl命令行的使用(aes/rsa簽名校驗/rsa加密解密/hmac)
?????????[crypto]-90-crypto的一些術語和思考
總結
以上是生活随笔為你收集整理的[crypto]-50-base64_encode和base64_decode的C语言实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一图弄懂ARM中都有什么
- 下一篇: [crypto]-01-对称加解密AES