Archived Forum Post

Index of archived forum posts

Question:

Sign and encrypt txt file

Apr 14 '13 at 22:27

Hi!

I've been stucked a while with this problem and i hope someone can help me. I need to sign and crypt a txt file for citibank but i can´t get it done, the requirements are:

The security requirements of CitiDirect Online Banking for the tool being utilized by the client for signing and encryption purposes are:

• Support for PKCS#7 Message standard (encryption and signing)

• Signing certificate must be included in the encoded message as per PKCS#7 standard.

• Signing format must be opaque (no clear-signing)

• Public Key trust chain must be included with encrypted/signed file

• Must utilize 3DES for symmetric encryption algorithm

• Must support SHA-1 RSA Public Key algorithms with key lengths of 1024 or larger

• You must upload your Public Key to CitiDirect prior to creating a File Delivery schedule.

• The tool must support the wrapping of a PKCS#7 structures in an ASN.1 Content Info structure

I´ve tried many ways to get this done, but i still don´t find a way out.

My code:

Private Function encryptandsign(ByVal strfile As String) As Boolean
        Dim sr As New StreamReader(strfile, System.Text.Encoding.UTF8)
        Dim strtocrypt As String = ""
        strtocrypt = sr.ReadToEnd()
        sr.Close()
        Dim mime As New Chilkat.Mime()
        Dim success As Boolean
        success = mime.UnlockComponent("30-day trial")
        If (success <> True) Then
            TextBox1.Text = TextBox1.Text & mime.LastErrorText & vbCrLf
            Exit Function
        End If

    '  Find our digital certificate from the Current User certificate store.
    Dim ccs As New Chilkat.CreateCS()
    Dim certStore As Chilkat.CertStore
    certStore = ccs.OpenCurrentUserStore()
    Dim cert As Chilkat.Cert
    cert = certStore.FindCertBySubjectCN("Juan Moreyra")

    If (cert Is Nothing) Then
        MsgBox(cert.LastErrorText)
        Exit Function
    End If

    '  Our MIME object is currently empty.  Add some header fields
    '  and a body:
    mime.AddHeaderField("Message-ID", "<CHILKAT-MID-83cf2fbf-10cb-4322-ad79-4c1097fd56f2@Matt>")
    mime.AddHeaderField("MIME-VERSION", "1.0")
    'mime.ContentType = "application/pkcs7-mime"
    mime.ContentType = "application/pkcs7-mime; smime-type=enveloped-data;"
    mime.Disposition = "attachment"
    mime.Filename = strfile
    'mime.Encoding = "base64"
    mime.SetBodyFromPlainText(strtocrypt)

    TextBox1.Text = TextBox1.Text & "Original MIME message:" & vbCrLf
    TextBox1.Text = TextBox1.Text & mime.GetMime() & vbCrLf
    mime.AddPfxSourceFile("kiddecer.pfx", "kidde")
    success = mime.ConvertToSigned(cert)
    If (success <> True) Then
        MsgBox(mime.LastErrorText)
        Exit Function
    End If

    Dim cert2 As New Chilkat.Cert
    success = cert2.LoadFromFile("C:\PROD_Encryption.cer")
    If (success <> True) Then
        'Text1.Text = Text1.Text & cert.LastErrorText & vbCrLf
        Call MsgBox(cert2.LastErrorText, MsgBoxStyle.Critical, "Error1")
        Exit Function
    End If
    mime.UseXPkcs7 = False
    mime.Pkcs7CryptAlg = "3des"
    mime.Pkcs7KeyLength = 192
    mime.Encrypt(cert2)
    TextBox1.Text = TextBox1.Text & "----------------------------------" & vbCrLf
    TextBox1.Text = TextBox1.Text & "Signed Message:" & vbCrLf
    TextBox1.Text = TextBox1.Text & mime.GetMime() & vbCrLf

    mime.SaveMime(strfile + ".p7m")
End Function

The expected file should look like this:
Message-ID: 20631685.1349796827856.JavaMail.jg87553@LACMX31D1181661
Date: Tue, 9 Oct 2012 10:33:47 -0500 (CDT)
MIME-Version: 1.0
Content-Type: application/pkcs7-mime; smime-type=enveloped-data; 
    name="C:/SMIMETool/Inbound/A092501.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="C:/SMIMETool/Inbound/A092501.txt"

MIAGCSqGSIb3DQEHA6CAMIACAQAxggRRMIIBigIBADCB8jCB3TELMAkGA1UEBhMCVVMxFzAVBgNV BAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTswOQYD VQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3JwYSAoYykwOTEe MBwGA1UECxMVUGVyc29uYSBOb3QgVmFsaWRhdGVkMTcwNQYDVQQDEy5WZXJpU2lnbiBDbGFzcyAx

Help please!