B4X Requires Chilkat v11.8.0+
B4X
Add PEM Text as a Decryption Source
See more MIME Examples
Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.
PEM text held in memory is loaded into a Pem object with LoadPem, and AddPemSource registers its certificates and private keys as a source for later decryption.
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. This is the in-memory counterpart of loading a PEM file with LoadPemFile, for cases where the PEM text is already in memory rather than residing in a file.
AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.Chilkat B4X Downloads
Dim success As Boolean = False
Dim mime As ChilkatMime
mime.Initialize
success = mime.LoadMimeFile("qa_data/encrypted.eml")
If success = False Then
Log(mime.LastErrorText)
Return
End If
' PEM text (a certificate and its private key) that is held in memory. Here it is loaded into a
' StringBuilder from a file.
Dim sbPem As ChilkatStringBuilder
sbPem.Initialize
success = sbPem.LoadFile("qa_data/recipient.pem", "utf-8")
If success = False Then
Log(sbPem.LastErrorText)
Return
End If
' Load the PEM text into a Pem object. The 2nd argument of LoadPem is the password of the private
' key (from a secure source).
Dim pemPassword As String = "myPemPassword"
Dim pem As ChilkatPem
pem.Initialize
success = pem.LoadPem(sbPem.GetAsString, pemPassword)
If success = False Then
Log(pem.LastErrorText)
Return
End If
' Add the Pem object as a source of certificates and private keys for later decryption.
success = mime.AddPemSource(pem)
If success = False Then
Log(mime.LastErrorText)
Return
End If
success = mime.Decrypt
If success = False Then
Log(mime.LastErrorText)
Return
End If
Log("Decrypted.")