openssl enc -base64 -d在一定长度之后无法解码字符串

7

我有一段简单的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

脚本在编码的字符串长度最多为49个字符时运作良好。长度超过49个字符的字符串无法正确解码。您觉得我的脚本可能有什么问题吗?
非常感谢!
1个回答

13

openssl enc -base64在对字符串进行编码时,每隔64个字符就会插入一个换行符。实际上,这会发生在要编码的字符串的第49个字符左右。

为避免在编码后的字符串中出现换行符,请在openssl命令中使用选项-A,以一次性编码或解码整个字符串:

echo -n $message | openssl enc -base64 -A

echo  $cipher | openssl enc -base64 -d -A 

这似乎是与openssl相关的特定情况。如果您使用来自coreutils软件包的base64可执行文件,这也可以正常工作。


作为一个附注,我们还可以使用一个简短的形式,即没有enc部分的openssl base64 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接