Sample code for 30+ languages & platforms
Visual FoxPro

Encrypt MIME for Multiple Recipients

See more MIME Examples

Demonstrates encrypting the current MIME entity for several recipients at once. A certificate is added for each recipient with AddEncryptCert, and EncryptN then produces a single CMS/PKCS #7 message that each recipient can independently decrypt.

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. When a message targets more than one recipient, each recipient's certificate is added to the internal list and EncryptN wraps the message key separately for each one. Any single recipient can then decrypt with their own private key.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loMime
LOCAL loCert1
LOCAL loCert2

lnSuccess = 0

loMime = CreateObject('Chilkat.Mime')
lnSuccess = loMime.SetBodyFromPlainText("This message will be encrypted.")
IF (lnSuccess = 0) THEN
    ? loMime.LastErrorText
    RELEASE loMime
    CANCEL
ENDIF

*  Add a certificate for each intended recipient.  Every recipient receives an independently
*  wrapped copy of the message key.
loCert1 = CreateObject('Chilkat.Cert')
lnSuccess = loCert1.LoadFromFile("qa_data/recipient1.cer")
IF (lnSuccess = 0) THEN
    ? loCert1.LastErrorText
    RELEASE loMime
    RELEASE loCert1
    CANCEL
ENDIF

lnSuccess = loMime.AddEncryptCert(loCert1)
IF (lnSuccess = 0) THEN
    ? loMime.LastErrorText
    RELEASE loMime
    RELEASE loCert1
    CANCEL
ENDIF

loCert2 = CreateObject('Chilkat.Cert')
lnSuccess = loCert2.LoadFromFile("qa_data/recipient2.cer")
IF (lnSuccess = 0) THEN
    ? loCert2.LastErrorText
    RELEASE loMime
    RELEASE loCert1
    RELEASE loCert2
    CANCEL
ENDIF

lnSuccess = loMime.AddEncryptCert(loCert2)
IF (lnSuccess = 0) THEN
    ? loMime.LastErrorText
    RELEASE loMime
    RELEASE loCert1
    RELEASE loCert2
    CANCEL
ENDIF

*  Encrypt for every added recipient at once.
lnSuccess = loMime.EncryptN()
IF (lnSuccess = 0) THEN
    ? loMime.LastErrorText
    RELEASE loMime
    RELEASE loCert1
    RELEASE loCert2
    CANCEL
ENDIF

lnSuccess = loMime.SaveMime("qa_output/encrypted_multi.eml")
IF (lnSuccess = 0) THEN
    ? loMime.LastErrorText
    RELEASE loMime
    RELEASE loCert1
    RELEASE loCert2
    CANCEL
ENDIF

? "Message encrypted for multiple recipients."

RELEASE loMime
RELEASE loCert1
RELEASE loCert2