About DES encryption and decryption implementations (PHP net two versions)

Source: Internet
Author: User
  1. /**

  2. DES encryption and decryption
  3. */
  4. Class Std3des
  5. {
  6. Private $key = "";
  7. Private $iv = "";

  8. /**

  9. * Construct, pass two key and IV that have been base64_encode
  10. *
  11. * @param string $key
  12. * @param string $iv
  13. */
  14. function __construct ($key, $iv)
  15. {
  16. if (Empty ($key) | | empty ($IV)) {
  17. Echo ' key and IV is not valid ';
  18. Exit ();
  19. }
  20. $this->key = $key;
  21. $this->iv = $iv;
  22. }

  23. /**

  24. * Encryption
  25. * @param $value
  26. * @return
  27. */
  28. Public function Encrypt ($value)
  29. {
  30. $TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_CBC, ');
  31. $iv = Base64_decode ($this->iv);
  32. $value = $this->PADDINGPKCS7 ($value);
  33. $key = Base64_decode ($this->key);
  34. Mcrypt_generic_init ($TD, $key, $IV);
  35. $ret = Base64_encode (Mcrypt_generic ($TD, $value));
  36. Mcrypt_generic_deinit ($TD);
  37. Mcrypt_module_close ($TD);
  38. return $ret;
  39. }

  40. /**

  41. * Decryption
  42. * @param $value
  43. * @return
  44. */
  45. Public function decrypt ($value)
  46. {
  47. $TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_CBC, ');
  48. $iv = Base64_decode ($this->iv);
  49. $key = Base64_decode ($this->key);
  50. Mcrypt_generic_init ($TD, $key, $IV);
  51. $ret = Trim (Mdecrypt_generic ($TD, Base64_decode ($value)));
  52. $ret = $this->UNPADDINGPKCS7 ($ret);
  53. Mcrypt_generic_deinit ($TD);
  54. Mcrypt_module_close ($TD);
  55. return $ret;
  56. }

  57. Private Function PaddingPKCS7 ($data)

  58. {
  59. $block _size = mcrypt_get_block_size (' TripleDES ', ' CBC ');
  60. $padding _char = $block _size-(strlen ($data)% $block _size);
  61. $data. = Str_repeat (Chr ($padding _char), $padding _char);
  62. return $data;
  63. }

  64. Private Function UnPaddingPKCS7 ($text)

  65. {
  66. $pad = Ord ($text {strlen ($text)-1});
  67. if ($pad > strlen ($text)) {
  68. return false;
  69. }
  70. if (strspn ($text, Chr ($pad), strlen ($text)-$pad)! = $pad) {
  71. return false;
  72. }
  73. Return substr ($text, 0,-1 * $pad);
  74. }
  75. }

  76. Use

  77. Include (' STD3Des.class.php ');
  78. $key = ' abcdefgh ';
  79. $iv = ' abcdefgh ';
  80. $msg = ' Test string ';
  81. $des =new std3des (Base64_encode ($key), Base64_encode ($IV));
  82. $rs 1= $des->encrypt ($msg);
  83. echo $rs 1. '
    ';
  84. $rs 2= $des->decrypt ($rs 1);
  85. echo $rs 2;

Copy Code

2. NET version

  1. DES encryption and decryption

  2. Sealed public class Cryptohelper
  3. {
  4. ///
  5. Encrypts the specified input.
  6. ///
  7. the input.
  8. Key
  9. IV
  10. ///
  11. public static string Encryptdes (string input, byte[] key, byte[] IV)
  12. {
  13. if (input = = NULL | | input. Length = = 0)
  14. return String.Empty;

  15. DESCryptoServiceProvider des = new DESCryptoServiceProvider ();

  16. MemoryStream ms = NULL;
  17. CryptoStream encstream = null;
  18. StreamWriter SW = null;
  19. string result = String.Empty;

  20. Try

  21. {
  22. ms = new MemoryStream ();

  23. Create a CryptoStream using the memory stream and the

  24. CSP DES key.
  25. Des. Mode = CIPHERMODE.CBC;
  26. Des. Padding = PADDINGMODE.PKCS7;
  27. Encstream = new CryptoStream (MS, Des. CreateEncryptor (Key, iv), CryptoStreamMode.Write);

  28. Create a StreamWriter to write a string

  29. to the stream.
  30. SW = new StreamWriter (encstream);

  31. Write the plaintext to the stream.

  32. Sw. Write (input);

  33. Sw. Flush ();

  34. Encstream.flushfinalblock ();
  35. Ms. Flush ();

  36. result = Convert.tobase64string (Ms. GetBuffer (), 0, Convert.ToInt32 (Ms. Length, CultureInfo.InvariantCulture));

  37. }
  38. Finally
  39. {
  40. Close objects
  41. if (sw! = NULL)
  42. Sw. Close ();
  43. if (encstream! = null)
  44. Encstream.close ();
  45. if (ms! = NULL)
  46. Ms. Close ();
  47. }

  48. Return the encrypted string

  49. return result;
  50. }
  51. ///
  52. Decrypts the specified input.
  53. ///
  54. the input.
  55. Key
  56. IV
  57. ///
  58. public static string Decryptdes (string input, byte[] key, byte[] IV)
  59. {
  60. byte[] buffer;
  61. try {buffer = convert.frombase64string (input);}
  62. catch (system.argumentnullexception) {return string.empty;}
  63. Length is zero, or the even multiple of four (plus a few other cases)
  64. catch (system.formatexception) {return string.empty;}

  65. DESCryptoServiceProvider des = new DESCryptoServiceProvider ();

  66. MemoryStream ms = NULL;
  67. CryptoStream encstream = null;
  68. StreamReader sr = null;
  69. string result = String.Empty;

  70. Try

  71. {
  72. ms = new MemoryStream (buffer);

  73. Create a CryptoStream using the memory stream and the

  74. CSP DES key.
  75. Encstream = new CryptoStream (MS, Des. CreateDecryptor (Key, iv), CryptoStreamMode.Read);

  76. Create a StreamReader for reading the stream.

  77. sr = new StreamReader (encstream);

  78. Read the stream as a string.

  79. result = Sr. ReadToEnd ();
  80. }
  81. Finally
  82. {
  83. Close objects
  84. if (SR! = NULL)
  85. Sr. Close ();
  86. if (encstream! = null)
  87. Encstream.close ();
  88. if (ms! = NULL)
  89. Ms. Close ();
  90. }

  91. return result;

  92. }
  93. }

  94. Call

  95. string key = "ABCDEFGH";
  96. String IV = "ABCDEFGH";
  97. String msg= "test string";
  98. String rs1 = Cryptohelper.encryptdes (msg, System.Text.Encoding.ASCII.GetBytes (key), System.Text.Encoding.ASCII.GetBytes (iv));
  99. string rs2 = Cryptohelper.decryptdes (Rs1, System.Text.Encoding.ASCII.GetBytes (key), System.Text.Encoding.ASCII.GetBytes (iv));

Copy Code

After reading the above two pieces of code, do not know you have harvest? Personal prefer the PHP version of the DES Encryption and decryption method, concise and clear. Scripting Academy (bbs.it-home.org), focus on you.

  • 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.