Base64 encoding and decoding in C Language

Source: Internet
Author: User
Tags printable characters rfc

Base64 Is based on 64 Printable characters to represent the representation of binary data. Because 26 = 64 , So every 6 It is a unit that corresponds to a printable character. Three bytes in total 24 Bit, corresponding 4 Items Base64 Unit, that is 3 Bytes 4 Printable characters. It is often used as the transmission code of emails. In Base64 The printable characters include uppercase letters.A-Z , Lowercase letters A-z Arabic numerals 0-9 . 62 Characters, and the two printable symbols are different in different systems. Generally, the plus sign ( + ) And forward slash ( / ). Plus the "completion symbol", usually with the equal sign ( = ).

complete base64 definition visibility RFC 1421 and RFC 2045 . The encoded data is slightly longer than the original data, which is the original 4/3 . In the email, according to RFC 822 , each 76 character must contain a carriage return and line break. It can be estimated that the length of the encoded data is about 135.1% .

Base64When encoding, put three pieces of data into one24In the buffer, the first-come buffer occupies a high position. Insufficient data3Bytes, the remaining bits in the buffer are used0Complement. Then, retrieve 6 at a time (because26 = 64) Bit, select according to its valueAbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 +/As the encoded output. Continue until all input data is converted. If the last two inputs are left, add one"="; If the last input data is left, the encoding result is followed by two"="; If there is no data left, do not add anything. This ensures the correctness of data restoration.

CLanguageSource codeAs follows:

 /* ** Base64 encoding and decoding implementation * C language sourceCode** Ye Jianfei ** instructions for use: * command line parameter description: if the "-d" parameter exists, it is base64 decoded; otherwise, it is base64 encoded. * The input comes from the standard input stdin, and the output is the standard output stdout. You can redirect input and output streams. ** Base64 encoding: enter any binary stream and read it until the file is fully read (the keyboard input ends with a file Terminator ). * Outputs the base64 encoding of plain text. ** Base64 decoding: base64 encoding of the input plain text. It can be read until the file is fully read (the keyboard input ends with a file Terminator ). * Output the original binary stream. *  */  # Include <Stdio. h> # Include <Stdlib. h> # Include <String. h> 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 ++] = (UnsignedChar ) (Unsigned Char ) (Temp [ 2 ] < 6 ))& 0xf0 ) | (Unsigned  Char ) (Temp [ 3 ] & 0x3f  ));}  Return  J ;}  Int Main ( Int Argc,Char * Argv []) {  Int  I; unsigned  Char Bindata [ 2050  ];  Char Base64 [ 4096  ]; Size_t bytes;  If (Argc = 1  ){  //  Encode         While (! Feof (stdin) {bytes = Fread (bindata, 1 , 2049  , Stdin); base64_encode (bindata, base64, bytes); printf (  "  % S  "  , Base64 );}}  Else   If (Argc = 2 &&! Strcmp (argv [ 1 ],"  -D  "  )){  //  Decode          While (! Feof (stdin )){  For (I = 0 ; I < 2048 ; I ++ ) {Base64 [I] = Getchar ();  If (Base64 [I] =EOF)  Break  ;  Else   If (Base64 [I] = '  \ N  ' | Base64 [I] = '  \ R  '  ) I -- ;} Bytes = Base64_decode (base64, bindata); fwrite (bindata, bytes, 1  , Stdout );}}  Else  {Fprintf (stderr,  "  Usage: % s [-D] \ n \ t-d \ tdecode data \ n  " , Argv [ 0  ]);  Return  Exit_failure ;}  Return  Exit_success ;} 

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.