PHP symmetric encryption Algorithm (DES/AES) class code

Source: Internet
Author: User
Tags mcrypt
  1. /**
  2. * Common symmetric encryption algorithm classes
  3. * Support key: 64/128/256 bit (byte length 8/16/32)
  4. * Support algorithm: Des/aes (based on key length automatic matching using: Des:64bit aes:128/256bit)
  5. * Support mode: CBC/ECB/OFB/CFB
  6. * Ciphertext Code: Base64 string/16 binary string/binary string stream
  7. * Fill mode: 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 function
  21. *
  22. * @param string key
  23. * @param string mode
  24. * @param string vector ("off": Not used/"Auto": Auto/Other: Specify value, length with 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 is 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 '); The CFB must have a vector
  49. Break
  50. Case ' ECB ':
  51. $this->mode = MCRYPT_MODE_ECB;
  52. $iv = ' off '; The ECB does not need vectors
  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. * Get vector values
  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 cipher encoding (Base64/hex/bin)
  93. * @return String cipher
  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 cipher
  118. * @param string cipher 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. }
Copy CodeExamples of Use:
    1. !--? php header (' content-type:text/html; Charset=utf-8; ');
    2. include "xcrypt.php";
    3. Echo '
      //////////////////////////////////////
    4. $a = isset ($_get[' a '])? $_get[' A ']: ' Test 123 ';
    5. //Key
    6. $key = ' 12345678123456781234567812345678 ';//256 bit
    7. $key = ' 1234567812345678 ';//1 The-bit
    8. $key = ' 12345678 ';//64 bit
    9. //Setup mode and IV
    10. $m = new Xcrypt ($key, ' CBC ', ' Auto ');
    11. //Get vector value
    12. echo ' vector: ';
    13. Var_dump ($m->getiv ());
    14. //encryption
    15. $b = $m->encrypt ($a, ' base64 ');
    16. //Decryption
    17. $c = $m->decrypt ($b, ' base64 ');
    18. Echo ' after encryption: ';
    19. Var_dump ($b);
    20. Echo ' after decryption: ';
    21. Var_dump ($c);
    22. /////////////////////////////////////////
    23. echo '
    24. ';
Copy Code

PHP, DES, AES
  • Related Article

    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.