0

次のコードがあります。私のステージング環境と実稼働前環境、および実稼働環境で動作しています。

本番環境でのみ突然動作しなくなった方法。それはまだプリプロダクションとプロダクションで動作します。

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;
}
4

2 に答える 2

1

これが問題です - または少なくとも問題です:

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

...そしておそらく前の行も同じです。

UTF-8 でエンコードされた文字列ではないEncoding.UTF8.GetString任意のバイナリ データで呼び出しています。これは、任意のデータの塊を画像ファイルとして読み込もうとするようなものです。

任意のバイナリ データを文字列に変換する場合は、それを使用するConvert.ToBase64Stringか、16 進数に変換します。

于 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 に答える