PHP/Utility Function/mcrypt encrypt

Материал из Web эксперт
Перейти к: навигация, поиск

mcrypt_encrypt.php

   <source lang="html4strict">

<?php

  $ivs = mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CBC);
  $iv = mcrypt_create_iv($ivs, MCRYPT_RAND);
  $key = "F925T";
  $message = "This is a test.";
  $enc = mcrypt_encrypt(MCRYPT_DES, $key, $message, MCRYPT_MODE_CBC, $iv);
  echo bin2hex($enc);

?>

 </source>
   
  


Stronger Encryption Algorithms

   <source lang="html4strict">

<?php $plaintext = "The test string"; $password = "enigma"; $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); srand(); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $ciphertext = mcrypt_encrypt(MCRYPT_BLOWFISH, $password, $plaintext,MCRYPT_MODE_ECB, $iv); file_put_contents("secret_message.txt", $iv . $ciphertext); $messagedata = file_get_contents("secret_message.txt"); $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); $iv = substr($messagedata, 0, $iv_size); $ciphertext = substr($messagedata, $iv_size); $password = "enigma"; $plaintext = mcrypt_decrypt(MCRYPT_BLOWFISH, $password, $ciphertext,

                           MCRYPT_MODE_ECB, $iv);

?>

 </source>