nirodhasoftware

mcrypt_encrypt

Dec 23rd, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.51 KB | None | 0 0
  1. <?php
  2.  
  3. # --- ENCRYPTION ---
  4.  
  5. # the key should be random binary, use scrypt, bcrypt or PBKDF2 to
  6. # convert a string into a key
  7. # key is specified using hexadecimal
  8. $key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
  9. # ??? This seems like 128 bits or 16 bytes to me (64 hex characters at 4 bits per character)
  10. # ??? Why does the below print out 32 for the key size?
  11. # ??? I thought you said below the key size should be 16 bit for AES-128.
  12.  
  13. # show key size use either 16, 24 or 32 byte keys for AES-128, 192
  14. # and 256 respectively
  15. $key_size =  strlen($key);
  16. echo "Key size: " . $key_size . "\n";
  17.  
  18. $plaintext = "This string was AES-256 / CBC / ZeroBytePadding encrypted.";
  19. # ??? Here you state that the string is encrypted with AES-256, but you
  20. # ??? use MCRYPT_RIJNDAEL_128 below. Why?
  21. # ??? Also what does ZeroBytePadding mean? As a newbie, I have no idea.
  22.  
  23. # create a random IV to use with CBC encoding
  24. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  25. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  26.  
  27. # creates a cipher text compatible with AES (Rijndael block size = 128)
  28. # to keep the text confidential
  29. # only suitable for encoded input that never ends with value 00h
  30. # (because of default zero padding)
  31. $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
  32.                              $plaintext, MCRYPT_MODE_CBC, $iv);
  33. # ??? You say this is only suitable for encoded input that never ends with
  34. # ??? value 00h. Is this something that should be checked? If so, can you
  35. # ??? please provide code to catch this case? I'm not sure what 00h even means.
  36.  
  37. # prepend the IV for it to be available for decryption
  38. $ciphertext = $iv . $ciphertext;
  39.  
  40. # encode the resulting cipher text so it can be represented by a string
  41. $ciphertext_base64 = base64_encode($ciphertext);
  42.  
  43. echo  $ciphertext_base64 . "\n";
  44.  
  45. # === WARNING ===
  46.  
  47. # Resulting cipher text has no integrity or authenticity added
  48. # and is not protected against padding oracle attacks.
  49.  
  50. # --- DECRYPTION ---
  51.  
  52. $ciphertext_dec = base64_decode($ciphertext_base64);
  53.  
  54. # retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
  55. $iv_dec = substr($ciphertext_dec, 0, $iv_size);
  56.  
  57. # retrieves the cipher text (everything except the $iv_size in the front)
  58. $ciphertext_dec = substr($ciphertext_dec, $iv_size);
  59.  
  60. # may remove 00h valued characters from end of plain text
  61. $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,
  62.                                 $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
  63.  
  64. echo  $plaintext_dec . "\n";
  65.  
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment