B4X Requires Chilkat v11.8.0+
B4X
Add a PEM Source from PEM Text in Memory
See more Email Object Examples
Demonstrates the Chilkat Email.AddPemSource method with PEM text that is held in memory. The PEM text is loaded into a Pem object with LoadPem, whose second argument is the password of the private key, and the Pem object is added to the list of certificate and private-key sources used during decryption. Call AddPemSource before obtaining the encrypted email so Chilkat can decrypt it automatically.
Background:
Pem.LoadPem loads PEM text that is already in memory. It is the right choice when the certificate and private key come from somewhere other than a file — a database, a secrets manager, or an HTTPS download — letting you supply them without first writing sensitive key material to disk. (Pem.LoadPemFile loads a PEM file directly.) AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.Chilkat B4X Downloads
Dim success As Boolean = False
' Demonstrates the AddPemSource method with PEM text that is held in memory. The certificates
' and private keys of the Pem object are added to the list of certificate and private-key
' sources used during decryption. Call it before obtaining the encrypted email.
Dim email As ChilkatEmail
email.Initialize
' The PEM text (a certificate and its private key) is in memory. Here it is loaded into a
' StringBuilder from a file; it could come from a database, a secrets manager, or a download.
Dim sbPem As ChilkatStringBuilder
sbPem.Initialize
success = sbPem.LoadFile("qa_data/certs/decryption.pem", "utf-8")
If success = False Then
Log(sbPem.LastErrorText)
Return
End If
' Load the PEM text into a Pem object. The 2nd argument is the password of the private key.
' (Pass an empty string if the private key is not encrypted.)
Dim pem As ChilkatPem
pem.Initialize
success = pem.LoadPem(sbPem.GetAsString, "pem_password")
If success = False Then
Log(pem.LastErrorText)
Return
End If
' Add the Pem object as a source of the certificate + private key.
success = email.AddPemSource(pem)
If success = False Then
Log(email.LastErrorText)
Return
End If
' Load an encrypted email; Chilkat decrypts it using the PEM source.
success = email.LoadEml("qa_data/eml/encrypted.eml")
If success = False Then
Log(email.LastErrorText)
Return
End If
Log("Decrypted = " & email.Decrypted)
' Note: Paths such as "qa_data/..." are relative local filesystem paths,
' relative to the current working directory of the running application.