locked
MD5 not avaible

    Question

  • Hello,

    I want to use MD5, but it is not shown up by "System.Security.Cryptography"

    I read a lot to figure it out, but I could find any help. I tried to look for an special MD5 extention, to make sure to have the MD5, but I didn't found anything.

    using System.Security.Cryptography;
    
    ...
    
    function:
    
    MD5 md = new MD5();

    but MD5 is here unknown (underlined in red)

    Sunday, April 13, 2014 4:52 PM

Answers

  • $password = md5(utf8_encode($_POST["password"]));


    this should be the same as

    func()
    {
      string sAlgName = HashAlgorithmNames.Md5;
      string sMsg = "123";
      string sEncodedHash = this.SampleHashMsg(sAlgName, sMsg);
    }
    
    public String SampleHashMsg(String sAlgName, String sMsg)
            {
                IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(sMsg, BinaryStringEncoding.Utf8);
                HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(sAlgName);
                String sAlgNameUsed = objAlgProv.AlgorithmName;
                IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
                if (buffHash.Length != objAlgProv.HashLength)
                {
                    throw new Exception("There was an error creating the hash");
                }
    
                // Convert the hash to a string (for display).
                String strHashBase64 = CryptographicBuffer.EncodeToBase64String(buffHash);
    
                // Return the encoded string
                return strHashBase64;
            }


    I've found after hours a bit different code, which is equal to the PHP encoding in my database

    public static string ComputeMD5(string str)
            {
                var alg = HashAlgorithmProvider.OpenAlgorithm("MD5");
                IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
                var hashed = alg.HashData(buff);
                var res = CryptographicBuffer.EncodeToHexString(hashed);
                return res;
            }

    With this it works!

    Thanks for your help.

    Sunday, April 13, 2014 6:24 PM

All replies

  • Windows Store apps use Windows.System.Cryptography, which has a Md5 | md5 property . You get the MD5 algorithm when you call OpenAlgorithm on your HashAlgorithmProvider. There isn't an explicit MD5 class
    Sunday, April 13, 2014 5:10 PM
    Owner
  • Thanks for your answer, but now I am totally confused.

    I work with a database where I have a string made by the MD5 algorithm. This is made from a webpage in php. when I now compare the file in my database with the OpenAlgorithm, I get a different string.

    Sunday, April 13, 2014 5:45 PM
  • Are you using the same string encoding?
    Sunday, April 13, 2014 6:03 PM
  • $password = md5(utf8_encode($_POST["password"]));


    this should be the same as

    func()
    {
      string sAlgName = HashAlgorithmNames.Md5;
      string sMsg = "123";
      string sEncodedHash = this.SampleHashMsg(sAlgName, sMsg);
    }
    
    public String SampleHashMsg(String sAlgName, String sMsg)
            {
                IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(sMsg, BinaryStringEncoding.Utf8);
                HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(sAlgName);
                String sAlgNameUsed = objAlgProv.AlgorithmName;
                IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
                if (buffHash.Length != objAlgProv.HashLength)
                {
                    throw new Exception("There was an error creating the hash");
                }
    
                // Convert the hash to a string (for display).
                String strHashBase64 = CryptographicBuffer.EncodeToBase64String(buffHash);
    
                // Return the encoded string
                return strHashBase64;
            }


    I've found after hours a bit different code, which is equal to the PHP encoding in my database

    public static string ComputeMD5(string str)
            {
                var alg = HashAlgorithmProvider.OpenAlgorithm("MD5");
                IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
                var hashed = alg.HashData(buff);
                var res = CryptographicBuffer.EncodeToHexString(hashed);
                return res;
            }

    With this it works!

    Thanks for your help.

    Sunday, April 13, 2014 6:24 PM