<small id='2VC0Y'></small><noframes id='2VC0Y'>

<legend id='2VC0Y'><style id='2VC0Y'><dir id='2VC0Y'><q id='2VC0Y'></q></dir></style></legend>

    <tfoot id='2VC0Y'></tfoot>
    • <bdo id='2VC0Y'></bdo><ul id='2VC0Y'></ul>

  1. <i id='2VC0Y'><tr id='2VC0Y'><dt id='2VC0Y'><q id='2VC0Y'><span id='2VC0Y'><b id='2VC0Y'><form id='2VC0Y'><ins id='2VC0Y'></ins><ul id='2VC0Y'></ul><sub id='2VC0Y'></sub></form><legend id='2VC0Y'></legend><bdo id='2VC0Y'><pre id='2VC0Y'><center id='2VC0Y'></center></pre></bdo></b><th id='2VC0Y'></th></span></q></dt></tr></i><div id='2VC0Y'><tfoot id='2VC0Y'></tfoot><dl id='2VC0Y'><fieldset id='2VC0Y'></fieldset></dl></div>
    1. 在 C# 中使用私钥对数据进行签名

      时间:2023-06-02

        • <bdo id='c8JAm'></bdo><ul id='c8JAm'></ul>

          <i id='c8JAm'><tr id='c8JAm'><dt id='c8JAm'><q id='c8JAm'><span id='c8JAm'><b id='c8JAm'><form id='c8JAm'><ins id='c8JAm'></ins><ul id='c8JAm'></ul><sub id='c8JAm'></sub></form><legend id='c8JAm'></legend><bdo id='c8JAm'><pre id='c8JAm'><center id='c8JAm'></center></pre></bdo></b><th id='c8JAm'></th></span></q></dt></tr></i><div id='c8JAm'><tfoot id='c8JAm'></tfoot><dl id='c8JAm'><fieldset id='c8JAm'></fieldset></dl></div>
          <tfoot id='c8JAm'></tfoot>
            • <legend id='c8JAm'><style id='c8JAm'><dir id='c8JAm'><q id='c8JAm'></q></dir></style></legend>
                <tbody id='c8JAm'></tbody>

              <small id='c8JAm'></small><noframes id='c8JAm'>

              • 本文介绍了在 C# 中使用私钥对数据进行签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我需要使用算法 SHA1RSA 用一个私钥对一些数据进行签名,Rsa 密钥长度为 2048,基本编码为 64.我的代码是这样的

                I need to sign some data with one private key using Algorithm SHA1RSA ,Rsa Key length 2048 with 64 base encoding.My code is like this

                string sPayload = "";
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("URI");
                httpWebRequest.ContentType = "application/json; charset=utf-8";
                httpWebRequest.Method = WebRequestMethods.Http.Post;
                
                using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    sPayload = "{"id":"14123213213"," +
                                ""uid":"teller"," +
                                ""pwd":"abc123"," +
                                ""apiKey":"2343243"," +
                                ""agentRefNo":"234324324"}";
                
                    httpWebRequest.Headers.Add("SIGNATURE", Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(sPayload))));
                    
                    streamWriter.Write(sPayload);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                
                System.Net.ServicePointManager.Expect100Continue = false;
                
                HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                
                using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    string result = streamReader.ReadToEnd();
                }
                

                在标头名称签名中,我需要使用私钥传递签名数据(sPayload).但是使用上面的代码会出现无效签名"错误.来自第三方,我不确定加密部分是否正确.

                In the Header name Signature i need to pass the signed data(sPayload) using the private key.But using above code an error is getting as "invalid signature" from third party and i'am not sure whether the Encryption part is correct or not.

                httpWebRequest.Headers.Add("SIGNATURE", Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(sPayload))));
                

                第三方提供了一个证书(cert,sha1)和密钥.我应该参考代码吗?

                Third party had provide one certificate(cert,sha1) and key.should i refer that to the code?

                推荐答案

                您计算了 sPayload 的 SHA-1 哈希,而不是 RSA-SHA1 签名.

                You have computed the SHA-1 hash of sPayload, not the RSA-SHA1 signature.

                如果您有 X509Certificate2:

                If you have an X509Certificate2:

                using (RSA rsa = cert.GetRSAPrivateKey())
                {
                    return rsa.SignData(sPayload, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
                }
                

                如果您已经拥有原始 RSA 密钥,那么请不要使用 using 语句.

                If you already have a raw RSA key then just leave off the using statement.

                如果你必须计算 sPayload 的哈希值,你可以这样做

                If you have to compute the hash of sPayload for some other reason you can do it like

                byte[] hash;
                byte[] signature;
                
                using (HashAlgorithm hasher = SHA1.Create())
                using (RSA rsa = cert.GetRSAPrivateKey())
                {
                    hash = hasher.ComputeHash(sPayload);
                    signature = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
                }
                

                SignHash 仍然需要 HashAlgorithmName 值,因为算法标识符嵌入在签名中.

                SignHash still requires the HashAlgorithmName value because the algorithm identifier is embedded within the signature.

                这篇关于在 C# 中使用私钥对数据进行签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:根据私有指数 (d)、公共指数 (e) 和模数 (n) 计算素 下一篇:给定一个私钥,是否可以导出它的公钥?

                相关文章

                最新文章

              • <small id='iMWSE'></small><noframes id='iMWSE'>

                    <bdo id='iMWSE'></bdo><ul id='iMWSE'></ul>
                  1. <legend id='iMWSE'><style id='iMWSE'><dir id='iMWSE'><q id='iMWSE'></q></dir></style></legend>

                    <tfoot id='iMWSE'></tfoot>

                  2. <i id='iMWSE'><tr id='iMWSE'><dt id='iMWSE'><q id='iMWSE'><span id='iMWSE'><b id='iMWSE'><form id='iMWSE'><ins id='iMWSE'></ins><ul id='iMWSE'></ul><sub id='iMWSE'></sub></form><legend id='iMWSE'></legend><bdo id='iMWSE'><pre id='iMWSE'><center id='iMWSE'></center></pre></bdo></b><th id='iMWSE'></th></span></q></dt></tr></i><div id='iMWSE'><tfoot id='iMWSE'></tfoot><dl id='iMWSE'><fieldset id='iMWSE'></fieldset></dl></div>