使用cer文件并签署字符串文件或xml文件。请帮助我们

时间:2013-05-18 09:23:45

标签: string x509certificate sign

我在使用x509certificates类时遇到了一些问题。

这些代码正在使用usb棒。但不工作“.cer”文件。

公共函数signString(ByVal msg As String,ByVal imzasahibi As String)As String

    Dim unicode As Encoding = Encoding.UTF8
    Dim msgBytes As Byte() = unicode.GetBytes(msg)

    Dim signerCert As X509Certificate2 = GetSignerCert(imzasahibi)
    Dim encodedSignedCms As Byte() = SignMsg(msgBytes, signerCert)

    Dim result As String = Convert.ToBase64String(encodedSignedCms)

    If Not VerifyMsg(msgBytes, encodedSignedCms) Then
        Throw New Exception("Any problems")
    End If

    Return result
End Function

Public Function MesajImzala(ByVal msg As String,ByVal imzasahibi As String)As String

Dim sertifikamiz As String = HttpContext.Current.Server.MapPath(“certificateFiles / testFile.cer”)

Dim signerCert As X509Certificate2 = X509Certificate2.CreateFromCertFile(sertifikamiz)

    Dim unicode As Encoding = Encoding.UTF8
    Dim msgBytes As Byte() = unicode.GetBytes(msg)

    Dim encodedSignedCms As Byte() = SignMsg(msgBytes, signerCert)

    Dim result As String = Convert.ToBase64String(encodedSignedCms)

    If Not VerifyMsg(msgBytes, encodedSignedCms) Then
        Throw New Exception("Any problems")
    End If

    Return result
End Function

如果你想我发送文件。谢谢大家...

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidCastException:'System.Security.Cryptography.X509Certificates.X509Certificate'türündekinesne'System.Security.Cryptography.X509Certificates.X509Certificate2'türüneatılamadı。

1 个答案:

答案 0 :(得分:0)

.CER文件通常只包含没有私钥的证书。要签署数据,您必须拥有私钥。这就是您无法仅使用.cer文件对数据进行签名的原因。