1

我正在做一个项目。客户有共同的认证系统。我必须从我的网络服务器生成一个随机数,使用身份验证系统的公钥加密,并使用我的网络服务器私钥唱歌并发布到身份验证系统(XML base64 编码)。身份验证系统使用他们的私钥解密,使用我的公钥生成随机数加密,然后发送回我的网络服务器作为响应。我的网络服务器使用我的私钥解密响应。现在下一步是使用基于 KDF2 算法的 nonce(客户端)和 nonce(服务器)构造一个 sessionKey。我正在使用 asp.net 4.0 .. 无法理解并在 asp.net 中找到关于“使用基于 KDF2 的 nonce(客户端)和 nonce(服务器)的 sessionKey”的任何帮助 。

    protected void ButtonLogin_Click()
    {
        string datatopost = CreatEncryptandSignXML();
        PostxmltoCommongateway(datatopost);
    }

    public string CreatEncryptandSignXML()
    {
        string ClientNonce = null;
        ClientNonce = Guid.NewGuid().ToString("N");
        Session["ClientNonce"] = ClientNonce;

        byte[] bytesToEncode = Encoding.UTF8.GetBytes(ClientNonce);

        string encodednonce = Convert.ToBase64String(bytesToEncode);

        string xml = "<?xml version=\"1.0\"?><root><LoginRequest>Nonce=" + encodednonce + "</LoginRequest></root>";
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.LoadXml(xml);
        doc.PreserveWhitespace = true;

        // Sing XML using private key ------------------------------------------------------------
        System.Security.Cryptography.X509Certificates.X509Certificate2 commonauthpublickey =
            new System.Security.Cryptography.X509Certificates.X509Certificate2
                (@"C:\commonauthserver\publickey\sample.cer");

        System.Xml.XmlElement elementToEncrypt = doc.GetElementsByTagName("LoginRequest")[0] as System.Xml.XmlElement;

        System.Security.Cryptography.Xml.EncryptedXml encXML = new System.Security.Cryptography.Xml.EncryptedXml();
        System.Security.Cryptography.Xml.EncryptedData data = encXML.Encrypt(elementToEncrypt, commonauthpublickey);

        System.Security.Cryptography.Xml.EncryptedXml.ReplaceElement(elementToEncrypt, data, false);


        // Sign XML using Private Key ---------------------------------------------------------------

        System.Security.Cryptography.X509Certificates.X509Certificate2 mywerbserverprivatekey =
            new System.Security.Cryptography.X509Certificates.X509Certificate2
                (@"C:\mywebserver\privatekey\mywebserver.pfx","samplepasword");

        System.Security.Cryptography.Xml.SignedXml sign = new System.Security.Cryptography.Xml.SignedXml(doc);

        System.Security.Cryptography.Xml.KeyInfo keyInfo = new System.Security.Cryptography.Xml.KeyInfo();
        sign.SigningKey = mywerbserverprivatekey.PrivateKey;
        System.Security.Cryptography.Xml.KeyInfoX509Data keyInfoData = new System.Security.Cryptography.Xml.KeyInfoX509Data();
        keyInfoData.AddIssuerSerial(mywerbserverprivatekey.Issuer, mywerbserverprivatekey.GetSerialNumberString());
        keyInfo.AddClause(keyInfoData);
        sign.KeyInfo = keyInfo;
        System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference();
        reference.Uri = "";
        System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform env = new
            System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform();
        reference.AddTransform(env);
        sign.AddReference(reference);
        sign.ComputeSignature();
        System.Xml.XmlElement signedElement = sign.GetXml();
        signedElement.Prefix = "ds";
        doc.DocumentElement.AppendChild(signedElement);
        return doc.InnerXml;
    }

    void PostxmltoCommongateway(string encData)
    {
        string URLAuth = "http://commonauth.com/getway/commomauth.do";

        byte[] bytesToEncode = Encoding.UTF8.GetBytes(encData);
        string encodedText = Convert.ToBase64String(bytesToEncode);
        string encodedXML = HttpUtility.UrlEncode(encodedText);

        string postString = string.Format("encryptedData={0}", encodedXML);

        const string contentType = "application/x-www-form-urlencoded";
        System.Net.ServicePointManager.Expect100Continue = false;

        CookieContainer cookies = new CookieContainer();
        HttpWebRequest webRequest = WebRequest.Create(URLAuth) as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = contentType;
        webRequest.CookieContainer = cookies;
        webRequest.ContentLength = postString.Length;
        webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
        webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
        requestWriter.Write(postString);
        requestWriter.Close();

        try
        {
            WebResponse response = webRequest.GetResponse();
            StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
            string responseData = responseReader.ReadToEnd();
            responseReader.Close();
            webRequest.GetResponse().Close();

            string servernonce = DecryptResponse(responseData);
            string clientnonce = Session["ClientNonce"].ToString();

            /// here i have to generate a sessionKey using nonce (client) and nonce(server) based on KDF2

            // CreateSessionkeybasedonKDF2(servernonce,clientnonce)                
        }

        catch (Exception ex)
        {
            LabelMessage.Text = ex.Message;
        }
    }

public string DecryptResponse(string response)
{
    System.Security.Cryptography.X509Certificates.X509Certificate2 mywerbserverprivatekey =
        new System.Security.Cryptography.X509Certificates.X509Certificate2
            (@"C:\mywebserver\privatekey\mywebserver.pfx", "samplepasword");

    RSACryptoServiceProvider.UseMachineKeyStore = false;
    RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)mywerbserverprivatekey.PrivateKey;
    byte[] decrypted = rsa.Decrypt(Convert.FromBase64String(response), false);
    return ASCIIEncoding.UTF8.GetString(decrypted);

}

感谢您提前回复。

4

1 回答 1

0

KDF2 是密钥种子(任何长度,应该表示具有足够熵的密钥),后缀为 BigEndian 表示法中的 4 字节计数器值,从值 1 开始。所以这将是KEY_SEED|00000001第一个密钥。在连接之后,使用像 SHA-1 这样的安全散列算法对值进行散列(要使用的散列是可配置的)。然后,您从生成的哈希值中获取所需的 X最左边的位或字节。

我只能告诉您如何实现 KDF2,因为它通常不包含在加密 API 中,恐怕您必须自己进行实现和测试(无论如何这“过于本地化”)。

于 2013-02-17T11:49:03.180 回答