欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

关于des加密与解密实现方法(php net两个版本)

程序员文章站 2022-05-16 14:42:26
...
  1. /**

  2. des 加密 解密
  3. */
  4. class STD3Des
  5. {
  6. private $key = "";
  7. private $iv = "";
  8. /**

  9. * 构造,传递二个已经进行base64_encode的KEY与IV
  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. *加密
  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. *解密
  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. //使用

  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. $rs1=$des->encrypt($msg);
  83. echo $rs1.'
    ';
  84. $rs2=$des->decrypt($rs1);
  85. echo $rs2;
复制代码

2、.net版本

  1. //des 加密 解密

  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 not an 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. //调用

  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));
复制代码

看完以上两段代码,不知道你有收获没有?个人比较喜欢php版的des加密与解密方法,简洁清晰。 脚本学堂(bbs.it-home.org),专心为您。