在不同操作系统(Win7 与 Win2008R2)上的 .NET 中生成时,加密的有效负载签名不同

发布于 2024-11-17 02:45:03 字数 1230 浏览 0 评论 0原文

我工作的一位开发人员在使用证书的私钥在我们的生产服务器上签署有效负载时遇到了一些困难。该代码可以在他的开发盒和生产服务器上运行,但是这两个不同的位置最终会为相同的有效负载提供不同的签名。我们已确认这两个位置的证书相同,但由于某种原因,RSACryptoServiceProvider.SignData 方法似乎会返回不同的值,具体取决于它是在 Windows 7 还是 Server 2008 R2 上运行。

这是我们正在使用的代码 - 您可以看到我们已经用配置文件中的 Base64 字符串替换了有效负载,因此甚至不可能是有效负载的差异导致了不同的签名。

byte[] encryptedSignature = new byte[1];
CspParameters cp = new CspParameters(24, "Microsoft Enhanced RSA and AES Cryptographic Provider", "{4FC30434-29E5-482D-B817-72102A046137}");
cp.Flags = CspProviderFlags.UseMachineKeyStore;
cp.KeyNumber = (int)KeyNumber.Exchange;

bool signatureVerified = false;

using (RSACryptoServiceProvider rsaCrypto = new RSACryptoServiceProvider(2048, cp))
{
    encryptedSignature = rsaCrypto.SignData(Convert.FromBase64String(Properties.Settings.Default.EncryptedSessionData), "SHA256");
    // Signature verifies properly on both servers
    signatureVerified = rsaCrypto.VerifyData(Convert.FromBase64String(Properties.Settings.Default.EncryptedSessionData), "SHA256", encryptedSignature);

}

服务器(一个 x64 机器)是否可能以与我们的 x86 开发工作站不同的方式处理签名?这似乎不太可能,但我们很困惑为什么生产服务器会产生不同的签名。另外,不确定这是否重要,但此代码来自 ASP.NET 页面,我们在其中创建有效负载,然后将访问者连同加密的有效负载一起转发到供应商的页面。

希望有人能在这里提供一些启示或看到类似的东西。

A developer where I work is running into some difficulty using a certificate's private key to sign a payload on our production server. The code works on both his development box and the production server, but the two different locations end up with a different signature for the same payload. We've confirmed that it's the same certificate in both locations, but for some reason, the RSACryptoServiceProvider.SignData method seems to return a different value depending on whether it's being run on Windows 7 or Server 2008 R2.

Here's the code we're using - you can see that we've replaced the payload with a Base64 string from our config file, so it's not even possible that it could be a difference in the payload that's causing the different signatures.

byte[] encryptedSignature = new byte[1];
CspParameters cp = new CspParameters(24, "Microsoft Enhanced RSA and AES Cryptographic Provider", "{4FC30434-29E5-482D-B817-72102A046137}");
cp.Flags = CspProviderFlags.UseMachineKeyStore;
cp.KeyNumber = (int)KeyNumber.Exchange;

bool signatureVerified = false;

using (RSACryptoServiceProvider rsaCrypto = new RSACryptoServiceProvider(2048, cp))
{
    encryptedSignature = rsaCrypto.SignData(Convert.FromBase64String(Properties.Settings.Default.EncryptedSessionData), "SHA256");
    // Signature verifies properly on both servers
    signatureVerified = rsaCrypto.VerifyData(Convert.FromBase64String(Properties.Settings.Default.EncryptedSessionData), "SHA256", encryptedSignature);

}

Is it possible that the server, an x64 box, could be handling the signature differently than our x86 development workstations? It doesn't seem likely, but we're stumped as to why the production server would produce a different signature. Also, not sure if it matters, but this code comes from an ASP.NET page where we create the payload and then forward the visitor to a vendor's page along with the encrypted payload.

Hopefully somebody has some light to shed here or has seen something similar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╭ゆ眷念 2024-11-24 02:45:03

更改

CspParameters(24, "Microsoft Enhanced RSA and AES Cryptographic Provider", "{4FC30434-29E5-482D-B817-72102A046137}");

CspParameters cp = new CspParameters(24, "Microsoft Enhanced RSA and AES Cryptographic Provider", ((RSACryptoServiceProvider)cert.PrivateKey).CspKeyContainerInfo.KeyContainerName);

,将从 PrivateKey 获取 ContainerName。
不错的头像图片;)

Change

CspParameters(24, "Microsoft Enhanced RSA and AES Cryptographic Provider", "{4FC30434-29E5-482D-B817-72102A046137}");

to

CspParameters cp = new CspParameters(24, "Microsoft Enhanced RSA and AES Cryptographic Provider", ((RSACryptoServiceProvider)cert.PrivateKey).CspKeyContainerInfo.KeyContainerName);

that will get the ContainerName from the PrivateKey.
Nice avatar pic ;)

揽清风入怀 2024-11-24 02:45:03

PKCS1 V 1.5 SIGNATURE 中未使用随机填充(PKCS1 RSA 文档中所述的 EMSA-PKCS1-v1_5)。它仅用于加密(即使用公钥加密)

Random Padding is NOT used in PKCS1 V 1.5 SIGNATURE (EMSA-PKCS1-v1_5 as stated in PKCS1 RSA document). It is used only in ENCRYPTION (ie Encryption using Public Keys)

余生共白头 2024-11-24 02:45:03

RSA 采用随机填充。您不能保证同一纯文本的两个签名相同。

RSA employs random padding. You can't guarantee two signatures of the same plain text be the same.

剩余の解释 2024-11-24 02:45:03

尝试根据公钥验证不同的签名...只要两个签名都有效,就没有问题

try to verify the different signatures against the public key ... as long as both signatures are valid, there is no problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文