使用Google服务帐户私钥签名数据失败

我无法使用从Google Developer Console下载的Service Application私钥对数据进行签名。 我收到以下错误:

OAuthTests.TestCrypto.testSha256SignWithGoogleKey: System.Security.Cryptography.CryptographicException : Invalid algorithm specified. at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) at System.Security.Cryptography.Utils._SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash, Int32 dwFlags) at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, String str) at OAuthTests.TestCrypto.testSha256SignWithGoogleKey() in e:\Development\MiscellaneousProjects\RSSNewsFeeder\Oauth\OAuthTests.cs:line 43 

是的,我之前已经问过这个问题,但没有得到太多的帮助,因为Stack的论坛模型不容易添加到现有的线程上,似乎我最好的改写问题就是做到这一点; 改写并提出一个新问题。

我写了三个unit testing(下面的代码)。 第一个unit testing显示我可以使用带有SHA256的RSACryptoServiceProvider对数据进行签名和validation,但此测试不使用我的Google证书的私钥。
当我使用Google的私钥证书和测试(下面的第二个测试)时,代码错误(上面的错误消息)。
第3次测试演示了使用Google的私钥并使用SHA1进行测试,这有效! 但根据规格无效。

以下代码是否有问题,或者证书有问题,或者可能是我的操作系统或其他环境问题? 我正在Windows 8.1机器上使用Windows C#3.5进行开发。

**这个工作**
不使用Google证书

  var cert = new X509Certificate2(@"E:\Development\MiscellaneousProjects\RSSNewsFeeder\Samples\9d16ba9bd04468b4cd0dd241e34b980643fd5b21-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable); byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey) { byte[] signature = rsa.SignData(data, "SHA256"); if (rsa.VerifyData(data, "SHA256", signature)) { Console.WriteLine("RSA-SHA256 signature verified"); } else { Console.WriteLine("RSA-SHA256 signature failed to verify"); } } 

**这个失败**
使用Google证书和SHA256
失败时:byte [] signature = rsa.SignData(data,“SHA256”);

  [Test] public void testSha256SignWithGoogleKey() { var cert = new X509Certificate2(@"....41e34b980643fd5b21-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable); byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key) { byte[] signature = rsa.SignData(data, "SHA256"); if (rsa.VerifyData(data, "SHA256", signature)) { Console.WriteLine("RSA-SHA256 signature verified"); } else { Console.WriteLine("RSA-SHA256 signature failed to verify"); } } } 

**这个工作**
使用Google证书,但使用SHA1

 [Test] public void testShaSignWithGoogleKey() { var cert = new X509Certificate2(@"....dd241e34b980643fd5b21-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable); byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey) { byte[] signature = rsa.SignData(data, "SHA1"); if (rsa.VerifyData(data, "SHA1", signature)) { Console.WriteLine("RSA-SHA1 signature verified"); } else { Console.WriteLine("RSA-SHA1 signature failed to verify"); } } } 

首先,您的示例#2中存在错误:您正在尝试使用公钥进行签名。 你应该得到错误:“对象只包含密钥对的公共一半。还必须提供私钥。”

但我想这只是一个复制/粘贴错误,你已经尝试过使用私钥。

从Google的证书PrivateKey获取的RSACryptoServiceProvider使用“Microsoft Base Cryptographic Provider v1.0”,而新创建的RSACryptoServiceProvider对象使用“Microsoft Enhanced RSA和AES Cryptographic Provider”。

解决此问题的技巧是将裸数学从cert的RSACSP导出到新的RSACSP对象:

 [Test] public void testSha256SignWithGoogleKey() { var cert = new X509Certificate2(@"....41e34b980643fd5b21-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable); byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey) { using (RSACryptoServiceProvider myRsa = new RSACryptoServiceProvider()) { myRsa.ImportParameters(rsa.ExportParameters(true)); byte[] signature = myRsa.SignData(data, "SHA256"); if (myRsa.VerifyData(data, "SHA256", signature)) { Console.WriteLine("RSA-SHA256 signature verified"); } else { Console.WriteLine("RSA-SHA256 signature failed to verify"); } } } }