Encryption Algorithm (II) -- base64

Source: Internet
Author: User
Tags printable characters

Base64 encoding converts three 8-bit bytes to four 6-bit bytes (3*8 = 4*6 = 24 ).
In fact, it is still 8 bits, but the height of the two bits is set to 0. When a byte is only 6 bits, its value space is 0.
6 to the power of 2 minus 1, that is, 63, that is, the space for each encoded base64 to be converted is (0 ~ 63)
.
In fact, 0 ~ There are many invisible characters in the ASCII code between 63, so we should make another ing. The ing table is
'A '~ 'Z '? ASCII (0 ~ 25)
'A '~ 'Z '? ASCII (26 ~ 51)
'0 '~ '9 '? ASCII (52 ~ 61)
'+ '? ASCII (62)
'/'? ASCII (63)
In this way, three 8-bit bytes can be converted to four visible characters.
The specific byte splitting method is as follows:

Aaaaaabb ccccdddd eeffffff
~~~~~~~~ ~~~~~~~~ ~~~~~~~~
1 byte 2 byte 3

//
00 aaaaaa 00 bbcccc 00 ddddee 00 ffffff

Note: The above three bytes are original, and the following four bytes are base64 encoded, and the first two are both 0.
In this case, the number of bytes in the original text should be a multiple of 3. When this condition cannot be met, the full zero bytes are used.
Complete. during conversion, the base64 encoding is replaced by the = sign, which is why some base64 encoding ends with one or two equal signs.
Because F (origin) represents the number of bytes in the original text, F (remain) represents
Table remainder, then
F (remain) = f (origin) mod 3 was established.
Therefore, the possible values of F (remain) are 0, 1, 2.
If n = [F (origin)-f (remain)]/3
When F (remain) = 0, it is converted to 4 * n Bytes of base64 encoding.
When F (remain) = 1, since a Source byte can be split into two base64 encoded bytes
Make base64 encoding a multiple of 4, so we should add two equal signs.
When F (remain) = 2, because the two original bytes can be split into three base64 encoded bytes, similarly,
An equal sign should be added.

The C language for base64 encoding and decoding is described below:
/* Base64 encoding */
Void base64 (unsigned char chasc [3], unsigned char chuue [4])
/*
Chasc: unencoded binary code
Chuue: encoded base64 code
*/
{
Int I, K = 2;
Unsinged char T = NULL;
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)> = 0) & (* (chuue + I) <= 25) * (chuue + I) + = 65;
Else if (* (chuue + I)> = 26) & (* (chuue + I) <= 51) * (chuue + I) + = 71;
Else if (* (chuue + I)> = 52) & (* (chuue + I) <= 61) * (chuue + I)-= 4;
Else if (* (chuue + I) = 62) * (chuue + I) = 43;
Else if (* (chuue + I) = 63) * (chuue + I) = 47;
}
/* Base64 decoding */
Void unbase64 (unsigned char chuue [4], unsigned char chasc [3])
/*
Chuue: undecoded base64 code
Chasc: decoded binary code
*/
{Int I, K = 2;
Unsigned char T = NULL;
For (I = 0; I <4; I ++)
If (* (chuue + I)> = 65) & (* (chuue + I) <= 90) * (chuue + I)-= 65;
Else if (* (chuue + I)> = 97) & (* (chuue + I) <= 122) * (chuue + I)-= 71;
Else if (* (chuue + I)> = 48) & (* (chuue + I) <= 57) * (chuue + I) + = 4;
Else if (* (chuue + I) = 43) * (chuue + I) = 62;
Else if (* (chuue + I) = 47) * (chuue + I) = 63;
Else if (* (chuue + I) = 61) * (chuue + I) = 0;
For (I = 0; I <3; I ++)
{* (Chhex + I) = * (chuue + I) <K;
K + = 2;
T = * (chuue + I + 1)> 8-K;
* (Chhex + I) | = T;
}
}

Bytes ----------------------------------------------------------------------------------------------------------------

