Symmetric Encryption and Decryption with a key file
Generate a random 256-bit key
To generate a random 256-bit key using /dev/urandom, you can directly extract the random data without the need for additional hashing. Here’s how you can do it:
$ dd if=/dev/urandom of=/path/to/your/keyfile bs=1 count=32
In this command:
if=/dev/urandom specifies the input source as /dev/urandom.
of=/path/to/your/keyfile specifies the output file where your key will be saved.
bs=1 sets the block size to 1 byte.
count=32 specifies that we want 32 bytes (256 bits) of random data.
Example:
$ dd if=/dev/urandom of=symmetric_keyfile.key bs=1 count=32
Encryption
We can use this command to encrypt the sample.txt file:
$ openssl enc -in sample.txt -out sample.txt.enc -e -aes256 -pbkdf2 -kfile symmetric_keyfile.key
Decryption
$ openssl enc -in sample.txt.enc -out sample_decrypted.txt -d -aes256 -pbkdf2 -kfile symmetric_keyfile.key
Comments
Post a Comment