C ++ Base64 encoding class

Source: Internet
Author: User

 

# Include <string>

 

Using namespace std;

 

Const static class CConvert {

Public:

Static void _ enBase64Help (unsigned char chasc [3], unsigned char chuue [4]);

Static void _ deBase64Help (unsigned char chuue [4], unsigned char chasc [3]);

Static string enBase64 (const char * inbuf, size_t inbufLen );

Static string enBase64 (const string & inbuf );

Static int deBase64 (string src, char * outbuf );

Static string deBase64 (string src );

};

 

 

# Include "stdafx. h"

# Include "Convert. h"

Void CConvert: _ enBase64Help (unsigned char chasc [3], unsigned char chuue [4])

{

Int I, k = 2;

Unsigned char t = 0;

For (I = 0; I <3; I ++)

{

* (Chuue + I) = * (chasc + I)> k;

* (Chuue + I) | = t;

T = * (chasc + I) <(8-k );

T> = 2;

K + = 2;

}

* (Chuue + 3) = * (chasc + 2) & 63;

For (I = 0; I <4; I ++ ){

If (* (chuue + I) <= 128) & (* (chuue + I) <= 25 )){

* (Chuue + I) + = 65; // 'a'-'Z'

} Else if (* (chuue + I)> = 26) & (* (chuue + I) <= 51 )){

* (Chuue + I) + = 71; // 'a'-'Z'

} Else if (* (chuue + I)> = 52) & (* (chuue + I) <= 61 )){

* (Chuue + I)-= 4; // 0-9

} Else if (* (chuue + I) = 62 ){

* (Chuue + I) = 43; // +

} Else if (* (chuue + I) = 63 ){

* (Chuue + I) = 47 ;///

}

}

}

Void CConvert: _ deBase64Help (unsigned char chuue [4], unsigned char chasc [3]) {

Int I, k = 2;

Unsigned char t = 0;

 

For (I = 0; I <4; I ++ ){

If (* (chuue + I)> = 65) & (* (chuue + I) <= 90 ))

* (Chuue + I)-= 65; // 'a'-'Z'-> 0-25

Else if (* (chuue + I)> = 97) & (* (chuue + I) <= 122 ))

* (Chuue + I)-= 71; // 'a'-'Z'-> 26-51

Else if (* (chuue + I)> = 48) & (* (chuue + I) <= 57 ))

* (Chuue + I) + = 4; // '0'-'9'-> 52-61

Else if (* (chuue + I) = 43)

* (Chuue + I) = 62; // +-> 62

Else if (* (chuue + I) = 47)

* (Chuue + I) = 63; //-> 63

Else if (* (chuue + I) = 61)

* (Chuue + I) = 0; // =-> 0 Note: Both 'A' and '=' correspond to 0.

}

For (I = 0; I <3; I ++ ){

* (Chasc + I) = * (chuue + I) <k;

K + = 2;

T = * (chuue + I + 1)> (8-k );

* (Chasc + I) | = t;

}

}

 

String CConvert: enBase64 (const char * inbuf, size_t inbufLen ){

String outStr;

Unsigned char in [8];

Unsigned char out [8];

Out [4] = 0;

Size_t blocks = inbufLen/3;

For (size_t I = 0; I <blocks; I ++ ){

In [0] = inbuf [I * 3];

In [1] = inbuf [I * 3 + 1];

In [2] = inbuf [I * 3 + 2];

_ EnBase64Help (in, out );

OutStr + = out [0];

OutStr + = out [1];

OutStr + = out [2];

OutStr + = out [3];

}

If (inbufLen % 3 = 1 ){

In [0] = inbuf [inbufLen-1];

In [1] = 0;

In [2] = 0;

_ EnBase64Help (in, out );

OutStr + = out [0];

OutStr + = out [1];

OutStr + = ';

OutStr + = ';

} Else if (inbufLen % 3 = 2 ){

In [0] = inbuf [inbufLen-2];

In [1] = inbuf [inbufLen-1];

In [2] = 0;

_ EnBase64Help (in, out );

OutStr + = out [0];

OutStr + = out [1];

OutStr + = out [2];

OutStr + = ';

}

Return string (outStr );

}

 

String CConvert: enBase64 (const string & inbuf)

{

Return CConvert: enBase64 (inbuf. c_str (), inbuf. size ());

}

 

Int CConvert: deBase64 (string src, char * outbuf ){

 

// Break when the incoming base64 coding is wrong

If (src. size () % 4 )! = 0)

{

Return 0;

}

 

Unsigned char in [4];

Unsigned char out [3];

 

Size_t blocks = src. size ()/4;

For (size_t I = 0; I <blocks; I ++ ){

In [0] = src [I * 4];

In [1] = src [I * 4 + 1];

In [2] = src [I * 4 + 2];

In [3] = src [I * 4 + 3];

_ DeBase64Help (in, out );

Outbuf [I * 3] = out [0];

Outbuf [I * 3 + 1] = out [1];

Outbuf [I * 3 + 2] = out [2];

}

Int length = src. size ()/4*3;

If (src [src. size ()-1] = '){

Length --;

If (src [src. size ()-2] = '){

Length --;

}

}

Return length;

}

String CConvert: deBase64 (string src)

{

Char * buf = new char [src. length () * 2];

Int len = deBase64 (src, buf );

Buf [len] = '\ 0 ';

String result = string (buf, len );

Delete [] buf;

Return result;

}

From the column moon5284

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.