Base64 encoding/decoding principles and function causes and principles:
Early emails were plain text, which is suitable in English-speaking countries without attachments (where the network is not very popular and the network speed is very slow)
However, with the development of the Internet, people want to transfer other things except English, such as applications, and other countries also want to use their own languages to write emails.
Therefore, the requirements for Binary transfer in emails are generated.
The mail is transmitted in plaintext.
If binary data is directly transmitted, the mail server may understand the error. For example, if the content in the body is incorrectly understood as a command (think about the special characters such as/0 in the second step), it has invented base64 encoding.
Encode the content with 64 printable characters
The 64 characters are
"Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /"
The encoding principle is: Convert the three bytes into four bytes using the above letters, each of which has only 6 bits.
The first byte consists of the first byte's 6-bit high.
The high position of the second byte is composed of the low two digits of the first byte. The low four digits are composed of the High four digits of the second byte.
The high position of the Third byte consists of the lower four bits of the second byte. The lower two bits consist of the higher two bits of the third byte.
The fourth byte is the remaining bit.

You can intuitively draw 18 bits on paper and divide them into 4 copies.
The following is an encoding example.

/Encode base64 string
// Return encode lencode /**
* @ Brief base64 encoding
*
* @ Param Buf [out] Save the encoding result
* @ Param text [in] the string to be encoded
* @ Param size [in] Text Length
*
* @ Return encoding Length
**/
Int base64enc (unsigned char * Buf, const unsigned char * Text, int size)
{
Static char * base64_encoding =
"Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";
Int buflen = 0;

While (size> 0 ){
* Buf ++ = base64_encoding [(Text [0]> 2) & 0x3f];
If (size> 2 ){
* Buf ++ = base64_encoding [(Text [0] & 3) <4) | (Text [1]> 4)];
* Buf ++ = base64_encoding [(Text [1] & 0xf) <2) | (Text [2]> 6)];
* Buf ++ = base64_encoding [Text [2] & 0x3f];
} Else {
Switch (size ){
Case 1:
* Buf ++ = base64_encoding [(Text [0] & 3) <4];
* Buf ++ = ';
* Buf ++ = ';
Break;
Case 2:
* Buf ++ = base64_encoding [(Text [0] & 3) <4) | (Text [1]> 4)];
* Buf ++ = base64_encoding [(Text [1] & 0x0f) <2) | (Text [2]> 6)];
* Buf ++ = ';
Break;
}
}

Text + = 3;
Size-= 3;
Buflen + = 4;
}

* Buf = 0;
Return buflen;
}

Char getbase64value (char ch)
{
If (CH> = 'A') & (CH <= 'Z '))
Return ch-'A ';
If (CH> = 'A') & (CH <= 'Z '))
Return ch-'A' + 26;
If (CH> = '0') & (CH <= '9 '))
Return ch-'0' + 52;
Switch (CH ){
Case '+ ':
Return 62;
Case '/':
Return 63;
Case '=':/* base64 padding */
Return 0;
Default:
Return 0;
}
}

/**
* @ Brief base64 Decoding
*
* @ Param Buf [out] Decoding result
* @ Param text [in] the string to be decoded must be a multiple of 4
* @ Param size [in] Text Size
*
* @ Return-1 error,> = 0 Decoding Buffer Length
**/
Int base64dec (unsigned char * Buf, const unsigned char * Text, int size)
{
If (size % 4)
Return-1;
Unsigned char chunk [4];
Int parsenum = 0;

While (size> 0 ){
Chunk [0] = getbase64value (Text [0]);
Chunk [1] = getbase64value (Text [1]);
Chunk [2] = getbase64value (Text [2]);
Chunk [3] = getbase64value (Text [3]);

* Buf ++ = (chunk [0] <2) | (chunk [1]> 4 );
* Buf ++ = (chunk [1] <4) | (chunk [2]> 2 );
* Buf ++ = (chunk [2] <6) | (chunk [3]);

Text + = 4;
Size-= 4;
Parsenum + = 3;
}

Return parsenum;
}

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.