C# 通过OpenSSL和Microsoft加密库进行签名有什么区别?
我使用RSA和SHA256编写了两种签名方法,第一种是使用OpenSSL库,第二种是使用Microsoft加密库 OpenSSL实现:C# 通过OpenSSL和Microsoft加密库进行签名有什么区别?,c#,cryptography,openssl,x509certificate,signing,C#,Cryptography,Openssl,X509certificate,Signing,我使用RSA和SHA256编写了两种签名方法,第一种是使用OpenSSL库,第二种是使用Microsoft加密库 OpenSSL实现: private string PasswordHandler(bool verify, object userdata) { return userdata.ToString(); } private string Sign(string signParams) { var private
private string PasswordHandler(bool verify, object userdata)
{
return userdata.ToString();
}
private string Sign(string signParams)
{
var privateCertPath = HttpContext.Current.Server.MapPath(@"~\certificate.pem");
string privateKey;
using (StreamReader sr = new StreamReader(privateCertPath))
{
privateKey = sr.ReadToEnd();
}
OpenSSL.Crypto.RSA rsa = OpenSSL.Crypto.RSA.FromPrivateKey(new BIO(privateKey), PasswordHandler, _password);
//hash method
MessageDigest md = MessageDigest.SHA1;
BIO b = new BIO(signParams);
CryptoKey ck = new CryptoKey(rsa);
byte[] res1 = MessageDigestContext.Sign(md, b, ck);
return Uri.EscapeDataString(System.Convert.ToBase64String(res1));
}
private string Sign(string data)
{
var privateCertPath = HttpContext.Current.Server.MapPath(@"~\certificate.pfx");
X509Certificate2 privateCert = new X509Certificate2(privateCertPath, _password, X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider privateKey = (RSACryptoServiceProvider)privateCert.PrivateKey;
RSACryptoServiceProvider privateKey1 = new RSACryptoServiceProvider();
privateKey1.ImportParameters(privateKey.ExportParameters(true));
// Get the bytes to be signed from the string
var bytes = System.Text.Encoding.UTF8.GetBytes(data);
//const string sha256Oid = "2.16.840.1.101.3.4.2.1";
//HashAlgorithm algorithm = new SHA256CryptoServiceProvider();
//byte[] hashBytes = algorithm.ComputeHash(bytes);
//byte[] signature = privateKey1.SignHash(hashBytes, sha256Oid);
byte[] signature = privateKey1.SignData(bytes, "SHA256");
// Base 64 encode the sig so its 8-bit clean
return Convert.ToBase64String(signature);
}
加密实现:
private string PasswordHandler(bool verify, object userdata)
{
return userdata.ToString();
}
private string Sign(string signParams)
{
var privateCertPath = HttpContext.Current.Server.MapPath(@"~\certificate.pem");
string privateKey;
using (StreamReader sr = new StreamReader(privateCertPath))
{
privateKey = sr.ReadToEnd();
}
OpenSSL.Crypto.RSA rsa = OpenSSL.Crypto.RSA.FromPrivateKey(new BIO(privateKey), PasswordHandler, _password);
//hash method
MessageDigest md = MessageDigest.SHA1;
BIO b = new BIO(signParams);
CryptoKey ck = new CryptoKey(rsa);
byte[] res1 = MessageDigestContext.Sign(md, b, ck);
return Uri.EscapeDataString(System.Convert.ToBase64String(res1));
}
private string Sign(string data)
{
var privateCertPath = HttpContext.Current.Server.MapPath(@"~\certificate.pfx");
X509Certificate2 privateCert = new X509Certificate2(privateCertPath, _password, X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider privateKey = (RSACryptoServiceProvider)privateCert.PrivateKey;
RSACryptoServiceProvider privateKey1 = new RSACryptoServiceProvider();
privateKey1.ImportParameters(privateKey.ExportParameters(true));
// Get the bytes to be signed from the string
var bytes = System.Text.Encoding.UTF8.GetBytes(data);
//const string sha256Oid = "2.16.840.1.101.3.4.2.1";
//HashAlgorithm algorithm = new SHA256CryptoServiceProvider();
//byte[] hashBytes = algorithm.ComputeHash(bytes);
//byte[] signature = privateKey1.SignHash(hashBytes, sha256Oid);
byte[] signature = privateKey1.SignData(bytes, "SHA256");
// Base 64 encode the sig so its 8-bit clean
return Convert.ToBase64String(signature);
}
使用OpenSSL签名可以工作,生成有效的数字签名,但使用加密库签名会生成无效签名,所以我的问题是我实现的错误是什么
我尝试使用不同的编码,但没有帮助。正确生成证书
告诉.pem证书的基本信息也可能有用:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC
我注意到OpenSSL示例使用的是SHA1而不是SHA256。当您说使用Microsoft库生成了无效的签名时,您是如何验证该签名的?什么证书?我刚刚看到一个私钥?@softwariness Payment gateway验证了它。@Maarten我刚刚发布了证书的开头,因为我认为没有更多有用的信息。@softwariness我了解到OpenSSL使用了某种特殊类型的SHA1,这在Microsoft library的SHA256中是受支持的。我还使用公钥验证来自支付网关的响应,我使用的SHA256工作正常。