Using openssl to encrypt and decrypt files.

Trenix25

Well-Known Member
Joined
Aug 15, 2017
Messages
673
Reaction score
366
Credits
6,139
I just wrote up these two bash scripts and decided to share them here in the hope that they may be useful to other forum users.

encrypt
Code:
#!/bin/bash
#
# Uses /usr/bin/openssl to encrypt a file.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ ! -z "$5" ]; then
     echo "Usage: encrypt <input file name> <output file name> <number of iterations> <password>"
     exit 1
fi

# The number of iterations is used to derive the encryption key from the password.

/usr/bin/openssl enc -aes-256-cbc -salt -pbkdf2 -iter "$3" -in "$1" -out "$2" -pass pass:"$4"

exit $?

# EOF

decrypt
Code:
#!/bin/bash
#
# Uses /usr/bin/openssl to decrypt a file.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ ! -z "$5" ]; then
     echo "Usage: decrypt <input file name> <output file name> <number of iterations> <password>"
     exit 1
fi

# The number of iterations is used to derive the decryption key from the password.

/usr/bin/openssl enc -aes-256-cbc -d -salt -pbkdf2 -iter "$3" -in "$1" -out "$2" -pass pass:"$4"

exit $?

# EOF

Signed,

Matthew Campbell
 
Last edited:


