Article Title: Use of base64 command tools in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Base64 encoding is very common in e-mails. For emails such as Foxmail and Outlook, the user agent performs SMTP verification when sending emails, that is, entering the username and password in base64 encoding format for verification, the body content and attachments of emails are generally transmitted using base64 encoding.
When using the telnet tool to test mail sending, if verification is required, the practical base64 encoding tool becomes necessary. I mentioned in another article that PHP functions can be used for implementation. Here we will introduce a more practical Linux tool: base64
The base64 tool of RHEL5 is in the coreutils installation package. It is usually installed in Linux. The following example describes how to use base64 and related precautions.
This is the file in the test file. The content has only one line of string "snailwarrior ".
[Root @ pps ~] # Cat file
Snailwarrior
1. encode the file in base64 format and print it to the standard output.
[Root @ pps ~] # Base64 file
C25haWx3YXJyaW9yCg =
You can also do this:
[Root @ pps ~] # Cat file | base64
C25haWx3YXJyaW9yCg =
2. Read the file content from the standard input, encoded in base64, and printed to the standard output.
[Root @ pps ~] # Base64
Snailwarrior
C25haWx3YXJyaW9yCg =
I entered the "snilwarrior", Press enter, and press Ctrl + D to end the file.
[Note] If I do not press enter, enter Ctrl + D twice in a row to see how it works:
[Root @ pps ~] # Base64
Snilwarriorc25hawx3yxjyaw9y
The encoded string is c25haWx3YXJyaW9y.
My God! The results of the two encoding methods are different! Continue the test.
3. encode the string "snilwarrior" and print it to the standard output.
[Root @ pps ~] # Echo "snailwarrior" | base64
C25haWx3YXJyaW9yCg =
This result is the same as the previous "cat file" and the entered "snailwarrior" Press ENTER "and press Ctrl + D. Why is the result of pressing Ctrl + D twice without pressing enter different? Let's take a look at the following example:
[Root @ pps ~] # Echo-n "snilwarrior" | base64
C25haWx3YXJyaW9y
Are you surprised to find out why?
The echo-n option does not output the '\ n' line break character at the end of the string. Therefore, the exact base64 encoding of the string "snilwarrior" is "c25haWx3YXJyaW9y", which can be verified by the PHP function. The "snailwarrior" string encoding in file mode includes '\ n' encoding. Therefore, an inexplicable Encoding Error occurs if you are not careful.
4. Base64 Decoding
[Root @ pps ~] # Echo "Snail warrior" | base64 | base64-d
Snailwarrior
Base64: invalid input
[Root @ pps ~] # Echo-n "snilwarrior" | base64 | base64-d
Snailwarriorbase64: invalid input
[Note] the base64 tool provided by RHEL5 has a BUG. When base64 is decoded, the following error occurs: base64: invalid input. Here we can download a base64 source code package and compile it by ourselves:
Http://www.fourmilab.ch/webtools/base64/base64-1.5.tar.gz
I have tested it:
[Root @ pps base64-1.5] # echo "Snail ilwarrior" |./base64 |./base64-d
Snailwarrior
[Root @ pps base64-1.5] # echo-n "Snail ilwarrior" |./base64 |./base64-d
Snail warrior [root @ pps base64-1.5] #