menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

Shell命令加解密AES

作者:Chancel Yang, 创建:2021-08-30, 字数:2674, 已阅:80, 最后更新:2024-03-10

使用Openssl加密与解密

Bash
echo -n "加密字符串" | openssl enc -aes-128-ecb -a -iter 100 -pbkdf2 -e -pass pass:"很随意的密钥" -nosalt
echo 加密后的字符串" | openssl enc -aes-128-ecb -a -iter 100 -pbkdf2 -d -pass pass:"很随意的密钥" -nosalt

实际操作如下,以加密字符串“hello world”为例,使用的KEY是“123456”

Bash
➜  openssl version
OpenSSL 1.1.1k  25 Mar 2021echo -n "hello world" | openssl enc -aes-128-ecb -a -iter 100 -pbkdf2 -e -pass pass:"123456" -nosalt
wqMX+v+z5uZdk8IAEK3IoA==echo "wqMX+v+z5uZdk8IAEK3IoA==" | openssl enc -aes-128-ecb -a -iter 100 -pbkdf2 -d -pass pass:"123456" -nosalt 
hello world

为了方便使用,编辑成一个简易的Shell脚本

Bash
#!bin/bash
#chancel's blog
#www.chancel.me

show_help() {
    echo "$0 [-h|-?|--help] [--text hello world] [--aes gwBRELFibLqsAANl] [--op decrypt]"
    echo "-h|-?|--help    显示帮助"
    echo "--text          字符串"
    echo "--aes           AES加密KEY"
    echo "--op            加密-encrypt,解密-decrypt"
}

while [[ $# -gt 0 ]]; do
    case $1 in
    -h | -\? | --help)
        show_help
        exit 0
        ;;
    --text)
        text="${2}"
        shift
        ;;
    --aes)
        aes="${2}"
        shift
        ;;
    --op)
        op="${2}"
        shift
        ;;
    --)
        shift
        break
        ;;
    *)
        echo -e "错误: $0 无效操作 '$1'\n可输入命令 '$0 --help' 获取更多帮助.\n" >&2
        exit -1
        ;;
    esac
    shift
done

if [ "${op}" == "encrypt" ]; then
    echo -n "${text}" | openssl enc -aes-128-ecb -a -iter 100 -pbkdf2 -e -pass pass:"${aes}" -nosalt
elif [ "${op}" == "decrypt" ]; then
    echo "$text" | openssl enc -aes-128-ecb -a -iter 100 -pbkdf2 -d -pass pass:"${aes}" -nosalt
else
    echo “模式参数错误”
fi
exit 0

使用例子如下

Bash
➜  bash aes-crypt.sh --help
1.sh [-h|-?|--help] [--text hello world] [--aes gwBRELFibLqsAANl] [--op decrypt]
-h|-?|--help    显示帮助
--text          字符串
--aes           AES加密KEY
--op            加密-encrypt,解密-decrypt
➜ bash aes-crypt.sh --text "hello world" --aes "gwBRELFibLqsAANl" --op "encrypt"
0MLTKgtu7AV3Kw7RQyhCFg==
➜  bash aes-crypt.sh --text "0MLTKgtu7AV3Kw7RQyhCFg==" --aes "gwBRELFibLqsAANl" --op "decrypt"
hello world

既然已经写成了脚本,更方便的当然要数放在Path里,将aes-crypt.sh文件放到用户目录下

Bash
➜ mv aes-crypt.sh ~/aes-crypt.sh & cd ~
➜ chmod +x aes-crypt.sh

编辑环境变量文件,增加如下自定义alias命令

Bash
vim ~/.zshrc

...
# chancel's alias
...
alias aese='aese_script(){bash /home/chancel/Scripts/aes-crypt.sh --text $1 --aes "123456" --op "encrypt"};aese_script'
alias aesd='aesd_script(){bash /home/chancel/Scripts/aes-crypt.sh --text $1 --aes "123456" --op "decrypt"};aesd_script'

新开一个终端,测试aes加密效果

Bash
➜  ~ aese "hello"
CWAtiyADpeBTrJeD/V3mhg==
➜  ~ aesd "CWAtiyADpeBTrJeD/V3mhg=="
hello

Perfect! !


[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]