douyakan8924 2016-04-27 06:03
浏览 128
已采纳

PHP Triple DES加密和兼容的C#解密

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.

  • 写回答

1条回答 默认 最新

  • douxiao0400 2016-04-27 10:01
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MySQL数据库安装时出现这些问题怎么办
  • ¥20 请用MATLAB画含有二重积分和累加求和的随时间t变化的函数图像,不知如何把二重积分加到循环里,请解答一下
  • ¥15 kali终端update时出现这样的情况
  • ¥15 matlab调用stl文件时报错说内存不足
  • ¥20 医学图像格式、医学图像后处理
  • ¥15 MATLAB中的矩阵分离问题
  • ¥15 服务端可运行语言,解析unity文件
  • ¥15 英飞凌TC387使用MCAL唤醒TJA1145问题
  • ¥15 android tv图标显示异常
  • ¥20 (标签-AR|关键词-预测分析)