今是昨非

今是昨非

日出江花红胜火,春来江水绿如蓝

Issues with base64 encryption

Background#

In the project, there is a feature similar to QQ's flash photo function. The logic for sending images is to first convert them to base64, then encrypt them, and finally send the encrypted string as a file. When processing the received images, the approach is to download the file first, then read the file content, decrypt it, and finally generate the image using the base64 string. However, this approach has been failing consistently.

Solution#

Initially, it was thought to be an encryption/decryption issue. After careful investigation, this assumption was ruled out. Finally, when copying the decrypted string to VSCode for closer observation, it was noticed that there were many \r\n characters present.

After searching, it was found that this issue was related to base64 encryption. Since base64 strings should not exceed 76 characters per line, if they do, a carriage return and line feed character will be added (in Windows it is \r\n, in Linux it is \n). The carriage return and line feed characters in the parsed string were causing the error. Therefore, the solution is to replace \r\n with an empty string and then generate the image, which resolves the issue.

Reference#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.