Base64 C ++ implementation

Source: Internet
Author: User
  1. Overview
    Base64 can convert non-printable encoding into printable encoding for storage.
    For example, HTTP generally transmits text messages. If you want to transmit images, you must first encode the images into base64 messages for transmission, and then when the client displays them, decodes base64 messages and converts them to binary image data for display.
    The source code is provided below, which can be compiled directly in Linux and msvc environments.
  2. Code
    The base64.h code is as follows:
    /** <Br/> * (c) Copyright 2009, asiaInfo <br/> * @ version V1.0 <br/> * @ author Chenli <br/> * @ brief base64 encoding and decoding <br/> * history: <br/> * <p> Chenli 2009-02-17 1.0 Build this Moudle </P> <br/> */</P> <p> # ifndef ___ base64_h ___ <br/> # define ___ base64_h ___ </P> <p> # include <string> </P> <p> using namespace STD; </P> <p> class cbase64 <br/>{< br/> Public: <br/> cbase64 (); <br/> ~ Cbase64 (); </P> <p> /******************************** * *********************** <br/> * function description: encode the input data in base64 format. <br/> * parameter description: [in] data to be encoded by pin <br/> [in] uinlen input Parameter bytes <br/> [out] string output after base64 encoding <br /> * return value: true: Processing succeeded. False: <br/> * OPERATOR: Chenli <br/> * Writing Time: <br/> *********************************** * *********************/<br/> bool static encode (const unsigned char * pin, unsigned long uinlen, string & Strout ); </P> <p> /******************************** * *********************** <br/> * function description: encode the input data in base64 format. <br/> * parameter description: [in] data to be encoded by pin <br/> [in] uinlen input Parameter bytes <br/> [out] pout output base64-encoded string <br /> [out] the base64 encoded String Length output by uoutlen <br/> * return value: true: Processing succeeded. False: <br/> * OPERATOR: Chenli <br/> * Writing Time: <br/> *********************************** * *********************/<br/> bool static encode (const unsigned char * pin, unsigned long uinlen, unsigned char * pout, unsigned long * uoutlen ); </P> <p> /******************************** * *********************** <br/> * function description: base64 Decoding of input data <br/> * parameter description: [in] data to be decoded by strin <br/> [out] pout outputs decoded data in the number of bytes <br/> [out] length of the decoded bytes output by uoutlen <br/> * return value: true: Processing succeeded. False: <br/> * OPERATOR: Chenli <br/> * Writing Time: <br/> *********************************** * *********************/<br/> bool static decode (const string & strin, unsigned char * pout, unsigned long * uoutlen ); </P> <p> /******************************** * *********************** <br/> * function description: base64 Decoding of input data <br/> * parameter description: [in] data to be decoded by strin <br/> [out] pout outputs decoded data in the number of bytes <br/> [out] length of the decoded bytes output by uoutlen <br/> * return value: true: Processing succeeded. False: <br/> * OPERATOR: Chenli <br/> * Writing Time: <br/> *********************************** * *********************/<br/> bool static decode (const unsigned char * pin, unsigned long uinlen, unsigned char * pout, unsigned long * uoutlen); <br/>}; </P> <p> # endif/___ base64_h ___ <br/>
    The base64.cpp code is as follows:
    /* <Br/> * base64? <Br/> */</P> <p> # include "base64.h" </P> <p> static const char * g_pcodes = <br/> "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 +/= "; </P> <p> static const unsigned char g_pmap [256] = <br/>{< br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255, 255,255,255, <br/> 255,255,255,255,255,255,255, 62,255,255,255, 63, <br/> 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255, <br/> 255,254,255,255,255, 0, 1, 2, 3, 4, 5, 6, <br/> 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, <br/> 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, <br/> 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, <br/> 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, <br/> 49, 50, 51,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 25 5,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255,255,255,255,255,255,255,255,255, <br/> 255,255,255,255 <br/> }; </P> <p> cbase64: cbase64 () <br/>{< br/>}</P> <p> cbase 64 ::~ Cbase64 () <br/>{< br/>}</P> <p> bool cbase64: encode (const unsigned char * pin, unsigned long uinlen, unsigned char * pout, unsigned long * uoutlen) <br/>{< br/> unsigned long I, len2, leven; <br/> unsigned char * P; </P> <p> If (pout = NULL | * uoutlen = 0) <br/> return false; </P> <p> // assert (pin! = NULL) & (uinlen! = 0) & (pout! = NULL) & (uoutlen! = NULL); </P> <p> len2 = (uinlen + 2)/3) <2; <br/> If (* uoutlen) <(len2 + 1) return false; </P> <p> P = pout; <br/> Leven = 3*(uinlen/3 ); <br/> for (I = 0; I <leven; I + = 3) <br/>{< br/> * P ++ = g_pcodes [pin [0]> 2]; <br/> * P ++ = g_pcodes [(PIN [0] & 3) <4) + (PIN [1]> 4)]; <br/> * P ++ = g_pcodes [(PIN [1] & 0xf) <2) + (PIN [2]> 6)]; <br/> * P ++ = g_pcodes [pin [2] & 0x3f]; <br/> pin + = 3; <br/>}</P> <P> if (I <uinlen) <br/>{< br/> unsigned char a = pin [0]; <br/> unsigned char B = (I + 1) <uinlen )? Pin [1]: 0; <br/> unsigned char C = 0; </P> <p> * P ++ = g_pcodes [A> 2]; <br/> * P ++ = g_pcodes [(A & 3) <4) + (B> 4)]; <br/> * P ++ = (I + 1) <uinlen )? G_pcodes [(B & 0xf) <2) + (C> 6)]: '='; <br/> * P ++ = '; <br/>}</P> <p> * p = 0; // append NULL byte <br/> * uoutlen = p-Pout; <br/> return true; <br/>}</P> <p> bool cbase64: encode (const unsigned char * pin, unsigned long uinlen, string & Strout) <br/>{< br/> unsigned long I, len2, leven; <br/> Strout = ""; </P> <p> // assert (pin! = NULL) & (uinlen! = 0) & (pout! = NULL) & (uoutlen! = NULL); </P> <p> len2 = (uinlen + 2)/3) <2; <br/> // If (* uoutlen) <(len2 + 1) return false; </P> <p> // P = pout; <br/> Leven = 3*(uinlen/3 ); <br/> for (I = 0; I <leven; I + = 3) <br/>{< br/> Strout + = g_pcodes [pin [0]> 2]; <br/> Strout + = g_pcodes [(PIN [0] & 3) <4) + (PIN [1]> 4)]; <br/> Strout + = g_pcodes [(PIN [1] & 0xf) <2) + (PIN [2]> 6)]; <br/> Strout + = g_pcodes [pin [2] & 0x3f]; <br/> pin + = 3; <br/>}</P> <p> if (I <uinlen) <br/>{< br/> unsigned char a = pin [0]; <br/> unsigned char B = (I + 1) <uinlen )? Pin [1]: 0; <br/> unsigned char C = 0; </P> <p> Strout + = g_pcodes [A> 2]; <br/> Strout + = g_pcodes [(A & 3) <4) + (B> 4)]; <br/> Strout + = (I + 1) <uinlen )? G_pcodes [(B & 0xf) <2) + (C> 6)]: '='; <br/> Strout + = '; <br/>}</P> <p> // * p = 0; // append NULL byte <br/> // * uoutlen = p-Pout; <br/> return true; <br/>}</P> <p> bool cbase64: Decode (const string & strin, unsigned char * pout, unsigned long * uoutlen) <br/>{< br/> unsigned long t, x, y, z; <br/> unsigned char C; <br/> unsigned long G = 3; </P> <p> // assert (pin! = NULL) & (uinlen! = 0) & (pout! = NULL) & (uoutlen! = NULL); </P> <p> for (x = y = z = T = 0; x <strin. length (); X ++) <br/>{< br/> C = g_pmap [strin [x]; <br/> If (C = 255) continue; <br/> If (C = 254) {c = 0; G -- ;}</P> <p> T = (T <6) | C; </P> <p> If (++ y = 4) <br/> {<br/> If (Z + g)> * uoutlen) {return false;} // Buffer Overflow <br/> pout [Z ++] = (unsigned char) (T> 16) & 255 ); <br/> If (G> 1) pout [Z ++] = (unsigned char) (T> 8) & 255 ); <br/> If (G> 2) pout [Z ++] = (unsigned char) (T & 255); <br/> Y = T = 0; <br/>}</P> <p> * uoutlen = z; <br/> return true; <br/>}< br/>

    There is a base64 Algorithm on it, and a program is written below for testing.
    The content of the Main. cpp file is as follows:
    # Include "base64.h" </P> <p> # include <iostream> </P> <p> using namespace STD; </P> <p> int main () <br/>{< br/> unsigned long Len = 10; </P> <p> unsigned char pin [100]; <br/> unsigned char pout [100]; </P> <p> memcpy (pin, "hello", 5); </P> <p> string Strout; </P> <p> cout <(char *) PIN <Endl; <br/> If (cbase64: encode (pin, 4, Strout )) <br/> cout <Strout <Endl; <br/> else <br/> cout <"encryption failed" <Endl; </P> <p> string stroin = Strout; <br/> cout <stroin <Endl; </P> <p> memset (pout, 0, sizeof (pout); </P> <p> If (cbase64: Decode (stroin, pout, & Len )) <br/> {<br/> cout <(char *) pout <Endl; <br/> cout <"Len =" <strlen (char *) pout) <Endl; <br/>}< br/> else <br/> cout <"decryption failed" <Endl; </P> <p> cout <Len <Endl; </P> <p> return 0; <br/>}

    The above code can be directly compiled to run.

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.