简体   繁体   中英

PHP Triple DES encryption and compatible C# decryption

I am decrypting a message in C#, this message is encrypted in PHP using below code:-

public function __construct($sEncryptionKey)
{
    $this->link = mcrypt_module_open('tripledes', '', 'ecb', '');
    $this->sInitializationVector = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->link), MCRYPT_RAND);
    $this->iKeySize = mcrypt_enc_get_key_size($this->link);
    $sEncryptionKey = substr(md5($sEncryptionKey), 0, $this->iKeySize);
    mcrypt_generic_init($this->link, $sEncryptionKey, $this->sInitializationVector);
}

public function encrypt($sDataToEncrypt)
{
    return base64_encode(mcrypt_generic($this->link, $sDataToEncrypt));
}

And I am using below decryption function in c# for decryption:-

public string Decrypt(string toDecrypt, string key, bool useHashing)
{                      
    byte[] keyArray;
    byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);    
    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        hashmd5.Clear();
    }
    else
    {
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
    }

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();               
    tdes.Key = keyArray;                                 
    tdes.Mode = CipherMode.ECB;                
    tdes.Padding = PaddingMode.Zeros;

    ICryptoTransform cTransform = tdes.CreateDecryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);                              
    tdes.Clear();
    var strValue = UTF8Encoding.UTF8.GetString(resultArray);    
    return UTF8Encoding.UTF8.GetString(resultArray);
}

I tried with few changes and getting below results:-

1.) PaddingMode.PKCS7 with no hashing = "Specified key is not a valid size for this algorithm."

2.) PaddingMode.PKCS7 with hashing = "Bad Data."

3.) PaddingMode.Zeros with no hashing = "Specified key is not a valid size for this algorithm."

4.) PaddingMode.Zeros with hashing = " 8 f q6IGs " some unknown characters

I think 4'th one will work, but not sure what I am doing wrong.

Found a solution, need to do changes in MD5 hashing, below are whole code if someone stuck in same situation :-

public string Decrypt(string toDecrypt, string key, bool useHashing)
{
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    byte[] keyArray;
    byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
    string keyArrayStr = "";
    if (useHashing)
    {
        MD5 md5 = MD5CryptoServiceProvider.Create();
        byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(key));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < dataMd5.Length; i++)
            sb.AppendFormat("{0:x2}", dataMd5[i]);

        keyArrayStr = sb.ToString().Substring(0, tdes.Key.Length);
        keyArray = UTF8Encoding.UTF8.GetBytes(keyArrayStr);
    }
    else
    {
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
    }

    tdes.Key = keyArray;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.Zeros;

    ICryptoTransform cTransform = tdes.CreateDecryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    tdes.Clear();
    var strValue = UTF8Encoding.UTF8.GetString(resultArray);
    return UTF8Encoding.UTF8.GetString(resultArray);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM