3

Given the following to-be-encrypted email, and this (weak) encryption key:

$source="example.email.from.someone@my-office.co.uk";
$pass="Somepassword...";

I want to generate a somewhat good encrypted string:

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$method="AES-128-CBC";

$encrypted=openssl_encrypt($source, $method, $pass, true, $iv);

If I try to decrypt it works fine:

$decrypted=openssl_decrypt ($encrypted, $method, $pass, true, $iv);
echo $decrypted;
// example.email.from.someone@my-office.co.uk

But when I tried to decrypt with a different $iv (!), I expected to get a non-sense result, but instead I got:

$iv2 = "tralala1tralala2";
$decrypted=openssl_decrypt ($encrypted, $method, $pass, true, $iv2);
echo $decrypted;
// m~Œ=¢ì  •wêàdÏŠom.someone@my-office.co.uk

So basically the last 26 characters are decrypted even with a different $iv ("om.someone@my-office.co.uk"). Can someone explain why this happens? (The same 26 chars are decrypted even when I change the $iv again)

I've got this encription method from the best answer here

Community
  • 1
  • 1
verjas
  • 1,793
  • 1
  • 15
  • 18
  • The reason I asked is because I thought IV's will somehow randomize the encryption. But this appears to work only partially. What am I missing? – verjas Jul 22 '15 at 09:22

1 Answers1

3

To understand this, you will need to look into how block cyphers work.

https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation

Commonly (and this includes AES/Rijndael), each block is used to influence the decryption of the next block. The IV merely acts to influence the decryption of the first block (where no previous block exists). So yes, a separate IV will - depending on the exact algorithm used - only impact the decryption of the first block of the cypher text. This is what you are seeing.

0x90
  • 6,079
  • 2
  • 36
  • 55
  • 1
    Thank you! And for the reference, it's more clear now. So, if I understood correctly: The length of a block in AES is 16 bytes, thus the first 16 chars from my email example (which is 42 in length) were affected by the IV, but the remaining 26 chars were not. – verjas Jul 22 '15 at 10:32