使用 Base64 编码密码时是否需要包含 -n 选项?

发布于 2025-01-19 06:50:30 字数 646 浏览 5 评论 0原文

我在某个地方读到,在基本64中编码密码时,我应该使用echo -n来防止新线包含在编码值中。例如,

echo changeme | base64
Y2hhbmdlbWUK
echo -n changeme | base64
Y2hhbmdlbWU=

这两个base64字符串是不同的,但是当我解码它们时,它们是相同的

echo Y2hhbmdlbWUK | base64 -d
changeme
echo Y2hhbmdlbWU= | base64 -d
changeme

,所以我真的需要添加-n option https://linux.die.net/男人/1/echo说:

请勿输出尾线

搜索,并使用Python为这个长期的示例提供了根据密码编码字符串的简单方法?

I read somewhere that when encoding a password in base64, I should use echo -n to prevent the newline from being included in the encoded value. For example,

echo changeme | base64
Y2hhbmdlbWUK
echo -n changeme | base64
Y2hhbmdlbWU=

These two base64 strings are different, but when I decode them they are the same

echo Y2hhbmdlbWUK | base64 -d
changeme
echo Y2hhbmdlbWU= | base64 -d
changeme

So do I really need to add the -n option https://linux.die.net/man/1/echo says:

do not output the trailing newline

Searches gave this long winded example using python... didn't read it all Simple way to encode a string according to a password?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2025-01-26 06:50:30

当您使用 在线转换器 将其解码为十六进制时,您会看到第一个字符串变为 6368616e67656d650a 并在末尾有 0a (ASCII 换行符),而第二个则没有。

所以答案是肯定的,您确实需要添加 -n 选项。

如果您将 echo 更改为 echo -n 您会看到这也是。

echo -n Y2hhbmdlbWU= | base64 -d
changemePROMPT-SHOWS-HERE

查看此情况的另一种方法是使用以下 UNIX 命令:

echo -n Y2hhbmdlbWUK | base64 -d | od -c
0000000   c   h   a   n   g   e   m   e  \n

echo -n Y2hhbmdlbWU= | base64 -d | od -c
0000000   c   h   a   n   g   e   m   e

您可以在其中看到第一个编码包含换行符,而第二个则不包含换行符。

When you use an online converter to decode it to hex, you'll see that the first string becomes 6368616e67656d650a and has 0a (ASCII Linefeed) on the end, which the second doesn't have.

So the answer is yes, you really need to add the -n option.

If you change your echo to echo -n you'll see this as well.

echo -n Y2hhbmdlbWU= | base64 -d
changemePROMPT-SHOWS-HERE

Another way to see this is using the following UNIX command:

echo -n Y2hhbmdlbWUK | base64 -d | od -c
0000000   c   h   a   n   g   e   m   e  \n

echo -n Y2hhbmdlbWU= | base64 -d | od -c
0000000   c   h   a   n   g   e   m   e

Where you can see the first encoding includes the linefeed and the second does not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文