top of page

A little script that I wrote to encrypt and decrypt files using openssl - Enjoy

#!/bin/bash # # Created by: Hagi Lerman # # Date Jul 28 2017. # # Please visit www.hagi-lerman.com/blog. # # Warning! : Use this script on your own risk! # # Use: This Script can Generate Private key and Public Key, Encrypt a File, Decrypt a File, Sign a file and verify the sender using openssl. # # You can use the script from the current directory of where the PublicKey is located or specify the full path of the files. # Have Fun :) #set -x GenKeys(){ KeysDir="$HOME/EncryptKeys" mkdir $KeysDir #Generate the PrivateKey-UserName.pem in the user home directory in a folder called EncryptKeys read -p "$(echo -e 'Please Enter Your Name: \n \b')" NAME openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:3 -out $KeysDir/PrivateKey-$NAME.pem #change permission to secure the public key chmod 600 $KeysDir/PrivateKey-$NAME.pem #Generate the PublicKey-UserName.pem in the user home directory in a folder called EncryptKeys openssl pkey -in $KeysDir/PrivateKey-$NAME.pem -out $KeysDir/PublicKey-$NAME.pem -pubout cp $KeysDir/PublicKey-$NAME.pem . echo "Your Public and Private keys were generated and can be found in $KeysDir" echo " " echo " " echo " " main } Signature(){ # we sign a message in order for the receiver to verified our file ( the sender ). echo " " echo " " echo " " read -p "Notice! The Signing method in this script might not work for you if you use diffrant private key algorithm. Would you Like to continue ( y / n )" UAnswer if [ $UAnswer == "y" ]; then read -p "$(echo -e 'Enter Your Private Key file. Usage: File.Ext: \n \b')" PrivateKey echo " " echo " " echo " " read -p "$(echo -e 'Enter the file Name that you want to sign. Usage: File.Ext: \n \b')" FileToSign openssl dgst -sha1 -sign $PrivateKey -out signature.bin $FileToSign echo " " echo " " echo " " exit elif [ $UAnswer == "n" ]; then main fi } Encrypt(){ echo " " echo " " echo " " ls -la echo " " echo " " echo " " read -p "$(echo -e 'Enter the Public Key file you received from the ***Other User***. Usage: File.Ext: \n \b')" OPubKey echo " " echo " " echo " " read -p "$(echo -e 'Enter the File file You want to ENCRYPT. Usage: File.Ext: \n \b')" EncryptFile echo " " echo " " echo " " openssl pkeyutl -encrypt -in $EncryptFile -pubin -inkey $OPubKey -out $EncryptFile.bin echo "Your File $EncryptFile is now encrypted and was rename to $EncryptFile.bin" rm -f $EncryptFile echo " " echo " " echo " " main } Decrypt(){ echo " " echo " " echo " " read -p "$(echo -e 'Enter the file you want to DECRYPT. Usage: File.Ext: \n \b')" DecFile read -p "$(echo -e 'Enter Your Private Key. Usage: File.Ext: \n \b')" PrivKey echo " " echo " " echo "Note:The Decrypted-File could be text or any other type. Its your responsibility to know what type is the file you recevied" openssl pkeyutl -decrypt -in $DecFile -inkey $PrivKey -out Decrypted-File echo "Yesssssss! Success ! The file was decrypt and renamed to Decryped-File" echo " " echo " " echo "Note:The Decrypted-File could be text or any other type. Its your responsibility to know what type is the file you recevied" echo " " echo " " echo " " main } Verify(){ echo " " echo " " echo " " read -p "$(echo -e 'Enter Your Public Key file of the ***Other User***. Usage: File.Ext: \n \b')" OUPkey read -p "$(echo -e 'Enter the SIGNATURE.BIN file you received from the ***Other User***. Usage: File.Ext: \n \b')" Sig read -p "$(echo -e 'Enter the Decrypted-File that was generated after the Decryption. Usage: File.Ext: \n \b')" DecFile openssl dgst -sha1 -verify $OUPkey -signature $Sig $DecFile echo " " echo " " echo " " main } ListFiles(){ echo " " echo " " echo " " ls -la echo " " echo " " echo " " main } ### MAIN ##### main(){ echo " " echo " " echo " " echo "Please Make a Selection: " PS3='Please enter your choice: ' options=("Generate Keys" "Sign A File-Before Sending" "Encrypt" "Decrypt" "Verify Signed File" "List Files" "Quit") select option in "${options[@]}" do case $option in "Generate Keys")GenKeys;; "Sign A File-Before Sending")Signature;; "Encrypt")Encrypt;; "Decrypt")Decrypt;; "Verify Signed File")Verify;; "List Files")ListFiles;; "Quit")exit;; *) echo invalid option;; esac done } main

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page