Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt a string using openssl command line

Tags:

linux

openssl

I have a 16 byte character that I would like to encrypt using openssl into a 16 byte encrypted string.

This encrypted string ( in human readable format ) then needs to be supplied to a user who would use it, and the string would be decrypted to its original 16-byte form for comparison and authentication. Could anyone please tell me how this would be possible with openssl commandline.

Thanks in advance.

like image 401
arun nath Avatar asked Apr 11 '12 13:04

arun nath


People also ask

How does OpenSSL encryption work?

Command line OpenSSL uses a rather simplistic method for computing the cryptographic key from a password, which we will need to mimic using the C++ API. OpenSSL uses a hash of the password and a random 64bit salt. Only a single iteration is performed.


1 Answers

Here's one way to encrypt a string with openssl on the command line (must enter password twice):

echo -n "aaaabbbbccccdddd" | openssl enc -e -aes-256-cbc -a -salt enter aes-256-cbc encryption password: Verifying - enter aes-256-cbc encryption password: 

Here's what the output looks like:

U2FsdGVkX1/6LATntslD80T2HEIn3A0BqxarNfwbg31D2kI00dYbmBo8Mqt42PIm 

Edit: To my knowledge, you can't control the number of bytes out. You can b64 or hex encode it, but that's about it. Also, if you want to save that string to a file rather than stdout, use the -out option.

like image 115
01100110 Avatar answered Oct 05 '22 13:10

01100110