Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an openssl iv, and why do I need a key and an iv?

Tags:

I am about to use the following script to encrypt and decrypt some data. I am using it because my current encryption does not work on our new server. We are currently using mcrypt so I want to change to openssl.

In our database we use aes encryption which uses a 128bit key so I know what a key is, but I do not know what an openssl iv is? And why would I need a key and an iv.

The code I am about to use is this, which I found on a website because I don't understand encryption very well.

Obviously I will modify it so that the key is kept somewhere else.

function encrypt_decrypt($action, $string) {     $output = false;      $encrypt_method = "AES-256-CBC";     $secret_key = 'This is my secret key';     $secret_iv = 'This is my secret iv';      // hash     $key = hash('sha256', $secret_key);      // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning     $iv = substr(hash('sha256', $secret_iv), 0, 16);      if( $action == 'encrypt' ) {         $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);         $output = base64_encode($output);     }     else if( $action == 'decrypt' ){         $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);     }      return $output; }  $plain_txt = "This is my plain text"; echo "Plain Text = $plain_txt\n";  $encrypted_txt = encrypt_decrypt('encrypt', $plain_txt); echo "Encrypted Text = $encrypted_txt\n";  $decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt); echo "Decrypted Text = $decrypted_txt\n";  if( $plain_txt === $decrypted_txt ) echo "SUCCESS"; else echo "FAILED";  echo "\n"; 
like image 709
Thomas Williams Avatar asked Sep 09 '16 13:09

Thomas Williams


People also ask

What does IV mean in OpenSSL?

An initialization vector (IV) is an arbitrary number that can be used with a secret key for data encryption to foil cyber attacks. This number, also called a nonce (number used once), is employed only one time in any session to prevent unauthorized decryption of the message by a suspicious or malicious actor.

What is IV in OpenSSL enc?

here comes the IV- Initialization vector. This servers as a initial value which can be used as the missing ciphertext block you concatenate to the first block of plaintext. Save this answer.

What is key and IV in AES?

The key file must work only on the corresponding data file. It should not work on any other file, either from the same user or from any other user. AES algorithm requires two different parameters for encryption, a key and an initialization vector (IV).

Why does AES need an IV?

An initialization vector (or IV) are used to ensure that the same value encrypted multiple times, even with the same secret key, will not always result in the same encrypted value. This is an added security layer.


2 Answers

The Initialization Vector is part of what makes AES in CBC (Cipher Block Chaining) mode work - IVs are not unique to OpenSSL. CBC works by XORing the previous block with the current block. The very first block has no previous block, so the IV serves that purpose.

Why this is necessary requires a bit of understanding of how block ciphers work. Without this chaining and IV, we're left with a mode of AES called ECB, or Electronic Code Book. ECB has weaknesses that allow a chosen plaintext attack, among many other problems.

I would recommend spending a bit of time with best practices for CBC initialization vectors. Using them incorrectly can weaken the overall security of AES. The short explanation is:

  • IVs should be random and generated by a CSPRNG.
  • IVs should not be reused. That is, don't encrypt plaintext "A" and plaintext "B" with the same IV. Every record should have its own IV.
  • The IV is not a secret like the key. It can be stored in plaintext along with the cipher text.

Also keep in mind that this advice only applies to AES-CBC. If you ever investigate other modes of AES, such as GCM, this does not apply.

like image 174
vcsjones Avatar answered Sep 29 '22 08:09

vcsjones


Let's say two users have the password "princess", and you encode them with the key "some-key", they will both have the same encrypted result:

| User       | Encrypted password    | |------------|-----------------------| | a          | sN7vIFg=              | | b          | sN7vIFg=              | 

Which means that if someone figures out using a different means that user a's real password is "princess" and their encrypted password is "sN7vIFg=", then any user with the encrypted password "sN7vIFg=" can be deemed to have the password "princess".

However if you use a random IV for each, then it is harder to guess the underlying passwords if you have access to the encrypted data.

| User       | Encrypted password    | IV             | |------------|-----------------------|----------------| | a          | sN7vIFg=              | h²ÓIF8]h%L     | | b          | BjZAzrA=              | VÂøíiøÚØò▓     | 

Now, even if someone has access to the above data, they can't figure out that users a and b have the same password.

See also Is “real salt” the same as “initialization vectors”? .

like image 31
alberto56 Avatar answered Sep 29 '22 07:09

alberto56