C# 需要在c中使用RSASSA-PKCS1-v1_5签名对字符串进行签名#
我有一个带消息和私钥的字符串。我需要编写一些C代码,可以用RSASSA-PKCS1-v1#u 5签名对这个字符串进行签名C# 需要在c中使用RSASSA-PKCS1-v1_5签名对字符串进行签名#,c#,signing,C#,Signing,我有一个带消息和私钥的字符串。我需要编写一些C代码,可以用RSASSA-PKCS1-v1#u 5签名对这个字符串进行签名 关于我应该从哪里着手完成这项工作,你有什么建议吗?.NET或可用库中是否有现成的API来执行此操作?这是一个如何执行此操作的示例,您可以通过对代码进行必要的更改来实现它(示例来自: 谢谢,但是我的私钥去哪儿了pem文件中的私钥这段代码用Ruby做这项工作,但我需要用c#。key=OpenSSL::PKey::RSA.new(pemstring)key.sign(OpenSSL
关于我应该从哪里着手完成这项工作,你有什么建议吗?.NET或可用库中是否有现成的API来执行此操作?这是一个如何执行此操作的示例,您可以通过对代码进行必要的更改来实现它(示例来自:
谢谢,但是我的私钥去哪儿了pem文件中的私钥这段代码用Ruby做这项工作,但我需要用c#。key=OpenSSL::PKey::RSA.new(pemstring)key.sign(OpenSSL::Digest.new('sha256'),body.)。unpack('H*)。首先谢谢,我今天晚些时候会尝试,但你不是在加载PubKeyFiles时轻描淡写问题。
using System;
using System.Security.Cryptography;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
class RSASample
{
const string PubKeyFile = @"c:\encrypt\fileWithKey.pem";
const string keyName = "Key01";
CspParameters cspp = new CspParameters();
RSAParameters RSAKeyInfo = new RSAParameters();
Org.BouncyCastle.Utilities.IO.Pem.PemObject po = null;
using (StreamReader sr = new StreamReader(PubKeyFile))
{
Org.BouncyCastle.OpenSsl.PemReader pr = new Org.BouncyCastle.OpenSsl.PemReader(reader);
po = pr.ReadPemObject();
}
RSAKeyInfo.Modulus = po.Content;
cspp.KeyContainerName = keyName;
try
{
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp))
{
rsa.PersistKeyInCsp = true;
rsa.ImportParameters(RSAKeyInfo);
//The hash to sign.
byte[] hash;
using (SHA256 sha256 = SHA256.Create())
{
byte[] data = new byte[] { 59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135 };
hash = sha256.ComputeHash(data);
}
//Create an RSASignatureFormatter object and pass it the
//RSACryptoServiceProvider to transfer the key information.
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsa);
//Set the hash algorithm to SHA256.
RSAFormatter.SetHashAlgorithm("SHA256");
//Create a signature for HashValue and return it.
byte[] SignedHash = RSAFormatter.CreateSignature(hash);
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
}