Cryptopp use of Open source Libraries (ii): Base64 encryption

Source: Internet
Author: User

A lot of times I'm just a good tool user, good tools to play a role

The recent use of Cryptopp's base64 to encrypt compressed ZIP file content has experienced problems.

First zip compression is fine, but the last Base64 string is incorrect:

1, the wrong length (the problem should be the source string problem)

2, using off-the-shelf tools to decrypt the contents of the zip file (the problem is not a clue)

Since there is no way to solve the problem from the performance of the question, then judge the root of the problem from the normal logic.

1, first check the zip read, using fread read out and then fwrite write, to determine fread content is valid

This process found fwrite write the zip file exception, compared to the file's hexadecimal data found in each 0x0a front of a 0x0d, compared to ASCII code is known to be the problem of line break;

But Fread will not have a problem, an unexpected harvest, so to determine fread read out the content of no problem.

2, check the output of Base64, first of all to see the base64 call code:

stringGetBase64 (Const byte* SRC,Const intLen)    {Base64encoder encoder; Encoder.    Put (SRC, len); Encoder.    Messageend (); Lword size=Encoder.    Maxretrievable (); byte* Encodestr =New byte[Size +1]; Encodestr[size]=' /'; Encoder.    Get (encodestr, size);    Ostringstream Osstr; Osstr<<Encodestr; Delete[]encodestr; returnosstr.str ();}

The above code can output the base64 encryption result normally, but the verification code obtained MD5 the result is invalid.

3, MD5 after the use of the previous article has been determined to be correct, that is, the output of Base64 (MD5 input) has an exception.

Someone may have felt the problem in step 2, which is 0XOD 0X0A. A friend with cross-platform development experience will think that this line break will cause problems.

4, Base64 encoded data still appear 0x0a (\ n), the Base64 output string using out-of-the-box tools to MD5, and then compare their MD5 code out of the results found anomalies.

5, at this time should be reflected on \ n, if the string needs to output \ nthe need to say "\\n", that is, the escape character, this problem is found. 2 times MD5 input is not the same as natural, then the string inside the code using the "\\n" test, MD5 results can be on the.

6, the problem found to find a solution, the most direct way to replace the output of Base64, "\ n" replaced with "\\n", to this seems to solve the problem, but this approach is always unsatisfactory.

7, why in the Base64 encryption will produce 0x0a, the first thought is the introduction of the input, but the number of comparison 0x0a after the conclusion is not input introduced, at this time base64 input into Notepad will find 0x0a location is quite fixed, is it base64 increase?

8, because it is the first time to use Base64, experience naturally useless, then Niang, Google at the same time, directly search for "Base64 escape character."

9, search results show that: Base64 encryption by default will add a newline character (0x0A) in a fixed length position. The rest is to see Cryptopp's base64. How to change this default setting so that it does not add line breaks, see the Cryptopp base64.h file after the code to modify the following.

false);

10, at this point to solve the problem, Base64 encryption use is also a preliminary understanding. In-depth principle research will need to be carried out later, after all, project time is not allowed.

Cryptopp use of Open source Libraries (ii): Base64 encryption

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.