Small suggestion:
if [ $# -ne 4 ]; then...
Instead of evaluating each argument for its abscence (or presence) to enforce number of arguments.
 
I just wrote up these two bash scripts and decided to share them here in the hope that they may be useful to other forum users.

encrypt
Code:
#!/bin/bash
#
# Uses /usr/bin/openssl to encrypt a file.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ ! -z "$5" ]; then
     echo "Usage: encrypt <input file name> <output file name> <number of interations> <password>"
     exit 1
fi

# The number of iterations is used to derive the encryption key from the password.

/usr/bin/openssl enc -aes-256-cbc -salt -pbkdf2 -iter "$3" -in "$1" -out "$2" -pass pass:"$4"

exit $?

# EOF

decrypt
Code:
#!/bin/bash
#
# Uses /usr/bin/openssl to decrypt a file.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ ! -z "$5" ]; then
     echo "Usage: decrypt <input file name> <output file name> <number of interations> <password>"
     exit 1
fi

# The number of iterations is used to derive the decryption key from the password.

/usr/bin/openssl enc -aes-256-cbc -d -salt -pbkdf2 -iter "$3" -in "$1" -out "$2" -pass pass:"$4"

exit $?

# EOF

Signed,

Matthew Campbell
OpenSSL is a powerful tool for encryption and decryption of files. Here’s a quick guide on how to use it:

### Encrypting a File
1. First, make sure OpenSSL is installed on your system.
2. To encrypt a file, use the following command:
Code:
bash
   openssl enc -aes-256-cbc -salt -in filename.txt -out encryptedfile.enc
- enc specifies the encoding command.
- -aes-256-cbc is the cipher for encryption.
- -salt adds random data to the encryption to make it more secure.
- -in specifies the input file.
- -out specifies the output file.

### Decrypting a File
1. To decrypt the file, use the following command:
Code:
bash
   openssl enc -d -aes-256-cbc -in encryptedfile.enc -out decryptedfile.txt
- -d tells OpenSSL to decrypt.
- The other options are the same as in the encryption command.

You’ll be prompted to enter a password during both the encryption and decryption processes. Make sure to use a strong and memorable password.

Feel free to ask if you need any more help!
 
I just wrote up these two bash scripts and decided to share them here in the hope that they may be useful to other forum users.

encrypt
Code:
#!/bin/bash
#
# Uses /usr/bin/openssl to encrypt a file.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ ! -z "$5" ]; then
     echo "Usage: encrypt <input file name> <output file name> <number of interations> <password>"
     exit 1
fi

# The number of iterations is used to derive the encryption key from the password.

/usr/bin/openssl enc -aes-256-cbc -salt -pbkdf2 -iter "$3" -in "$1" -out "$2" -pass pass:"$4"

exit $?

# EOF

decrypt
Code:
#!/bin/bash
#
# Uses /usr/bin/openssl to decrypt a file.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ ! -z "$5" ]; then
     echo "Usage: decrypt <input file name> <output file name> <number of interations> <password>"
     exit 1
fi

# The number of iterations is used to derive the decryption key from the password.

/usr/bin/openssl enc -aes-256-cbc -d -salt -pbkdf2 -iter "$3" -in "$1" -out "$2" -pass pass:"$4"

exit $?

# EOF

Signed,

Matthew Campbell
Your script looks great! It uses OpenSSL to encrypt a file with a specified number of iterations for key derivation. Here’s a quick rundown of what your script does:

1. Validates Input: Ensures all required arguments are provided.
2. Encrypts File: Uses OpenSSL with the AES-256-CBC cipher and PBKDF2 key derivation function.
3. Exits with Status: Returns the exit status of the OpenSSL command.

Just a minor typo: "interations" should be "iterations". Here's the corrected version:

Bash:
#!/bin/bash
#
# Uses /usr/bin/openssl to encrypt a file.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ ! -z "$5" ]; then
     echo "Usage: encrypt <input file name> <output file name> <number of iterations> <password>"
     exit 1
fi

# The number of iterations is used to derive the encryption key from the password.

/usr/bin/openssl enc -aes-256-cbc -salt -pbkdf2 -iter "$3" -in "$1" -out "$2" -pass pass:"$4"

exit $?

# EOF
 
Your decryption script looks solid. It follows the same logic as your encryption script. Just like before, there’s a minor typo: "interations" should be "iterations". Here's the corrected version:

Bash:
#!/bin/bash
#
# Uses /usr/bin/openssl to decrypt a file.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ ! -z "$5" ]; then
     echo "Usage: decrypt <input file name> <output file name> <number of iterations> <password>"
     exit 1
fi

# The number of iterations is used to derive the decryption key from the password.

/usr/bin/openssl enc -aes-256-cbc -d -salt -pbkdf2 -iter "$3" -in "$1" -out "$2" -pass pass:"$4"

exit $?

# EOF

This script will take an encrypted file and decrypt it using the provided password and the specified number of iterations.
 
admin said me why I reply fast and write more maybe I am a bot.
and said do not reply very old threads like that
 
admin said me why I reply fast and write more maybe I am a bot.
and said do not reply very old threads like that
Personally I don't think there's anything wrong with a new member looking around to explore the site. How could you have responded sooner if you weren't here? Please feel free to read any of my past posts and offer your feedback or corrections.

Signed,

Matthew Campbell
 
I use some AI some me for correct or add. I new at coding programmming and development. but I fast learner. I am some hacker like gray hat anything can happem and make nut not. bcz never my intension bad. mind is Clear for good dor people help omly not hacking not crackin but knowing wisdom and knowledge. and like you help people and share ideas/thoughts/minds be open mind like open source but ethically and not disturbing. if this site not help people if people not look old threads why this site built for? me same think as like you thanks dear. I will text you in pm.
 
I use some AI some me for correct or add. I new at coding programmming and development. but I fast learner. I am some hacker like gray hat anything can happem and make nut not. bcz never my intension bad. mind is Clear for good dor people help omly not hacking not crackin but knowing wisdom and knowledge. and like you help people and share ideas/thoughts/minds be open mind like open source but ethically and not disturbing. if this site not help people if people not look old threads why this site built for? me same think as like you thanks dear. I will text you in pm.
Would you be interested in learning about C?


Signed,

Matthew Campbell
 
I use some AI some me for correct or add. I new at coding programmming and development. but I fast learner. I am some hacker like gray hat anything can happem and make nut not. bcz never my intension bad. mind is Clear for good dor people help omly not hacking not crackin but knowing wisdom and knowledge. and like you help people and share ideas/thoughts/minds be open mind like open source but ethically and not disturbing. if this site not help people if people not look old threads why this site built for? me same think as like you thanks dear. I will text you in pm.
Regarding LLMs and answers generated by them, you may want to follow these guidelines https://www.linux.org/threads/forum...ules-on-using-llms-to-answer-questions.49422/ in the future; understanding, editing & reviewing, and stating you used an LLM. To the third point, I'm pretty sure "at the time of posting" is inferred. I'm also pretty sure it's not always required if you demonstrate a decent understanding and can collate the info in a meaningful way.


Okay, commercial break over, back to the thread...

That's a nice collection of info and projects you got there. You should put a link in your sig :) C is very undervalued these days (outside of lower-level stuff and a few apps), which is sad. When I first moved out of my BASIC phase, I had C and C++ to learn. Ended up with C initially. To my younger self, C was a far more elegant, because it was a smaller, simpler, cleaner language that gave you total freedom (and responsibility, lol).
 
yes I am good add C and Python. I can
I did see a little about Python is a tutorial video, but really haven't picked it up yet. I use it as a calculator because I don't have to compile anything to get it to run. I just use print() statements one at a time in the Python interpreter.

Signed,

Matthew Campbell
 

Members online

No members online now.

Top