我正在尝试使用 C#验证OpenSSL
签名。它使用正确的公钥证书返回 false。(created using openssl_sign with SHA1 in PHP)
RSACryptoProvider.VerifyData
关于如何成功做到这一点的任何想法?
编辑:
我尝试使用以下代码使用 BouncyCastle 验证 OpenSSL SHA1 签名,但验证失败。签名的计算方式是否不同?如何使用 OpenSSL 创建可由 .NET 验证的签名?
byte[] signatureBytes = UTF8Encoding.Default.GetBytes(signature);
byte[] dataBytes = UTF8Encoding.Default.GetBytes(data);
StreamReader sr = new StreamReader(Path.Combine(@"C:\test", @"test\test.crt"));
PemReader pr = new PemReader(sr);
Org.BouncyCastle.X509.X509Certificate cert = (Org.BouncyCastle.X509.X509Certificate)pr.ReadObject();
ISigner sig = SignerUtilities.GetSigner("SHA1WithRSAEncryption");
sig.Init(false, cert.GetPublicKey());
sig.BlockUpdate(dataBytes, 0, dataBytes.Length);
if (sig.VerifySignature(signatureBytes)) {
Console.WriteLine("all good!");
}
PHP代码:
function signTokenWithPrivateKey($message, $keyLocation) {
try {
if (file_exists($keyLocation)) {
$privateKey= openssl_get_privatekey(file_get_contents($keyLocation));
$signature = '';
if (!openssl_sign($message, $signature, $privateKey)) {
die('Failed to encrypt');
}
openssl_free_key($privateKey);
}
}
catch (Exception $ex) {
}
return $signature;
}