在C#常用加密解密一文中,介紹了幾個加密解密方法,其中有個如何使用對稱加密演算法DES,此次說下DES的升級版,TripleDES。
DES和TripleDES之間的關係可以參考下面的部落格文章。
對稱加密DES和TripleDES
重點內容是,如何分別在C#,PHP中使用TripleDES,然後可以讓二者可以解密對方發送的加密內容。
廢話不多說,直接上兩端的程式碼。
C#代碼
<code><span>using</span> System; <span>using</span> System.Security.Cryptography; <span>using</span> System.Text; namespace TripleDes { class Program { <span>static</span><span>void</span> Main(<span>string</span>[] args) { <span>//加密的字符串</span><span>string</span> source = <span>"Happy Father's Day!"</span>; <span>//用来加密的key 但是最终用在des加密的key</span><span>//是这个key的 md5 hash</span><span>string</span> sourceKey = <span>"home"</span>; <span>//获取原始key值的 md5 hash</span><span>byte</span>[] keyBytes = GetKeyMd5Hash(sourceKey); <span>string</span> encryptedStr = DesEncrypt(source, keyBytes); Console.WriteLine(<span>"encrypted string: "</span>+ encryptedStr); <span>string</span> decryptedStr = DesDecrypt(encryptedStr, keyBytes); Console.WriteLine(<span>"Decrypted String: "</span> + decryptedStr); Console.ReadKey(); } <span><span>///</span><span><summary></span></span><span><span>///</span> 获取加密key的 md5 hash,最终DES加密的时候使用这个hash值</span><span><span>///</span><span></summary></span></span><span><span>///</span><span><param name="key"></span>原始key值<span></param></span></span><span><span>///</span><span><returns></span><span></returns></span></span><span>public</span><span>static</span><span>byte</span>[] <span>GetKeyMd5Hash</span>(<span>string</span> key) { MD5CryptoServiceProvider hashmd5 = <span>new</span> MD5CryptoServiceProvider(); <span>byte</span>[] keyBytes = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); hashmd5.Clear(); <span>return</span> keyBytes; } <span><span>///</span><span><summary></span></span><span><span>///</span> TripleDES 加密</span><span><span>///</span><span></summary></span></span><span><span>///</span><span><param name="toEncrypt"></span><span></param></span></span><span><span>///</span><span><param name="privateKey"></span><span></param></span></span><span><span>///</span><span><returns></span><span></returns></span></span><span>public</span><span>static</span><span>string</span><span>DesEncrypt</span>(<span>string</span> toEncrypt, <span>byte</span>[] privateKey) { <span>byte</span>[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); TripleDESCryptoServiceProvider tdes = <span>new</span> TripleDESCryptoServiceProvider { Key = privateKey, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform cTransform = tdes.CreateEncryptor(); <span>byte</span>[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, <span>0</span>, toEncryptArray.Length); tdes.Clear(); <span>return</span> Convert.ToBase64String(resultArray, <span>0</span>, resultArray.Length); } <span><span>///</span><span><summary></span></span><span><span>///</span> TripleDES解密</span><span><span>///</span><span></summary></span></span><span><span>///</span><span><param name="toDecrypt"></span><span></param></span></span><span><span>///</span><span><param name="privateKey"></span><span></param></span></span><span><span>///</span><span><returns></span><span></returns></span></span><span>public</span><span>static</span><span>string</span><span>DesDecrypt</span>(<span>string</span> toDecrypt, <span>byte</span>[] privateKey) { <span>//先base64解密 因为加密的时候最后走了一道base64加密</span><span>byte</span>[] enBytes = Convert.FromBase64String(toDecrypt); TripleDESCryptoServiceProvider tdes = <span>new</span> TripleDESCryptoServiceProvider { Key = privateKey, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform cTransform = tdes.CreateDecryptor(); <span>byte</span>[] resultArray = cTransform.TransformFinalBlock(enBytes, <span>0</span>, enBytes.Length); tdes.Clear(); <span>return</span> Encoding.UTF8.GetString(resultArray); } } } </code>
PHP代碼
<code><span><span><?php</span><span>/* TripleDES加密 */</span><span><span>function</span><span>DesEncrypt</span><span>(<span>$data</span>)</span> {</span><span>//Pad for PKCS7</span><span>$blockSize</span> = mcrypt_get_block_size(<span>'tripledes'</span>, <span>'ecb'</span>); <span>$len</span> = strlen(<span>$data</span>); <span>$pad</span> = <span>$blockSize</span> - (<span>$len</span> % <span>$blockSize</span>); <span>$data</span> .= str_repeat(chr(<span>$pad</span>), <span>$pad</span>); <span>$key</span> = <span>"home"</span>; <span>$key</span> = md5(<span>$key</span>,<span>TRUE</span>); <span>$key</span> .= substr(<span>$key</span>,<span>0</span>,<span>8</span>); <span>//comment this if you use 168 bits long key</span><span>//Encrypt data</span><span>$encData</span> = mcrypt_encrypt(<span>'tripledes'</span>, <span>$key</span>, <span>$data</span>, <span>'ecb'</span>); <span>return</span> base64_encode(<span>$encData</span>); } <span>/* TripleDES解密 */</span><span><span>function</span><span>DesDecrypt</span><span>(<span>$data</span>)</span> {</span><span>$key</span> = <span>"home"</span>; <span>$key</span> = md5(<span>$key</span>, <span>TRUE</span>); <span>$key</span> .= substr(<span>$key</span>, <span>0</span>, <span>8</span>); <span>//Decrypt data</span><span>$fromBase64Str</span> = base64_decode(<span>$data</span>); <span>$decData</span> = mcrypt_decrypt(<span>'tripledes'</span>, <span>$key</span>, <span>$fromBase64Str</span>, <span>'ecb'</span>); <span>return</span><span>$decData</span>; } <span>/* 测试 */</span><span>$encryptStr</span> = DesEncrypt(<span>"Happy Father's Day!"</span>); <span>echo</span><span>"encrypted string: $encryptStr</br>"</span>; <span>$decryptStr</span> = DesDecrypt(<span>$encryptStr</span>); <span>echo</span><span>"decrypted string: $decryptStr"</span>; <span>?></span></span></code>
這樣以來,PHP就可以解密C#加密後的內容,同樣C#也可以解密PHP加密後的內容。
雖然都是TripleDES加密演算法,但因為C#,PHP兩種語言加密使用的預設mode,padding都不一樣,如果直接使用各自語言自帶的TripleDES,會導致雙方使用相同的金鑰,加密同樣的內容後,加密結果不一樣,進而不能互相解密了。
關於各種語言使用的加密模式等內容,可以多多Google。就像RSA加密演算法,C#,JAVA,PHP三者使用的金鑰格式都不一樣,JAVA慣用ASN,C#慣用XML,而PHP慣用PEM。這樣就導致各自不能互通。
p.s.還沒研究出三者RSA互通的方法。
參考文章:
加密演算法模式填充
PHP padding PKCS7
How to add/remove PKCS7 padding from stran AES encrypan string
Triple DES with PCSK7 and ECB between PHP and .NET今天父親節,祝老爸節日快樂! ').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); }); 以上就介紹了ph值是什麼意思 TripleDES之C#和PHP之間加密解密,包括了ph值是什麼意思方面的內容,希望對PHP教程有興趣的朋友有所幫助。