Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Last active December 28, 2021 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvbrianking/75b39e52e8dd05ae3d8f766761289bd8 to your computer and use it in GitHub Desktop.
Save mtvbrianking/75b39e52e8dd05ae3d8f766761289bd8 to your computer and use it in GitHub Desktop.
OpenSSL Encryption & Decryption
#!/bin/bash
encryptedPayloadFile=""
privateKeyFile=""
outputFile=""
function diy() {
cat <<EOF
openssl enc -base64 -A -d -in payload.cipher.base64 -out payload.cipher
openssl rsautl -decrypt -inkey ssl/client.key -in payload.cipher -out payload.txt
EOF
}
function usage() {
# ./decrypt.sh -p "payload.cipher.base64" -k "ssl/client.key" -o "payload.txt"
cat <<EOF
Usage: $0 [ -p PAYLOAD ] [ -k PRIVATE_KEY ] [ -o OUTPUT ] [ -h HELP ]
Options:
-p [Payload] file to decrypt.
-k [Private Key] file for decryption.
-o [Output] file for encrypted payload. (optional)
-h [Help] Show usage
EOF
}
function abort() {
errcho "Usage: $0 [ -p PAYLOAD ] [ -k PRIVATE_KEY ] [ -o OUTPUT ] [ -h HELP ]" 1>&2
exit 1
}
function errcho() {
>&2 echo $@;
}
while getopts ":p:k:o:h" options
do
case $options in
p)
encryptedPayloadFile="$OPTARG"
;;
k)
privateKeyFile="$OPTARG"
;;
o)
outputFile="$OPTARG"
;;
h)
usage
exit 0
;;
:)
errcho "Option: [ -${OPTARG} ] requires an argument."
abort
;;
*)
abort
;;
esac
done
if [ -z "$encryptedPayloadFile" ]; then
errcho "[ -p PAYLOAD ] is required"
exit 1
fi
if [ ! -f "$encryptedPayloadFile" ]; then
errcho "Encrypted payload file: '$encryptedPayloadFile' doesn't exist."
exit 1
fi
if [ -z "$privateKeyFile" ]; then
errcho "[ -k KEY ] is required"
exit 1
fi
if [ ! -f "$privateKeyFile" ]; then
errcho "Public key file: '$privateKeyFile' doesn't exist."
exit 1
fi
openssl enc -base64 -A -d -in $encryptedPayloadFile -out /tmp/payload.cipher
if [ "$outputFile" ]; then
openssl rsautl -decrypt -inkey $privateKeyFile -in /tmp/payload.cipher -out $outputFile
else
openssl rsautl -decrypt -inkey $privateKeyFile -in /tmp/payload.cipher
fi
exit 0
#!/bin/bash
payload=""
publicKey=""
outputFile=""
function diy() {
cat <<EOF
printf %s 'jdoe:21331157' > payload.txt
openssl rsautl -encrypt -inkey ssl/client.pub -pubin -in payload.txt -out payload.cipher
openssl enc -base64 -A -in payload.cipher -out payload.cipher.base64
EOF
}
function usage() {
# ./encrypt.sh -p "jdoe:21331157" -k "ssl/client.pub" -o "payload.cipher.base64"
cat <<EOF
Usage: $0 [ -p PAYLOAD ] [ -k PUBLIC_KEY ] [ -o OUTPUT ]
Options:
-p [Payload] to be encrypted.
-k [Public key] file for encryption.
-o [Output] file for encrypted payload. (optional)
-h [Help] Show usage.
EOF
}
function abort() {
errcho "Usage: $0 [ -p PAYLOAD ] [ -k PUBLIC_KEY ] [ -o OUTPUT ] [ -h HELP ]" 1>&2
exit 1
}
function errcho() {
>&2 echo $@;
}
while getopts ":p:k:o:h" options
do
case $options in
p)
payload="$OPTARG"
;;
k)
publicKey="$OPTARG"
;;
o)
outputFile="$OPTARG"
;;
h)
usage
exit 0
;;
:)
errcho "Option: [ -${OPTARG} ] requires an argument."
abort
;;
*)
abort
;;
esac
done
if [ -z "$payload" ]; then
errcho "[ -p PAYLOAD ] is required"
exit 1
fi
if [ -z "$publicKey" ]; then
errcho "[ -k PUBLIC_KEY ] is required"
exit 1
fi
if [ ! -f "$publicKey" ]; then
errcho "Public key file: '$publicKey' doesn't exist."
exit 1
fi
printf "%s" $payload > /tmp/payload.txt
openssl rsautl -encrypt -inkey $publicKey -pubin -in /tmp/payload.txt -out /tmp/payload.cipher
if [ "$outputFile" ]; then
openssl enc -base64 -A -in /tmp/payload.cipher -out $outputFile
else
openssl enc -base64 -A -in /tmp/payload.cipher
fi
rm -f /tmp/payload.txt
rm -f /tmp/payload.cipher
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment