Use of the "base64" command tool in Linux

Source: Internet
Author: User

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. In another articleArticlePHP 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


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


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


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


Are you surprised to find out why?

The echo-N option does not output the ''line break character at the end of the string. Therefore, the exact base64 encoding of the string" snailwarrior "is" c25hawx3yxjyaw9y ", which can be verified by the PHP function. The file encoding method and other "snilwarrior" strings are all encoded with ''. Therefore, an inexplicable Encoding Error occurs if you are not careful.

4. base64 Decoding

[Root @ PPS ~] # Echo-n "c25hawx3yxjyaw9y" | base64-d


[Root @ PPS ~] #

From: and make appropriate adjustments

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: 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.