我有一个字符串,它实际上是密码。我想加密字符串并将加密结果存储在参数文件中。接下来在脚本执行期间,加密字符串将被提取并在运行时被解密。所以我想知道如何在linux环境中加密和解密字符串/文本?
cas*_*ams 10
这不是这里要求的内容,但它显示了一种无需密码提示即可执行此操作的简单方法
#!/usr/bin/env bash
echo $1 | openssl aes-256-cbc -a -salt -pass pass:somepassword
Run Code Online (Sandbox Code Playgroud)
#!/usr/bin/env bash
echo $1 | openssl aes-256-cbc -d -a -pass pass:somepassword
Run Code Online (Sandbox Code Playgroud)
chmod +x encode.sh decode.sh
Run Code Online (Sandbox Code Playgroud)
./encode.sh "this is my test string"
# => U2FsdGVkX18fjSHGEzTXU08q+GG2I4xrV+GGtfDLg+T3vxpllUc/IHnRWLgoUl9q
Run Code Online (Sandbox Code Playgroud)
./decode.sh U2FsdGVkX18fjSHGEzTXU08q+GG2I4xrV+GGtfDLg+T3vxpllUc/IHnRWLgoUl9q
# => this is my test string
Run Code Online (Sandbox Code Playgroud)
小智 8
openssl 可以做得更好。
编码:
$ secret=$(echo "this is a secret." | openssl enc -e -des3 -base64 -pass pass:mypasswd -pbkdf2)
Run Code Online (Sandbox Code Playgroud)
解码:
$ echo "${secret}" | openssl enc -d -des3 -base64 -pass pass:mypasswd -pbkdf2
Run Code Online (Sandbox Code Playgroud)
尖端: