Skip to content

Instantly share code, notes, and snippets.

@CyJimmy264
Created June 7, 2018 06:18
Show Gist options
  • Save CyJimmy264/2d841c26f98287aa2ab84619a9033a56 to your computer and use it in GitHub Desktop.
Save CyJimmy264/2d841c26f98287aa2ab84619a9033a56 to your computer and use it in GitHub Desktop.
openssl files encrypt/decrypt
##########################
# Small files
##########################
# convert pub key to pem.pub format
ssh-keygen -f ~/.ssh/id_rsa.pub -e -m PKCS8 > id_rsa.pem.pub
# inverse
ssh-keygen -f id_rsa.pem.pub -i -m PKCS8
# encrypt small files
openssl rsautl -encrypt -pubin -inkey id_rsa.pem.pub -ssl -in myMessage.txt -out myEncryptedMessage.txt
# decrypt
openssl rsautl -decrypt -inkey ~/.ssh/id_rsa -in myEncryptedMessage.txt -out myDecryptedMessage.txt
##########################
# Big files
##########################
openssl rsa -in id_rsa -outform pem > id_rsa.pem
openssl rsa -in id_rsa -pubout -outform pem > id_rsa.pub.pem
openssl rand -base64 32 > key.bin
openssl rsautl -encrypt -inkey id_rsa.pub.pem -pubin -in key.bin -out key.bin.enc
openssl enc -aes-256-cbc -salt -in SECRET_FILE -out SECRET_FILE.enc -pass file:./key.bin
openssl rsautl -decrypt -inkey id_rsa.pem -in key.bin.enc -out key.bin
openssl enc -d -aes-256-cbc -in SECRET_FILE.enc -out SECRET_FILE -pass file:./key.bin
##########################
# Function proto
##########################
function pem_encrypt() {
ssh-keygen -f "$1" -e -m PKCS8 > "$1.pem"
openssl rsautl -encrypt -pubin -inkey "$1.pem" -ssl -in "$2" -out "$2.enc"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment