BASE64 encoding and decoding

Source: Internet
Author: User
Tags printable characters

Base64 This is a binary encoding method that is converted to printable characters. Mainly used for mail transmission.

Base64 will be 64 characters (a-z,a-z,0-9,+,/) due to the basic character set. Converts all symbols into a character set.

Coding:
The encoding is converted to 4 bytes per 3 knots. If the input byte number is not a multiple of 3. The 0-byte padding at the end makes it a multiple of 3.

for 3 bytes. Remove 6 bits at a time and add 2 bits 0 to the front to make a byte. This byte is the subscript to find the Base64 code table (for example,) to output the corresponding characters.

Convert 3 bytes to 4 bytes (3*8=4*6) at a time until you get the encoding result for the entire input string. Finally, if a 0 byte is added to the input before, the last 1 bytes of the output will be replaced with "=", and if 2 0 bytes are added, the last 2 bytes of the output result will be replaced with "=".
Decoding:
Base64 decoding is the inverse of the encoding process, note that the decoding of each byte of the first 2 bits 0 is artificially populated. The latter 6 bits are the valid bits. Each time a 24-bit significant bit in 4 bytes is extracted to make up 3 bytes.



The core of the encoding and decoding process is bit manipulation.

Base64 Code Table

Code:

#include <stdio.h> #include <stdlib.h> #include <string.h>const char table[] = " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; int FindIndex (char c) {for (int i=0;i<64;i++) {if (C==table[i]) return i;} return-1;} void Base64enc (const CHAR*SRC,CHAR*&AMP;DST) {int Slen=strlen (SRC); int len=slen;if (len%3!=0) len= (len/3+1) *3;// The length is multiplied by 3 unsigned char*s= (unsigned char*) malloc (len*sizeof (unsigned char));//unsigned char is used here, otherwise the Chinese input memset cannot be processed (s,0 , Len); memcpy (s,src,slen); int dlen= (LEN/3) *4+1;dst= (char*) malloc (dlen*sizeof (char)); for (int i=0,j=0;i<len;i+=3 ) {//Core step//**************************************dst[j]=table[s[i]>>2];d st[j+1]=table[((s[i]&0X03) &LT;&LT;4) | (s[i+1]>>4)]; dst[j+2]=table[((s[i+1]&0x0f) <<2) | ( S[I+2]&GT;&GT;6)];d st[j+3]=table[s[i+2]&0x3f];//**************************************j+=4;} Free (s); if (slen%3==1) dst[dlen-2]=dst[dlen-3]= ' = '; if (slen%3==2) dst[dlen-2]= ' = ';d st[dlen-1]=0;} void Base64dec (const CHAR*SRC,CHAR*&AMP;DST) {int SLEn=strlen (SRC); if (slen%4!=0) return; int dlen= (SLEN/4) *3+1;char*d= (char*) malloc (dlen*sizeof (char)); int index[4]; for (int i=0,j=0;i<slen;i+=4) {for (int k=0;k<4;k++) {index[k]=findindex (src[i+k]);} Core Step//**************************************d[j]=index[0]<<2|index[1]>>4;d[j+1]=index[1]<< 4|index[2]>>2;d[j+2]=index[2]<<6|index[3];//**************************************j+=3;} Remove the effect of the src tail ' = ' if (src[slen-1]== ' = ' &&src[slen]!= ' = ') dlen-=1;if (src[slen-1]== ' = ' &&src[slen]== ' = ') ) dlen-=2;dst= (char*) malloc (dlen*sizeof (char)); memcpy (Dst,d,dlen); free (d);d st[dlen-1]=0;} int main () {char src[]= "Hello, Little Luo"; Char*enc,*dec;base64enc (Src,enc); Base64dec (Enc,dec);p rintf ("%s\n", SRC);p rintf (" %s\n ", enc);p rintf ("%s\n ", DEC); free (enc); free (dec); return 0;}



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

BASE64 encoding and decoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.