What does the ph value mean? TripleDES encryption and decryption between C # and PHP

Source: Internet
Author: User
Tags decrypt md5 hash pkcs7
In C # Common encryption and decryption article, introduced several encryption and decryption methods, including how to use the symmetric encryption algorithm DES, the next des upgrade version, TripleDES.

The relationship between Des and TripleDES can be referenced in the following blog post.
Symmetric encryption des and tripledes

The focus is on how to use TripleDES in c#,php, and then allow both to decrypt the encrypted content sent by the other.

Nonsense not much to say, directly on both ends of the code.

C # code

usingSystem;usingSystem.Security.Cryptography;usingSystem.text;namespace tripledes{class Program {StaticvoidMain (string[] args) {//Encrypted stringstringSource ="Happy Father ' s day!";//Key used for encryption but eventually used in DES encryption key//Is the MD5 hash of this keystringSourcekey ="Home";//Get the MD5 hash of the original key valuebyte[] keybytes = Getkeymd5hash (Sourcekey);stringEncryptedstr = desencrypt (source, keybytes); Console.WriteLine ("Encrypted string:"+ encryptedstr);stringDecryptedstr = Desdecrypt (Encryptedstr, keybytes); Console.WriteLine ("decrypted String:"+ decryptedstr);        Console.readkey (); }///////////   for MD5 hash of the encryption key, use this hash value    for the final DES encryption// ///    original  key value///
     
      
     
   PublicStaticbyte[]Getkeymd5hash(stringKey) {MD5CryptoServiceProvider HASHMD5 =NewMD5CryptoServiceProvider ();byte[] keybytes = Hashmd5.computehash (UTF8Encoding.UTF8.GetBytes (key)); Hashmd5. Clear ();returnKeybytes; }/////   TripleDES encryption       ///////////
     
      
     
   PublicStaticstringDesencrypt(stringToencrypt,byte[] privatekey) {byte[] Toencryptarray = UTF8Encoding.UTF8.GetBytes (Toencrypt); TripleDESCryptoServiceProvider Tdes =NewTripleDESCryptoServiceProvider {Key = Privatekey, Mode = CIPHERMODE.ECB,            Padding = PADDINGMODE.PKCS7}; ICryptoTransform ctransform = tdes. CreateEncryptor ();byte[] Resultarray = Ctransform.transformfinalblock (Toencryptarray,0, toencryptarray.length); Tdes. Clear ();returnConvert.tobase64string (Resultarray,0, resultarray.length); }/////   tripledes decryption       ///////////
     
      
     
   PublicStaticstringDesdecrypt(stringTodecrypt,byte[] privatekey) {//First Base64 decryption because of the encryption when the last to go a base64 encryptionbyte[] enbytes = convert.frombase64string (Todecrypt); TripleDESCryptoServiceProvider Tdes =NewTripleDESCryptoServiceProvider {Key = Privatekey, Mode = CIPHERMODE.ECB,            Padding = PADDINGMODE.PKCS7}; ICryptoTransform ctransform = tdes. CreateDecryptor ();byte[] Resultarray = Ctransform.transformfinalblock (Enbytes,0, enbytes.length); Tdes. Clear ();returnEncoding.UTF8.GetString (resultarray); }    }}

PHP code


    / * TripleDES encryption * / functiondesencrypt($data){//pad for PKCS7$blockSize= Mcrypt_get_block_size (' TripleDES ',' ECB ');$len= Strlen ($data);$pad=$blockSize- ($len%$blockSize);$data. = Str_repeat (Chr ($pad),$pad);$key="Home";$key= MD5 ($key,TRUE);$key. = substr ($key,0,8);//comment This if 168 bits long key//encrypt Data$encData= Mcrypt_encrypt (' TripleDES ',$key,$data,' ECB ');returnBase64_encode ($encData);}/ * TripleDES decryption * / functiondesdecrypt($data) {$key="Home";$key= MD5 ($key,TRUE);$key. = substr ($key,0,8);//decrypt Data$fromBase 64Str= Base64_decode ($data);$decData= Mcrypt_decrypt (' TripleDES ',$key,$fromBase 64Str,' ECB ');return$decData; }/ * Test * /$encryptStr= Desencrypt ("Happy Father ' s day!");Echo"Encrypted string: $ENCRYPTSTR
";$decryptStr= Desdecrypt ($encryptStr);Echo"decrypted string: $decryptStr";?>

Since then, PHP can decrypt C # encrypted content, as well as C # can also decrypt the content of PHP encryption.

Although the TripleDES encryption algorithm, but because the c#,php two language encryption using the default mode,padding are different, if the direct use of their own language TripleDES, will cause both sides to use the same key, encrypt the same content, the encryption results are different, In turn, they cannot decrypt each other.

About the various languages use encryption mode and so on, can be more Google. Just like the RSA encryption algorithm, c#,java,php use the key format is different, JAVA is used asn,c# idiomatic XML, and PHP is accustomed to PEM. This leads to the inability of each other to interact.
P.S. There has been no study of the three methods of RSA interoperability.

Reference article:

Cryptographic algorithm pattern padding

PHP padding PKCS7

How to Add/remove PKCS7 padding from an AES encrypted string

Triple DES with PCSK7 and ECB between PHP and. NET

Today Father's Day, I wish Dad a happy holiday!

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes what is the meaning of the ph value of the TripleDES C # and PHP encryption and decryption, including the meaning of the content of what the ph value, I hope that the PHP tutorial interested in a friend helpful.

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