<?php
$plaintext = "Four score and seven years ago";
$cipher = MCRYPT_TRIPLEDES;
$mode = MCRYPT_MODE_ECB;
$rand_src = MCRYPT_DEV_RANDOM; //MCRYPT_DEV_RANDOM
$password = 'Extra secret password';
print ("Plaintext: $plaintext\n");
// OK, let's encrypt the data
$handle = mcrypt_module_open ($cipher, '', $mode, '');
if (!$handle)
die ("Couldn't locate open mcrypt module for '$cipher' algorithm");
$iv_size = mcrypt_enc_get_iv_size ($handle);
$ivector = mcrypt_create_iv ($iv_size, $rand_src);
if (mcrypt_generic_init ($handle, $password, $ivector) == -1)
die ("Error: mcrypt_generic_init() failed.");
$ciphertext = mcrypt_generic ($handle, $plaintext);
mcrypt_generic_end ($handle);
print ("Ciphertext: " . bin2hex ($ciphertext) . "\n");
// Now let's decrypt it
$handle = mcrypt_module_open ($cipher, '', $mode, '');
if (!$handle)
die ("Couldn't locate open mcrypt module for '$cipher' algorithm");
if (mcrypt_generic_init ($handle, $password, $ivector) == -1)
die ("Error: mcrypt_generic_init() failed.");
$plaintext = mdecrypt_generic ($handle, $ciphertext);
mcrypt_generic_end ($handle);
print ("Plaintext: $plaintext\n");
?>
Above are the codes that have the following warnings. Please help or give me advise to solve the problems.
Plaintext: Four score and seven years ago
Warning: mcrypt_create_iv(): Cannot open source device in d:\program files\apache group\apache\htdocs\mcrypt.php on line 15
Warning: mcrypt_generic_init(): Iv size incorrect; supplied length: 0, needed: 8 in d:\program files\apache group\apache\htdocs\mcrypt.php on line 16
Ciphertext: 53d7433429dc39d220f52d4ab23ae692e76c742253edb320bb
dcf942b6595952
Warning: mcrypt_generic_init(): Iv size incorrect; supplied length: 0, needed: 8 in d:\program files\apache group\apache\htdocs\mcrypt.php on line 27
Plaintext: S×C4)Ü9Ò õ-J²:æ’çlt"Sí³ »ÜùB¶YYR
If i changed the $rand_src = MCRYPT_DEV_RANDOM; to $rand_src = 8; the outputs are :
Plaintext: Four score and seven years ago Ciphertext: 53d7433429dc39d220f52d4ab23ae692e76c742253edb320bb
dcf942b6595952 Plaintext: S×C4)Ü9Ò õ-J²:æ’çlt"Sí³ »ÜùB¶YYR
But why the Plaintext is different than the original plaintext? isn't it supposed to be the same? What's wrong?