Home > Backend Development > PHP Tutorial > What does the ph value mean? TripleDES encryption and decryption between C# and PHP

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

WBOY
Release: 2016-07-28 08:29:56
Original
1298 people have browsed it

In the article Common Encryption and Decryption in C#, several encryption and decryption methods are introduced. One of them is how to use the symmetric encryption algorithm DES. This time we will talk about the upgraded version of DES, TripleDES.

The relationship between DES and TripleDES can be found in the following blog post.
Symmetric encryption DES and TripleDES

The key point is how to use TripleDES in C# and PHP respectively, so that the two can decrypt the encrypted content sent by the other party.

No more nonsense, let’s go directly to the code at both ends.

C# code

<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>
Copy after login

PHP code

<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>
Copy after login

In this way, PHP can decrypt the content encrypted by C#, and C# can also decrypt the content encrypted by PHP.

Although they are both TripleDES encryption algorithms, the default mode and padding used for encryption in C# and PHP are different. If you directly use TripleDES that comes with each language, both parties will use the same key to encrypt the same After the content is encrypted, the encryption results are different, and thus they cannot decrypt each other.

For more information on encryption modes used in various languages, you can search Google. Just like the RSA encryption algorithm, C#, JAVA, and PHP use different key formats. JAVA uses ASN, C# uses XML, and PHP uses PEM. This results in the inability to communicate with each other.
p.s. We haven’t found a method for RSA interoperability among the three.

Reference article:

Encryption 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 is Father’s Day, I wish you the best Happy Dad’s Day!

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces what the ph value means. TripleDES encryption and decryption between C# and PHP, including what the ph value means. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template