bash 解密 base64 并保存在第二个变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18845361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:35:53  来源:igfitidea点击:

Decrypt base64 and save in second variable

bashshellencryptionbase64

提问by WorstCase

I am trying to write a password security system. (Just a small homesystem to learn some bash scripting) I am able to encrypt the password string and save it to another file.

我正在尝试编写密码安全系统。(只是一个学习 bash 脚本的小型家庭系统)我能够加密密码字符串并将其保存到另一个文件中。

My next step is to export these encrypted password strings in my shell script and decrypt them to use them in this routine. I found an export solution with simple export commands and I finally saved the encrypted strings to new variables.

我的下一步是在我的 shell 脚本中导出这些加密的密码字符串并解密它们以在此例程中使用它们。我找到了一个带有简单导出命令的导出解决方案,我最终将加密的字符串保存到新变量中。

How can I decrypt these strings and save them in another variable. I am using openssl and base64 encryption.

如何解密这些字符串并将它们保存在另一个变量中。我正在使用 openssl 和 base64 加密。

This is the source code:

这是源代码:

export user=$(cat logindata | head -n1 | tail -n1)
export passwd=$(cat logindata | head -n2 | tail -n1)
passwd2=echo -n $passwd | openssl enc -d -base64
echo "$user"
echo "$passwd2"

回答by konsolebox

This script would give the concept:

该脚本将给出以下概念:

#!/bin/sh

username='my_username'
password='my_password'

username_encoded=$(echo -n "$username" | openssl enc -base64)
password_encoded=$(echo -n "$password" | openssl enc -base64)

username_decoded=$(echo "$username_encoded" | openssl enc -d -base64)
password_decoded=$(echo "$password_encoded" | openssl enc -d -base64)

echo "username: $username"
echo "username_encoded: $username_encoded"
echo "username_decoded: $username_decoded"
echo "password: $password"
echo "password_encoded: $password_encoded"
echo "password_decoded: $password_decoded"

Output:

输出:

username: my_username
username_encoded: bXlfdXNlcm5hbWU=
username_decoded: my_username
password: my_password
password_encoded: bXlfcGFzc3dvcmQ=
password_decoded: my_password

Notice that when decoding, you need to send a newline at the end that's why I didn't use -nwith echo.

请注意,在解码时,您需要在末尾发送一个换行符,这就是我没有使用-necho 的原因。