3

我正在尝试使用 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;
}
4

1 回答 1

2

以下代码应该可以为您解决问题。它从给定的文件路径加载证书,然后使用公钥根据给定的签名验证数据。如果有效则返回真。

            byte[] signature = Convert.FromBase64String(Signature);

            byte[] data = Encoding.UTF8.GetBytes(Data);

            var x509 = new X509Certificate2(Path.Combine(@"C:\test", @"test\test.crt"));

            var rsa = x509.PublicKey.Key as RSACryptoServiceProvider;
            if (rsa == null)
            {
                LogMessage("Authorize", "Invalid", Level.Alert);
                return false;
            }

            string sha1Oid = CryptoConfig.MapNameToOID("SHA1");

            //use the certificate to verify data against the signature
            bool sha1Valid = rsa.VerifyData(data, sha1Oid, signature);

            return sha1Valid;
于 2012-10-05T04:42:10.640 回答