0

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

它如何突然停止仅在生产环境中工作。它仍然适用于预生产和生产。

它抛出“哈希值不匹配”错误,这意味着存储哈希!= 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;
}
4

2 回答 2

1

这就是问题——或者至少是一个问题:

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

...而且很可能与前几行相同。

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

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

于 2014-07-10T06:49:10.277 回答
-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;
        }
    }
于 2016-06-29T01:24:51.343 回答