有谁知道一个简单的教程或如何在c#中使用充气城堡签名数据的示例代码。在Java中有大量的教程和示例。我在c#中找不到一个示例。有谁知道如何做到这一点?C#使用BouncyCastle与RSA签名数据
回答
好吧我找不到任何关于如何做到这一点的文档。但是我最终搞清楚了。 我在这里粘贴完整的代码,希望它可以帮助未来的人。
该类将使用sha1散列为提供的字符串计算RSA签名并对其进行验证。
using System;
using System.IO;
using System.Text;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
namespace API.Crypto
{
public class RsaSha1Signing
{
private RsaKeyParameters MakeKey(String modulusHexString, String exponentHexString, bool isPrivateKey)
{
var modulus = new Org.BouncyCastle.Math.BigInteger(modulusHexString, 16);
var exponent = new Org.BouncyCastle.Math.BigInteger(exponentHexString, 16);
return new RsaKeyParameters(isPrivateKey, modulus, exponent);
}
public String Sign(String data, String privateModulusHexString, String privateExponentHexString)
{
/* Make the key */
RsaKeyParameters key = MakeKey(privateModulusHexString, privateExponentHexString, true);
/* Init alg */
ISigner sig = SignerUtilities.GetSigner("SHA1withRSA");
/* Populate key */
sig.Init(true, key);
/* Get the bytes to be signed from the string */
var bytes = Encoding.UTF8.GetBytes(data);
/* Calc the signature */
sig.BlockUpdate(bytes, 0, bytes.Length);
byte[] signature = sig.GenerateSignature();
/* Base 64 encode the sig so its 8-bit clean */
var signedString = Convert.ToBase64String(signature);
return signedString;
}
public bool Verify(String data, String expectedSignature, String publicModulusHexString, String publicExponentHexString)
{
/* Make the key */
RsaKeyParameters key = MakeKey(publicModulusHexString, publicExponentHexString, false);
/* Init alg */
ISigner signer = SignerUtilities.GetSigner("SHA1withRSA");
/* Populate key */
signer.Init(false, key);
/* Get the signature into bytes */
var expectedSig = Convert.FromBase64String(expectedSignature);
/* Get the bytes to be signed from the string */
var msgBytes = Encoding.UTF8.GetBytes(data);
/* Calculate the signature and see if it matches */
signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
return signer.VerifySignature(expectedSig);
}
}
}
它帮助我很大,非常感谢你 – Recurrsion 2012-11-30 19:35:23
你能评论http://stackoverflow.com/questions/18344670/rsa-and-publickey-interop-with-dotnet – gpa 2013-08-20 22:04:06
好吧,最后。有人发布了明确的指示。非常感谢! – 2014-03-19 10:55:55
.NET不公开加密算法以供选择。还有很多RSA教程。
”.NET不公开加密算法以供选择。“那是什么意思?此链接不是RSA签名,它的加密。 – 2012-01-12 05:49:50
这意味着你不能指定将用于加密的算法。我没有说链接是用于签名的。这是为了与RSA合作。 SignData只是RSACryptoServiceProvider上的另一种方法。 – 2012-01-12 21:14:37
看看Bouncy Castle的网站。有来源和例子的档案。 http://www.bouncycastle.org/csharp/download/bccrypto-net-1.7-src-ext.zip
作为一个例子,有很多NUnit测试。 下面是使用RSA算法作为样本加密数据字节数组的方法代码,但在Bouncy Castle源代码和测试中可以找到更多样本。
public static byte[] Encrypt(byte[] data, AsymmetricKeyParameter key)
{
RsaEngine e = new RsaEngine();
e.Init(true, key);
int blockSize = e.GetInputBlockSize();
List<byte> output = new List<byte>();
for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
{
int chunkSize = Math.Min(blockSize, data.Length - (chunkPosition * blockSize));
output.AddRange(e.ProcessBlock(data, chunkPosition, chunkSize));
}
return output.ToArray();
}
- 1. C++ RSA签名
- 2. PKCS#7在bouncycastle签名数据问题
- 3. C#使用RSA签名和验证签名。编码问题
- 4. RSA与数字签名在Java中
- 5. RSA用PHP签名并用C验证#
- 6. AS3Crypto RSA签名
- 7. 如何在C#中使用BouncyCastle dll签名文件
- 8. RSA数字签名验证
- 9. 使用Crypt :: RSA在perl中进行数据签名
- 10. 使用pycrypto签名和验证数据(RSA)
- 11. 使用C#,BouncyCastle和导入的RSA密钥进行RSA签名和验证 - 使用Python示例和非工作的C#代码示例内
- 12. 签名X509数码证书W/BouncyCastle的 - 数字签名无效
- 13. .net RSA签名数据错误与PSS填充
- 14. RSA签名性能
- 15. 无法验证SHA1签名与RSA
- 16. 签名使用RSA-SHA256签名方法问题
- 17. 尝试从工作C#代码与Python生成RSA签名
- 18. C#PCL HMACSHAX与BouncyCastle的-PCL
- 19. 验证RSA签名iOS
- 20. RSA PKCS#1兼容签名
- 21. 了解JWT的RSA签名
- 22. 如何使用OpenSSL库生成RSA-SHA256数字签名?
- 23. 数字签名的令牌与RSA-SHA1的Base64加密
- 24. 使用BouncyCastle验证PgP加密和签名文件
- 25. BouncyCastle ECDSA签名验证失败使用prime256v1和SHA256withECDSA算法
- 26. BouncyCastle使用自定义密钥的J2ME RSA
- 27. 我如何RSA加密使用Java API BouncyCastle的Android上
- 28. 我如何将pem公钥转换为rsa公钥与c#中的bouncycastle?
- 29. 签名并创建一个asn1签名消息 - Bouncycastle
- 30. 如何使Botan RSA签名验证与PyCrypto相匹配
有没有你想要使用BouncyCastle的这方面的任何原因? .NET有它自己的RSA加密类。 – blowdart 2012-01-12 18:33:22
我想这是一个公平的问题。我过去曾在Java中使用过BouncyCastle,并且知道它在不需要兼容性的情况下对后端进行通信。它可能的.Net版本也是如此,但它似乎更不灵活,并有更多的选择与填充等。 – 2012-01-12 22:14:10
这是有道理的 - 尤其是如果你担心互操作:) – blowdart 2012-01-13 01:09:42