duai8153 2015-04-28 13:38
浏览 88

C#md5 Hash和Php

I have encrypt a string in c# with md5 hash and stored it to mysql database with code below.

function getMd5Hash(string md5)
{
    string md5;
    MD5 md5hash = new MD5CryptoServiceProvider();
    md5hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass));
    byte[] result = md5hash.Hash;

    StringBuilder strbuilder = new StringBuilder();
    for (int i = 0; i < result.Length; i++)
    {
        strbuilder.Append(result[i].ToString("x2"));
    }
    md5 = strbuilder.ToString();
}

//salt is ten random character
string pass = getMd5Hash(getMd5Hash("fermentasi")+salt);

Now, how can I hashing same string in PHP to get match value with my function in c# ? I have been searching but didn't find a solution so far. Thanks for Helping and sorry for my bad english :)

  • 写回答

1条回答 默认 最新

  • duancan65665 2015-04-28 13:55
    关注

    I recently came across this same issue and found a pretty good resource here. We were also trying to encrypt an item from a PHP form, store it in MySQL and hopefully decrypt it in (and for use in) a C# application. It uses AES which is much more secure than MD5 as well. Here is a snipet of my PHP code

    function encrypt($text) {
    
        $iv = "45287112549354892144548565456541";
        $key = "anjueolkdiwpoida";
    
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
    
        $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv]);
    
    
        $crypttext64=base64_encode($crypttext);
    
        return $crypttext64;
    
    }
    
    function decrypt ($text) {
    
        $iv = "45287112549354892144548565456541";
        $key = "anjueolkdiwpoida";
    
        $crypttext64=base64_decode($text);
    
        $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256 ,  $key,  $crypttext64 ,  MCRYPT_MODE_CBC, $key );
    
        return $decrypted;
    
    }
    

    So all you need to do is call encrypt($text_to_encrypt) before storing it in the database, and you can call decrypt($encrypted_text) to take it back out. However, as I was only on the PHP side, you might need to refer to the previous link to see what goes into decrypting it on the C# side. Apologies for only a half-answer.

    And this will only work if you have control of the C# too and you have the option of moving away from the MD5.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