解码字符串时哈希值不匹配

时间:2014-07-10 06:10:50

标签: c# c#-4.0 cryptography string-decoding

我有以下代码。它一直在我的临时和预生产环境以及生产环境中工作。

它是如何突然停止在生产环境中工作的。它仍然适用于前期制作和制作。

它正在抛出"哈希值不匹配"错误意味着storedHash!= calcHash。

为什么这可能只发生在3种环境中?

static public string StrDec(string value, string key)
{
    String dataValue = "";
    String calcHash = "";
    String storedHash = "";

    MACTripleDES mac3des = new MACTripleDES();
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    mac3des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
    try
    {
       string strRemoveSpace = value.Replace(" ", "+");
       dataValue = Encoding.UTF8.GetString(System.Convert.FromBase64String(strRemoveSpace.Split(System.Convert.ToChar("-"))[0]));
       storedHash = Encoding.UTF8.GetString(System.Convert.FromBase64String(strRemoveSpace.Split(System.Convert.ToChar("-"))[1]));
       calcHash = Encoding.UTF8.GetString(mac3des.ComputeHash(Encoding.UTF8.GetBytes(dataValue)));

        if (storedHash != calcHash)
        {
            //Throw exception because data was corrupted here
            throw new ArgumentException("Hash value does not match");
        }
    }
    catch (System.Exception ex)
    {
        //Catch System.Exception  here
    }
    return dataValue;
}

2 个答案:

答案 0 :(得分:1)

这是问题 - 或者至少是 问题:

Encoding.UTF8.GetString(mac3des.ComputeHash(Encoding.UTF8.GetBytes(dataValue)));

......前几行很可能相同。

您正在使用任意二进制数据调用Encoding.UTF8.GetString不是 UTF-8编码的字符串。你不能这样做 - 就像试图将任意数据块作为图像文件加载一样。

如果要将某些任意二进制数据转换为字符串,请使用Convert.ToBase64String或将其转换为十六进制。

答案 1 :(得分:-2)

    // Verify the signature of an XML file and return the result.
    public static Boolean VerifySignRequest(String requestXML, string privateKeyIdentifier)
    {
        bool isValid = false;
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            X509Store store = new X509Store(StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);

            X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectKeyIdentifier, privateKeyIdentifier, false);
            X509Certificate2 cert = certs[0];

            // Create a new XML document.
            XmlDocument xmlDocument = new XmlDocument();

            // Format using white spaces.
            xmlDocument.PreserveWhitespace = true;

            // Load the passed XML file into the document. 
            xmlDocument.LoadXml(requestXML);

            // Create a new SignedXml object and pass it
            // the XML document class.
            SignedXml signedXml = new SignedXmlWithId(xmlDocument);

            // Find the "Signature" node and create a new
            // XmlNodeList object.
            XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature", "http://www.w3.org/2000/09/xmldsig#");

            // Load the signature node.
            signedXml.LoadXml((XmlElement)nodeList[0]);

            // Check the signature and return the result.
            isValid = signedXml.CheckSignature(cert,true);
        });
        return isValid;
    }

    /// <summary>
    /// SignedXmlWithId
    /// </summary>
    public class SignedXmlWithId : SignedXml
    {
        public SignedXmlWithId(XmlDocument xml)
            : base(xml)
        {
        }

        public SignedXmlWithId(XmlElement xmlElement)
            : base(xmlElement)
        {
        }

        public override XmlElement GetIdElement(XmlDocument doc, string id)
        {
            // check to see if it's a standard ID reference
            XmlElement idElem = base.GetIdElement(doc, id);

            if (idElem == null)
            {
                XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
                nsManager.AddNamespace("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

                idElem = doc.SelectSingleNode("//*[@wsu:Id=\"" + id + "\"]", nsManager) as XmlElement;
            }

            return idElem;
        }
    }