在 C# 中使用私钥对数据进行签名

2 c# cryptography rsa httprequest digital-signature

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

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();
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

bar*_*njs 6

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

如果您有 X509Certificate2:

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

如果您已有原始 RSA 密钥,则只需省略 using 语句即可。

sPayload如果您由于其他原因必须计算哈希值,您可以这样做

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);
}
Run Code Online (Sandbox Code Playgroud)

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