Php code encrypted with 3des (compatible with. net)

Source: Internet
Author: User
Php code encrypted with 3des (compatible with. net)

  1. Class Crypt3Des
  2. {
  3. Private $ key = "";
  4. Private $ iv = "";
  5. /**
  6. * Construct and pass two keys and IV that have been base64_encode
  7. *
  8. * @ Param string $ key
  9. * @ Param string $ iv
  10. */
  11. Function _ construct ($ key, $ iv)
  12. {
  13. If (empty ($ key) | empty ($ iv )){
  14. Echo 'key and iv is not valid ';
  15. Exit ();
  16. }
  17. $ This-> key = $ key;
  18. $ This-> iv = $ iv;
  19. }
  20. /**
  21. * Encryption
  22. * @ Param $ Value
  23. * @ Return
  24. */
  25. Public function encrypt ($ value)
  26. {
  27. $ Td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_CBC ,'');
  28. $ Iv = base64_decode ($ this-> iv );
  29. $ Value = $ this-> PaddingPKCS7 ($ value );
  30. $ Key = base64_decode ($ this-> key );
  31. Mcrypt_generic_init ($ td, $ key, $ iv );
  32. $ Ret = base64_encode (mcrypt_generic ($ td, $ value ));
  33. Mcrypt_generic_deinit ($ td );
  34. Mcrypt_module_close ($ td );
  35. Return $ ret;
  36. }
  37. /**
  38. * Decryption
  39. * @ Param $ Value
  40. * @ Return
  41. */
  42. Public function decrypt ($ value)
  43. {
  44. $ Td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_CBC ,'');
  45. $ Iv = base64_decode ($ this-> iv );
  46. $ Key = base64_decode ($ this-> key );
  47. Mcrypt_generic_init ($ td, $ key, $ iv );
  48. $ Ret = trim (mdecrypt_generic ($ td, base64_decode ($ value )));
  49. $ Ret = $ this-> UnPaddingPKCS7 ($ ret );
  50. Mcrypt_generic_deinit ($ td );
  51. Mcrypt_module_close ($ td );
  52. Return $ ret;
  53. }
  54. Private function PaddingPKCS7 ($ data)
  55. {
  56. $ Block_size = mcrypt_get_block_size ('tripledes ', 'cbc ');
  57. $ Padding_char = $ block_size-(strlen ($ data) % $ block_size );
  58. $ Data. = str_repeat (chr ($ padding_char), $ padding_char );
  59. Return $ data;
  60. }
  61. Private function UnPaddingPKCS7 ($ text)
  62. {
  63. $ Pad = ord ($ text {strlen ($ text)-1 });
  64. If ($ pad> strlen ($ text )){
  65. Return false;
  66. }
  67. If (strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad ){
  68. Return false;
  69. }
  70. Return substr ($ text, 0,-1 * $ pad );
  71. }
  72. }
  73. ?>

Articles that you may be interested in: php DES encryption and decryption code example about des encryption and decryption implementation method (php net)

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.