Php symmetric encryption algorithm (DES/AES) code

Source: Internet
Author: User
Php symmetric encryption algorithm (DESAES) code

  1. /**
  2. * Common symmetric encryption algorithms
  3. * Supported keys: 64/128/256 bit (byte length: 8/16/32)
  4. * Supported algorithms: DES/AES (auto match based on key length: DES: 64bit AES: 128/256 bit)
  5. * Supported mode: CBC/ECB/OFB/CFB
  6. * Ciphertext encoding: base64 string/hexadecimal string/binary string stream
  7. * Filling method: PKCS5Padding (DES)
  8. *
  9. * @ Author: linvo
  10. * @ Version: 1.0.0
  11. * @ Date: 2013/1/10
  12. */
  13. Class Xcrypt {
  14. Private $ mcrypt;
  15. Private $ key;
  16. Private $ mode;
  17. Private $ iv;
  18. Private $ blocksize;
  19. /**
  20. * Constructor
  21. *
  22. * @ Param string key
  23. * @ Param string mode
  24. * @ Param string vector ("off": not used/"auto": Automatic/Others: Specified value, same length as the key)
  25. */
  26. Public function _ construct ($ key, $ mode = 'cbc', $ iv = "off "){
  27. Switch (strlen ($ key )){
  28. Case 8:
  29. $ This-> mcrypt = MCRYPT_DES;
  30. Break;
  31. Case 16:
  32. $ This-> mcrypt = MCRYPT_RIJNDAEL_128;
  33. Break;
  34. Case 32:
  35. $ This-> mcrypt = MCRYPT_RIJNDAEL_256;
  36. Break;
  37. Default:
  38. Die ("Key size must be 8/16/32 ");
  39. }
  40. $ This-> key = $ key;
  41. Switch (strtolower ($ mode )){
  42. Case 'ofb ':
  43. $ This-> mode = MCRYPT_MODE_OFB;
  44. If ($ iv = 'off') die ('ofb must give a IV'); // OFB must have a vector
  45. Break;
  46. Case 'cfb ':
  47. $ This-> mode = MCRYPT_MODE_CFB;
  48. If ($ iv = 'off') die ('cfb must give a IV'); // CFB must have a vector
  49. Break;
  50. Case 'ECB ':
  51. $ This-> mode = MCRYPT_MODE_ECB;
  52. $ Iv = 'off'; // vector not required by ECB
  53. Break;
  54. Case 'cbc ':
  55. Default:
  56. $ This-> mode = MCRYPT_MODE_CBC;
  57. }
  58. Switch (strtolower ($ iv )){
  59. Case "off ":
  60. $ This-> iv = null;
  61. Break;
  62. Case "auto ":
  63. $ Source = PHP_ OS = 'winnt '? MCRYPT_RAND: MCRYPT_DEV_RANDOM;
  64. $ This-> iv = mcrypt_create_iv (mcrypt_get_block_size ($ this-> mcrypt, $ this-> mode), $ source );
  65. Break;
  66. Default:
  67. $ This-> iv = $ iv;
  68. }
  69. }
  70. /**
  71. * Obtain vector value
  72. * @ Param string vector value encoding (base64/hex/bin)
  73. * @ Return string vector value
  74. */
  75. Public function getIV ($ code = 'base64 '){
  76. Switch ($ code ){
  77. Case 'base64 ':
  78. $ Ret = base64_encode ($ this-> iv );
  79. Break;
  80. Case 'Hex ':
  81. $ Ret = bin2hex ($ this-> iv );
  82. Break;
  83. Case 'bin ':
  84. Default:
  85. $ Ret = $ this-> iv;
  86. }
  87. Return $ ret;
  88. }
  89. /**
  90. * Encryption
  91. * @ Param string plaintext
  92. * @ Param string ciphertext encoding (base64/hex/bin)
  93. * @ Return string ciphertext
  94. */
  95. Public function encrypt ($ str, $ code = 'base64 '){
  96. If ($ this-> mcrypt = MCRYPT_DES) $ str = $ this-> _ pkcs5Pad ($ str );
  97. If (isset ($ this-> iv )){
  98. $ Result = mcrypt_encrypt ($ this-> mcrypt, $ this-> key, $ str, $ this-> mode, $ this-> iv );
  99. } Else {
  100. @ $ Result = mcrypt_encrypt ($ this-> mcrypt, $ this-> key, $ str, $ this-> mode );
  101. }
  102. Switch ($ code ){
  103. Case 'base64 ':
  104. $ Ret = base64_encode ($ result );
  105. Break;
  106. Case 'Hex ':
  107. $ Ret = bin2hex ($ result );
  108. Break;
  109. Case 'bin ':
  110. Default:
  111. $ Ret = $ result;
  112. }
  113. Return $ ret;
  114. }
  115. /**
  116. * Decryption
  117. * @ Param string ciphertext
  118. * @ Param string ciphertext encoding (base64/hex/bin)
  119. * @ Return string plaintext
  120. */
  121. Public function decrypt ($ str, $ code = "base64 "){
  122. $ Ret = false;
  123. Switch ($ code ){
  124. Case 'base64 ':
  125. $ Str = base64_decode ($ str );
  126. Break;
  127. Case 'Hex ':
  128. $ Str = $ this-> _ hex2bin ($ str );
  129. Break;
  130. Case 'bin ':
  131. Default:
  132. }
  133. If ($ str! = False ){
  134. If (isset ($ this-> iv )){
  135. $ Ret = mcrypt_decrypt ($ this-> mcrypt, $ this-> key, $ str, $ this-> mode, $ this-> iv );
  136. } Else {
  137. @ $ Ret = mcrypt_decrypt ($ this-> mcrypt, $ this-> key, $ str, $ this-> mode );
  138. }
  139. If ($ this-> mcrypt = MCRYPT_DES) $ ret = $ this-> _ pkcs5Unpad ($ ret );
  140. }
  141. Return $ ret;
  142. }
  143. Private function _ pkcs5Pad ($ text ){
  144. $ This-> blocksize = mcrypt_get_block_size ($ this-> mcrypt, $ this-> mode );
  145. $ Pad = $ this-> blocksize-(strlen ($ text) % $ this-> blocksize );
  146. Return $ text. str_repeat (chr ($ pad), $ pad );
  147. }
  148. Private function _ pkcs5Unpad ($ text ){
  149. $ Pad = ord ($ text {strlen ($ text)-1 });
  150. If ($ pad> strlen ($ text) return false;
  151. If (strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad) return false;
  152. $ Ret = substr ($ text, 0,-1 * $ pad );
  153. Return $ ret;
  154. }
  155. Private function _ hex2bin ($ hex = false ){
  156. $ Ret = $ hex! = False & preg_match ('/^ [0-9a-fA-F] + $/I', $ hex )? Pack ("H *", $ hex): false;
  157. Return $ ret;
  158. }
  159. }

Example:

  1. Header ('content-Type: text/html; Charset = utf-8 ;');
  2. Include "xcrypt. php ";
  3. Echo'
    ';
  4. //////////////////////////////////////
  5. $ A = isset ($ _ GET ['A'])? $ _ GET ['A']: 'Test 100 ';
  6. // Key
  7. $ Key = '000000'; // 12345678123456781234567812345678 bit
  8. $ Key = '000000'; // 1234567812345678 bit
  9. $ Key = '000000'; // 64 bit
  10. // Set the mode and IV
  11. $ M = new Xcrypt ($ key, 'cbc', 'auto ');
  12. // Obtain the vector value
  13. Echo 'vector :';
  14. Var_dump ($ m-> getIV ());
  15. // Encryption
  16. $ B = $ m-> encrypt ($ a, 'base64 ');
  17. // Decrypt
  18. $ C = $ m-> decrypt ($ B, 'base64 ');
  19. Echo 'encrypted :';
  20. Var_dump ($ B );
  21. Echo 'decrypted :';
  22. Var_dump ($ c );
  23. /////////////////////////////////////////
  24. Echo'
  25. ';



Php, DES, AES

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.