-4

I need your help on implementing an encryption process from C# code to sql server.

Here is the method I get in c# code :

    private static string GetPhpMD5(string strPlain) 
    {
        ASCIIEncoding UE = new ASCIIEncoding();
        byte[] HashValue, MessageBytes = UE.GetBytes(strPlain);
        MD5 md5 = new MD5CryptoServiceProvider();
        string strHex = "";

        HashValue = md5.ComputeHash(MessageBytes);
        foreach(byte b in HashValue) 
        {
            strHex += String.Format("{0:x2}", b);
        }
        return strHex;
    } /* GetPhpMD5 */

    GetPhpMD5("nvhG#hdsdsqsd3H");

And so I want to do the same in sql server. I try this but not giving me the same result :

select Hashbytes('MD5', CONVERT(varchar(max),'nvhG#hdsdsqsd3H', 2) )

Result in c# : f4ae6882a7e2aff90d58a29100c5bbf6

Result in sql server : 0xF4AE6882A7E2AFF90D58A29100C5BBF6

Can anyone help me on this please.

Thanks a lot.

david yeah
  • 244
  • 1
  • 4
  • 11
  • 4
    looks totally the same to me – Steve Jul 13 '18 at 20:39
  • 1
    Apart from one being in upper and the other in lower, and being in a binary/hex, they appear identical. I don't really see what the problem is here. The algorithm appears to be the same. – Thom A Jul 13 '18 at 20:40
  • Just uppercase the C# result? Maybe add the "0x" if you need it? Hex values do not care about casing. – Broots Waymb Jul 13 '18 at 20:42
  • 1
    1) `0x` is a prefix indicating "what follows is hexadecimal". It is optional, depending on the context. 2) casing of hexadecimal values does not matter. 3) Even if it did matter, you know how to upper/lower case and prefix strings, don't you? –  Jul 13 '18 at 20:45
  • For further information on the "0x", see [this Q&A](https://stackoverflow.com/questions/2670639/why-are-hexadecimal-numbers-prefixed-with-0x). – Worthwelle Jul 13 '18 at 20:45
  • Thanks for answers, I will try to see that – david yeah Jul 13 '18 at 20:54
  • 1
    Also, encryption != hashing. Encryption implies the possibility of decryption, while (strong) hashing should be one-way-only. (MD5 is fast, but not cryptographically strong) – Corak Jul 13 '18 at 21:11

1 Answers1

0

Just find this way that looks good :

select SUBSTRING(MASTER.dbo.Fn_varbintohexstr(Hashbytes('MD5', CONVERT(varchar(max),'nvhG#hdsdsqsd3H', 2) )), 3, 8000)

Thanks for your help.

david yeah
  • 244
  • 1
  • 4
  • 11