Created
January 2, 2021 09:04
-
-
Save sakamossan/91e95b39c6301d337dfd402efa71d729 to your computer and use it in GitHub Desktop.
文字列をopensslで暗号化/復号するbashスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu | |
function die () { | |
echo "$@" 1>&2 | |
echo 'encrypt.sh [-e|-d|--encrypt|--decrypt|] --body [base64enc or securing phrase]' | |
exit 1 | |
} | |
mode= | |
body= | |
for OPT in "$@"; do | |
case $OPT in | |
'-h' | '--help') | |
die | |
shift | |
;; | |
'-e' | '--encrypt') | |
mode=encrypt | |
shift | |
;; | |
'-d' | '--decrypt') | |
mode=decrypt | |
shift | |
;; | |
'--body') | |
body=$2 | |
shift 2 | |
;; | |
esac | |
done | |
# 必須の引数チェック | |
test -z "$mode" && die "--mode required" | |
test -z "$body" && die "--body required" | |
read -sp "enter password: " password | |
echo "" >&2 # プロンプトに改行をつける | |
function encrypt () { | |
openssl aes-256-cbc -e \ | |
-in <(echo $body) \ | |
-pass pass:$password \ | |
| base64 | |
} | |
function decrypt () { | |
openssl aes-256-cbc -d \ | |
-in <(echo $body | base64 -d) \ | |
-pass pass:$password | |
} | |
if [ "$mode" == "encrypt" ]; then | |
encrypt | |
elif [ "$mode" == "decrypt" ]; then | |
decrypt | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment