在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></summary></span></span><span><span>///</span> 获取加密key的 md5 hash,最终DES加密的时候使用这个hash值</span><span><span>///</span><span></span></span><span><span>///</span><span><param name="key"></span>原始key值<span></span></span><span><span>///</span><span><returns></returns></span><span></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></summary></span></span><span><span>///</span> TripleDES 加密</span><span><span>///</span><span></span></span><span><span>///</span><span><param name="toEncrypt"></span><span></span></span><span><span>///</span><span><param name="privateKey"></span><span></span></span><span><span>///</span><span><returns></returns></span><span></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></summary></span></span><span><span>///</span> TripleDES解密</span><span><span>///</span><span></span></span><span><span>///</span><span><param name="toDecrypt"></span><span></span></span><span><span>///</span><span><param name="privateKey"></span><span></span></span><span><span>///</span><span><returns></returns></span><span></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"</span>; <span>$decryptStr</span> = DesDecrypt(<span>$encryptStr</span>); <span>echo</span><span>"decrypted string: $decryptStr"</span>; <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 an AES encrypted 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教程有兴趣的朋友有所帮助。