Notes: des encryption and decryption, php and. net implementation

Source: Internet
Author: User
Note: For des encryption and decryption, php and. net implementation php5.x, you need to add php extension php_mcrypt.

1 class STD3Des 2 {3 private $ key = ""; 4 private $ iv = ""; 5 6/** 7 * structure, pass two keys with base64_encode and IV 8*9 * @ param string $ key10 * @ param string $ iv11 */12 function _ construct ($ KEY, $ iv) 13 {14 if (empty ($ key) | empty ($ iv) {15 echo 'key and iv is not valid'; 16 exit (); 17} 18 $ this-> key = $ key; 19 $ this-> iv = $ iv; 20} 21 22/** 23 * encrypted 24 * @ param
 
  
$ Value25 * @ return
  
   
26 */27 public function encrypt ($ value) 28 {29 $ td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_CBC ,''); 30 $ iv = base64_decode ($ this-> iv); 31 $ value = $ this-> PaddingPKCS7 ($ value); 32 $ key = base64_decode ($ this-> key ); 33 mcrypt_generic_init ($ td, $ key, $ iv); 34 $ ret = base64_encode (mcrypt_generic ($ td, $ value); 35 mcrypt_generic_deinit ($ td ); 36 mcrypt_module_close ($ td); 37 return $ ret; 38} 39 40/** 41 * decrypt 42 * @ param
   
    
$ Value43 * @ 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 58 Private function PaddingPKCS7 ($ data) 59 {60 $ block_size = mcrypt_get_block_size ('tripledes ', 'cbc'); 61 $ padding_char = $ block_size-(strlen ($ data) % $ block_size); 62 $ data. = str_repeat (chr ($ padding_char), $ padding_char); 63 return $ data; 64} 65 66 private function UnPaddingPKCS7 ($ text) 67 {68 $ pad = ord ($ text {strlen ($ text)-1}); 69 if ($ pad> strlen ($ text) {70 return false; 71} 72 if (strspns ($ text, Chr ($ pad), strlen ($ text)-$ pad )! = $ Pad) {73 return false; 74} 75 return substr ($ text, 0,-1 * $ pad ); 76} 77} 78 79 80 // use 81 include ('std3des. class. php '); 82 $ key = 'abcdefgh'; 83 $ iv = 'abcdefgh'; 84 $ msg = 'Test string '; 85 $ des = new STD3Des (base64_encode ($ key), base64_encode ($ iv); 86 $ rs1 = $ des-> encrypt ($ msg); 87 echo $ rs1 .'
     
'; 88 $ rs2 = $ des-> decrypt ($ rs1); 89 echo $ rs2;

. Net version

1 sealed public class CryptoHelper 2 {3 ///4 // Encrypts the specified input. 5 ///6 ///The input.7 ///Key8 ///Iv9 ///
 10 public static string EncryptDes (string input, byte [] key, byte [] iv) 11 {12 if (input = null | input. length = 0) 13 return String. empty; 14 15 DESCryptoServiceProvider des = new DESCryptoServiceProvider (); 16 MemoryStream MS = null; 17 CryptoStream encStream = null; 18 StreamWriter sw = null; 19 string result = String. empty; 20 21 try 22 {23 MS = new MemoryStream (); 24 25 // Create a Crypto Stream using the memory stream and the 26 // csp des key. 27 // des. mode = CipherMode. CBC; 28 // des. padding = PaddingMode. PKCS7; 29 encStream = new CryptoStream (MS, des. createEncryptor (key, iv), CryptoStreamMode. write); 30 31 // Create a StreamWriter to write a string 32 // to the stream. 33 sw = new StreamWriter (encStream); 34 35 // Write the plaintext to the stream. 36 sw. write (input); 37 38 sw. F Lush (); 39 encStream. flushFinalBlock (); 40 ms. flush (); 41 42 43 result = Convert. toBase64String (ms. getBuffer (), 0, Convert. toInt32 (ms. length, CultureInfo. invariantCulture); 44} 45 finally 46 {47 // close objects 48 if (sw! = Null) 49 sw. Close (); 50 if (encStream! = Null) 51 encStream. Close (); 52 if (MS! = Null) 53 ms. Close (); 54} 55 56 // Return the encrypted string 57 return result; 58} 59 ///60 // Decrypts the specified input. 61 ///62 ///The input.63 ///Key64 ///Iv65 ///
 66 public static string DecryptDes (string input, byte [] key, byte [] iv) 67 {68 byte [] buffer; 69 try {buffer = Convert. fromBase64String (input);} 70 catch (System. argumentNullException) {return String. empty;} 71 // length is zero, or not an even multiple of four (plus a few other cases) 72 catch (System. formatException) {return String. empty;} 73 74 DESCryptoServiceProvider des = new DESCrypto ServiceProvider (); 75 MemoryStream MS = null; 76 CryptoStream encStream = null; 77 StreamReader sr = null; 78 string result = String. empty; 79 80 try 81 {82 MS = new MemoryStream (buffer); 83 84 // Create a CryptoStream using the memory stream and the 85 // csp des key. 86 encStream = new CryptoStream (MS, des. createDecryptor (key, iv), CryptoStreamMode. read); 87 88 // Create a StreamReader for read Ing the stream. 89 sr = new StreamReader (encStream); 90 91 // Read the stream as a string. 92 result = sr. readToEnd (); 93} 94 finally 95 {96 // close objects 97 if (sr! = Null) 98 sr. Close (); 99 if (encStream! = Null) 100 encStream. Close (); 101 if (MS! = Null) 102 ms. close (); 103} 104 105 return result; 106} 107 108 109 // call 110 111 string key = "abcdefgh"; 112 string iv = "abcdefgh "; 114 string msg = "test string"; 115 string rs1 = CryptoHelper. encryptDes (msg, System. text. encoding. ASCII. getBytes (key), System. text. encoding. ASCII. getBytes (iv); 116 string rs2 = CryptoHelper. decryptDes (rs1, System. text. encoding. ASCII. getBytes (key), System. text. encoding. ASCII. getBytes (iv ));

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.