简体   繁体   English

C#与公钥签名匹配

[英]C# Signature Matching with Public Key

I'm working with a third party attempting to establish digital signature matching in both directions. 我正在与第三方合作,尝试在两个方向上建立数字签名匹配。 On my side we are using C# while the third party is using Java. 在我这边,我们正在使用C#而第三方正在使用Java。 So far I am able to generate a key pairing within OpenSSL and use the private key to generate a signature which they are able to match in their Java environment (C# code below). 到目前为止,我能够在OpenSSL中生成密钥配对,并使用私钥生成他们能够在其Java环境中匹配的签名(下面的C#代码)。 Going the opposite direction, how do I use the generated public key to create a matching signature? 反方向,如何使用生成的公钥创建匹配的签名?

I've attempted using the same code below simply changing the CngKeyBlobFormat when importing the public key but all other types produce an error. 我尝试使用下面相同的代码,只需在导入公钥时更改CngKeyBlobFormat,但所有其他类型都会产生错误。

On both sides the following OpenSSL commands were used to generate the public and private keys: 在两端,以下OpenSSL命令用于生成公钥和私钥:

openssl genrsa -des3 -out my_rsa_key_pair 2048

openssl pkcs8 -topk8 -inform PEM -in my_rsa_key_pair -outform PEM -out private_key.pem -nocrypt

openssl rsa -in my_rsa_key_pair -outform PEM -pubout -out public_key.pem

This is the C# code used to generate a signature from the private key: 这是用于从私钥生成签名的C#代码:

byte[] keyBytes = Convert.FromBase64String(privateKey);
byte[] valueBytes = Encoding.UTF8.GetBytes(stringToSign);
byte[] signedBytes;

using (CngKey key = CngKey.Import(keyBytes, CngKeyBlobFormat.Pkcs8PrivateBlob))
using (RSA rsa = new RSACng(key))
{
    signedBytes = rsa.SignData(valueBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
    return Convert.ToBase64String(signedBytes);
}

Using the public key I need to generate a signature within C# that matches what was generated with the private key. 使用公钥我需要在C#中生成一个与私钥生成的签名相匹配的签名。 What is the proper way to do this? 这样做的正确方法是什么?

There are a lot of posts surrounding signature generation / verification and after hitting on the right key words I found the answer in the following post. 有很多帖子围绕签名生成/验证,在点击正确的关键词后,我在下面的帖子中找到答案。 Given the above code and method of generating public/private key pairings the answer in this post will successfully validate the signature generated with a private key using only the public key. 给定上述生成公钥/私钥配对的代码和方法,本文中的答案将成功验证仅使用公钥使用私钥生成的签名。 Leaving this open for anyone searching for both sides of the solution: Verify signature generated with RSA 2048-bit key, SHA256 algorithm and PKCSv1.5 padding 对于搜索解决方案两端的任何人都保持开放状态: 验证使用RSA 2048位密钥,SHA256算法和PKCSv1.5填充生成的签名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM