Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Base64 in Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "sample analysis of Base64 in Linux", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of Base64 in Linux".

Base64 coding is very common in e-mail. When e-mail user agents such as Foxmail and Outlook do SMTP verification, they enter the user name and password of base64 encoding format for verification, while the main content and attachments of mail are generally transmitted by base64 code.

Examples of Base64 command usage

This is the test file file, with only one line of the string "snailwarrior".

[root@pps ~] # cat filesnailwarrior12

First, encode the file file with base64 and print it to standard output, as follows

[root@pps ~] # base64 filec25haWx3YXJyaW9yCg==12

You can also do this:

[root@pps ~] # cat file | base64c25haWx3YXJyaW9yCg==12

Second, read the contents of the file from standard input, base64 encoding and printing to standard output

[root@pps ~] # base64snailwarriorc25haWx3YXJyaW9yCg==123

I enter snailwarrior, enter, and then press Ctrl+D to end the file input.

[note] if I do not enter enter, enter Ctrl+D twice in a row to see how it works, as follows

[root@pps ~] # base64snailwarriorc25haWx3YXJyaW9y12

As you can see, the encoded string is: c25haWx3YXJyaW9y

My God! The results of the two ways of coding are different. Keep testing.

Third, encode the string "snailwarrior" and print it to standard output, as follows

[root@pps ~] # echo "snailwarrior" | base64c25haWx3YXJyaW9yCg==12

This result is the same as the previous "cat file" and enter snailwarrior press enter and then press Ctrl+D. Why not press enter and press Ctrl+D twice and the result is different? Let's take a look at the following example:

[root@pps ~] # echo-n "snailwarrior" | base64c25haWx3YXJyaW9y12

Are you surprised to find the "reason"?

The echo-n option does not output the newline character at the end of the string, so the exact base64 encoding of the string "snailwarrior" is "c25haWx3YXJyaW9y", which can be verified by the PHP function. File methods and other "snailwarrior" string encoding are brought into the'\ n 'encoding, therefore, inexplicable coding errors will occur accidentally.

The Java code for base64 is as follows

String pwd = "test"; byte [] enbytes = Base64.encodeBase64 (pwd.getBytes ()); log.info ("> {}", new String (enbytes)); byte [] debytes = Base64.decodeBase64 (new String ("eyJuYW1lIjoi5byg5LiJIn0K")); log.info ("> {}", new String (debytes)); 1234567

You need to follow the corresponding jar file through maven, as follows

Commons-codec commons-codec

The above is all the content of the article "sample Analysis of Base64 in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report