openssl enc -base64 -d在一定长度后不会解码字符串

Moh*_*mah 1 bash encoding openssl

我有一个简单的bash脚本,它使用base64对字符串进行编码/解码。脚本是:

#!/bin/bash
echo "encode or decode ?"
read input

if [ "$input" == "encode" ]
then

echo Please, enter your secret message 
read message
echo "The cipher representing your message is"
echo -n $message | openssl enc -base64


else

echo "Please enter your cipher"
read cipher
echo "Your secret message is"
echo  $cipher | openssl enc -base64 -d
echo ""


fi
Run Code Online (Sandbox Code Playgroud)

只要所编码的字符串的最大长度为49个字符,该脚本就可以正常工作。长度超过49个字符的字符串无法正确解码。知道我的脚本中可能有什么问题吗?

非常感谢!

oli*_*liv 5

openssl enc -base64在64个字符的编码字符串后插入回车符。实际上,这大约发生在要编码的字符串的第49个字符上。

为了避免在编码字符串中出现换行符,请-A在两个openssl命令中都使用option 一次对整个字符串进行编码或解码:

echo -n $message | openssl enc -base64 -A
Run Code Online (Sandbox Code Playgroud)

echo  $cipher | openssl enc -base64 -d -A 
Run Code Online (Sandbox Code Playgroud)

这似乎是特定于的openssl。如果您要使用base64coreutils软件包中的可执行文件,那么它也将正常工作。