1. Base64 encode the file file and print it to the standard output
[root@pps ~]# base64 file c25haWx3YXJyaW9yCg== 也可以这样: [root@pps ~]# cat file | base64 c25haWx3YXJyaW9yCg==
2. Read the file content from the standard input, base64 encode it and print it to the standard output
[root@pps ~]# base64 snailwarrior c25haWx3YXJyaW9yCg==
3. Encode the string "snailwarrior" and print it to the standard output
[root@pps ~]# echo "snailwarrior" | base64 c25haWx3YXJyaW9yCg==
4. Base64 decoding
[root@pps ~]# echo "snailwarrior" | base64 | base64 -d snailwarrior base64: invalid input[root@pps ~]# echo -n "snailwarrior" | base64 | base64 -d snailwarriorbase64: invalid input
Note:
The echo -n option does not output the '\n' newline character at the end of the string, so the exact base64 encoding of the string "snailwarrior" is "c25haWx3YXJyaW9y" , you can use PHP function to check. The "snailwarrior" string encoding in file formats, etc. all incorporates the encoding of '\n', so inexplicable encoding errors may occur accidentally.
用法:base64 [选项]... [文件] 使用 Base64 编码/解码文件或标准输入输出。 如果没有指定文件,或者文件为"-",则从标准输入读取。 必选参数对长短选项同时适用。 -d, --decode 解码数据 -i, --ignore-garbag 解码时忽略非字母字符 -w, --wrap=字符数 在指定的字符数后自动换行(默认为76),0 为禁用自动换行 --help 显示此帮助信息并退出 --version 显示版本信息并退出 数据以 RFC 4648 规定的 base64 字母格式进行编码。 解码时,输入数据(编码流)可能包含一些非有效 base64 字符以及换行符。 可以尝试用 --ignore-garbage 选项来绕过编码流中的无效字符。
The above is the detailed content of How to implement linux base64 encryption and decryption. For more information, please follow other related articles on the PHP Chinese website!