PHP AES Algorithm

Source: Internet
Author: User
PHP AES Algorithm




  1. function Aes128cbcencrypt ($key, $text)
  2. {
  3. /**
  4. * Open the cipher
  5. */
  6. $TD = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_CBC, ');
  7. if (! $td)
  8. {
  9. throw new Generalsecurityexception (' Invalid mcrypt cipher, check your Libmcrypt library and Php-mcrypt extention ');
  10. }
  11. Replaced Mcrypt_dev_random with Mcrypt_rand since Windows doesn ' t have/dev/rand:)
  12. Srand (Double) microtime () * 1000000);
  13. $iv = Mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);
  14. /**
  15. * Intialize Encryption
  16. */
  17. Mcrypt_generic_init ($TD, $key, $IV);
  18. /**
  19. * Encrypt Data
  20. */
  21. $encrypted = Mcrypt_generic ($TD, $text);
  22. /**
  23. * Terminate Encryption Handler
  24. */
  25. Mcrypt_generic_deinit ($TD);
  26. /**
  27. * AES-128-CBC encryption. The IV is returned as the first bytes
  28. * of the cipher text.
  29. */
  30. Return $IV. $encrypted;
  31. }
  32. function Aes128cbcdecrypt ($key, $encrypted _text)
  33. {
  34. /**
  35. * Open the cipher
  36. */
  37. $TD = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_CBC, ');
  38. if (is_callable (' mb_substr '))
  39. {
  40. $iv = Mb_substr ($encrypted _text, 0, crypto_cipher_block_size, ' latin1 ');
  41. }
  42. Else
  43. {
  44. $iv = substr ($encrypted _text, 0, crypto_cipher_block_size);
  45. }
  46. /**
  47. * Initialize encryption module for decryption
  48. */
  49. Mcrypt_generic_init ($TD, $key, $IV);
  50. /**
  51. * Decrypt Encrypted string
  52. */
  53. if (is_callable (' mb_substr '))
  54. {
  55. $encrypted = Mb_substr ($encrypted _text, Crypto_cipher_block_size, Mb_strlen ($encrypted _text, ' latin1 '), ' latin1 ');
  56. }
  57. Else
  58. {
  59. $encrypted = substr ($encrypted _text, crypto_cipher_block_size);
  60. }
  61. $decrypted = Mdecrypt_generic ($TD, $encrypted);
  62. /**
  63. * Terminate decryption handle and close module
  64. */
  65. Mcrypt_generic_deinit ($TD);
  66. Mcrypt_module_close ($TD);
  67. /**
  68. * Show String
  69. */
  70. Return trim ($decrypted);
  71. }
  72. Define (' Crypto_cipher_block_size ', 16);
  73. $a = aes128cbcencrypt (' Pass ', ' This is Text ');
  74. echo Base64_encode ($a). "/r/n";
  75. $b = Aes128cbcdecrypt (' Pass ', $a);
  76. Echo $b. "/r/n";
  77. $c = Aes128cbcdecrypt (' Pass ', Base64_decode (Base64_encode ($a)));
  78. Echo $c. "/r/n";
  79. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.