Sample code for 30+ languages & platforms
Visual Basic 6.0

Get the Certificate Used for Decryption

See more MIME Examples

Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.

After a successful decryption, LastDecryptCert loads the certificate(s) actually used into a Cert object, indexed from zero up to NumDecryptCerts minus one.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. Following decryption or security unwrapping, Chilkat records which certificate(s) were used. LastDecryptCert retrieves them so the application can report or verify the identity involved.

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

Dim mime As New ChilkatMime
success = mime.LoadMimeFile("qa_data/encrypted.eml")
If (success = 0) Then
    Debug.Print mime.LastErrorText
    Exit Sub
End If

'  Configure a decryption source and decrypt.
Dim pfxPassword As String
pfxPassword = "myPfxPassword"
success = mime.AddPfxSourceFile("qa_data/recipient.pfx",pfxPassword)
If (success = 0) Then
    Debug.Print mime.LastErrorText
    Exit Sub
End If

success = mime.Decrypt()
If (success = 0) Then
    Debug.Print mime.LastErrorText
    Exit Sub
End If

'  After a successful decryption, load the certificate(s) that were used.  The 1st argument is the
'  zero-based index and the 2nd is a Cert that receives the certificate.
Dim n As Long
n = mime.NumDecryptCerts
Dim cert As New ChilkatCert
Dim i As Long
For i = 0 To n - 1
    success = mime.LastDecryptCert(i,cert)
    If (success = 0) Then
        Debug.Print mime.LastErrorText
        Exit Sub
    End If

    Debug.Print "Decrypted with: " & cert.SubjectCN
Next