如何在.NET中使用SHA1WITHRSA,X509Certificate2和BouncyCastle对字符串进行签名?

How to sign a string using SHA1WITHRSA, X509Certificate2 and BouncyCastle in .NET?

提问人:Jay Weber 提问时间:9/11/2023 更新时间:9/11/2023 访问量:54

问:

我目前正在处理一个项目,我必须对 XML 文档进行签名才能将其发送到 Web 服务。

我尝试了几个可以在这里和网络上找到的代码片段,但还没有实现我的目标。

这是我当前使用的函数,但它会引发异常,因为模数为负。


Function Encrypt_XML(sUnsigned_Snippet As String, oCertificate As X509Certificate2) As String
    Dim Parameters As RSAParameters = CType(oCertificate.PrivateKey, RSACryptoServiceProvider).ExportParameters(True)

    Dim modulus As Org.BouncyCastle.Math.BigInteger = New Org.BouncyCastle.Math.BigInteger(Parameters.Modulus)
    Dim exponent As Org.BouncyCastle.Math.BigInteger = New Org.BouncyCastle.Math.BigInteger(Parameters.Exponent)

    Dim key As RsaKeyParameters = New RsaKeyParameters(True, modulus, exponent) 'exception is thrown here


    Dim sig As ISigner = SignerUtilities.GetSigner("SHA1WITHRSA")
    sig.Init(True, key)


    sig.BlockUpdate(inputData, 0, inputData.Length)
    Dim signature As Byte() = sig.GenerateSignature

    Dim signedString = Convert.ToBase64String(signature)

    Debug.Print("BouncyCastle" & vbCrLf & signedString)
    Encrypt_XML = signedString

End Function

异常:不是有效的 RSA 模数 参数名称:模数

我在这里做错了什么?

请注意:我对加密不是很有经验,甚至发现理解文档都非常具有挑战性。

vb.net RSA Bouncycastle SHA1 x509certificate2

评论

2赞 Topaco 9/11/2023
关于负号:使用带有 .Org.BouncyCastle.Math.BigInteger(sign As Integer, bytes As Byte())sign = 1

答: 暂无答案