MD5加密中的特殊字符

Special characters in MD5 encryption

我在 C# 和 SQL 服务器中遇到 MD5 加密问题,它只发生在具有特殊字符的行上。

这是 c# 中的代码:

public virtual string RowHash<T>(T item)
    {
        PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        var finalvalue = "";
        foreach (PropertyInfo p in properties)
        {
            if (p.Name != "Hash")
            {
                if (!p.CanWrite || !p.CanRead) { continue; }

                MethodInfo mget = p.GetGetMethod(false);
                MethodInfo mset = p.GetSetMethod(false);

                // Get and set methods have to be public
                if (mget == null) { continue; }
                if (mset == null) { continue; }
                var value = p.GetValue(item, null) == null ? "" : p.GetValue(item, null) is Entity? ((Entity)p.GetValue(item, null)).Id.ToString() : p.GetValue(item, null).ToString();
                finalvalue += value;
            }

        }
        return finalvalue;
    }

    public static string GetMD5(string text)
    {
        var md5 = MD5CryptoServiceProvider.Create();
        var encoding = new ASCIIEncoding();
        byte[] stream = null;

        var sb = new StringBuilder();
        stream = md5.ComputeHash(encoding.GetBytes(text));

        for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);

        return sb.ToString();
    }

    public static string PasswordMD5(string password)
    {
        var pwd = GetMD5(password + GetMD5(password).Substring(0,2));
        return pwd;
    }

使用 RowHash 方法,我将 Row 中的所有字段连接起来,除非将存储结果的字段散列,然后我将它发送到方法 PasswordMD5,该方法创建我们保存在数据库中的加密散列。

然后我在 SQL 服务器中使用此代码执行相同操作:

CONVERT(VARCHAR(32), HashBytes('MD5',CONVERT(VARCHAR(MAX),CONVERT(VARCHAR(MAX),ISNULL([Field1],''))+ISNULL([Field2],'')+ISNULL([Field3],'')+CONVERT(VARCHAR(MAX),ISNULL([Field4],''))+ISNULL(CONVERT(VARCHAR(MAX),[Field5]),'')+ISNULL(CONVERT(VARCHAR(MAX),Field6]),'')+CONVERT(VARCHAR(MAX),Field7]))+SUBSTRING(CONVERT(VARCHAR(32),HashBytes('MD5',CONVERT(VARCHAR(MAX),CONVERT(VARCHAR(MAX),ISNULL([Field1],''))+ISNULL([Field2],'')+ISNULL([Field3],'')+CONVERT(VARCHAR(MAX),ISNULL([Field4],''))+ISNULL(CONVERT(VARCHAR(MAX),Field5]),'')+ISNULL(CONVERT(VARCHAR(MAX),[Field6]),'')+CONVERT(VARCHAR(MAX),Field7]))) 2), 1, )), 2)

然后我将它与我在 C# 中创建的哈希进行比较,以检查我在数据库中拥有的数据的完整性。这对于没有特殊字符的所有行都非常有效,但对于包含任何特殊字符(如 é 或 ö)的所有行都会失败。

这是我前段时间遇到的问题,但是任务被其他优先级暂停了,我不记得我已经尝试过哪些解决方案,我记得我尝试了一些解决方案改变字符串的编码C# 但 none 有效。

我做错了什么?

提前致谢。

我对我的代码使用了这种加密和解密方法,我已经用特殊字符进行了测试,它对我来说工作正常。我会把我的代码发给你,请测试一下,希望它对你有用。

TableFilter 是普通的class 文件,文件包含加密和解密方法没有别的你可以在.cs 页面上写这两个方法。

public static string Encrypt(string toEncrypt, bool useHashing = true)
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

        System.Configuration.AppSettingsReader settingsReader =
                                            new AppSettingsReader();
        // Get the key from config file

        string key = (string)settingsReader.GetValue("EncryptionKey",
                                                         typeof(String));
        //System.Windows.Forms.MessageBox.Show(key);
        //If hashing use get hashcode regards to your key
        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //Always release the resources and flush data
            // of the Cryptographic service provide. Best Practice

            hashmd5.Clear();
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)
        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)

        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        //transform the specified region of bytes array to resultArray
        byte[] resultArray =
          cTransform.TransformFinalBlock(toEncryptArray, 0,
          toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor
        tdes.Clear();
        //Return the encrypted data into unreadable string format
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }

    public static string Decrypt(string cipherString, bool useHashing = true)
    {
        byte[] keyArray;
        //get the byte code of the string

        byte[] toEncryptArray = Convert.FromBase64String(cipherString);

        System.Configuration.AppSettingsReader settingsReader =
                                            new AppSettingsReader();
        //Get your key from config file to open the lock!
        string key = (string)settingsReader.GetValue("EncryptionKey",
                                                     typeof(String));

        if (useHashing)
        {
            //if hashing was used get the hash code with regards to your key
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //release any resource held by the MD5CryptoServiceProvider

            hashmd5.Clear();
        }
        else
        {
            //if hashing was not implemented get the byte code of the key
            keyArray = UTF8Encoding.UTF8.GetBytes(key);
        }

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes. 
        //We choose ECB(Electronic code Book)

        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(
                             toEncryptArray, 0, toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor                
        tdes.Clear();
        //return the Clear decrypted TEXT
        return UTF8Encoding.UTF8.GetString(resultArray);
    }

:对于文本框字符串(而不是 txtpassword.text 把 testingé123ö 用于理解))

login_Model.Password = TableFilter.Encrypt("testingé123ö");

: Web.config


    <add key="EncryptionKey" value="encyptionkeyvalue"/>

最后我找不到使用 MD5 的好的解决方案,我转向 SHA256 并使用这个答案工作: