Skip to content

Instantly share code, notes, and snippets.

@marius-m
Last active July 12, 2022 14:06
Show Gist options
  • Save marius-m/1ae903b69b6b2e3a5c743f3ed2f2a378 to your computer and use it in GitHub Desktop.
Save marius-m/1ae903b69b6b2e3a5c743f3ed2f2a378 to your computer and use it in GitHub Desktop.
SH script that uses `openssl` to encrypt raw text input to aes-sha256-cbc + base64 (Check decrypt.sh for inverse function)
#!/bin/bash
# Encrypts input file (+base64) contents with AES-256-CBC and a password
#set -x
FILE_IN=$1
if [[ -f $FILE_IN ]]; then
echo "Converting..."
else
echo "File not found!"
exit 1
fi
#KEY_PASS="pass1"
KEY_PASS="k7hlmq6sst2qz5yg2cl3bg1yyj1y5zge"
KEY_PASS_DIGEST="$(echo -n $KEY_PASS | openssl sha256)"
KEY_PASS_DIGEST_B64="$(echo -n $KEY_PASS_DIGEST | base64)"
FILE_NOEXT=$(basename "$FILE_IN" | cut -d. -f1)
FILE_OUT="${FILE_NOEXT}.txt.enc"
## Dependencies
echo "Pass: ${KEY_PASS}"
echo "Pass digest: ${KEY_PASS_DIGEST}"
echo "Pass digest B64: ${KEY_PASS_DIGEST_B64}"
## Original
echo "==(original)==> ${FILE_IN}"
echo -e "------------------------\n"
cat ${FILE_IN}
echo -e "\n------------------------\n"
## Encrypt
# @param "-a" encodes output directly to b64
# @param "-e" encode mode
# @param "-p" preview
# @param "-k" key password
tr -d '\n' < $FILE_IN | openssl aes-256-cbc -e -a -A \
-out ${FILE_OUT} \
-nosalt \
-K "${KEY_PASS_DIGEST}" \
-iv "0000000000000000" \
-p
echo "$FILE_IN ==(enc: aes256: \"$KEY_PASS\" + b64)=> ${FILE_OUT}"
echo -e "------------------------\n"
cat ${FILE_OUT}
echo -e "\n------------------------\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment