Sample code for 30+ languages & platforms
DataFlex

Decrypt a Password-Encrypted Email with AES

See more Email Object Examples

Demonstrates the Chilkat Email.AesDecrypt method, which decrypts and restores an email message previously encrypted with AesEncrypt. The password must be the same one used to encrypt. This is Chilkat's password-based email-object decryption — it is not S/MIME certificate-based decryption. This example encrypts an email and then decrypts it to recover the original body.

Background: Symmetric encryption is reversible with the same secret: the identical password that scrambled the message unscrambles it. There is no separate "public" and "private" key as in S/MIME — which makes the scheme simple but means anyone holding the password can both encrypt and decrypt. The round trip shown here (encrypt then decrypt) is a common way to verify the password and confirm the content is faithfully restored.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    Boolean iSuccess
    String sTemp1

    //  Demonstrates the AesDecrypt method, which decrypts and restores an email that was
    //  previously encrypted with AesEncrypt.  The password must match the one used to encrypt.
    //  This example encrypts an email, then decrypts it to restore the original content.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Confidential"
    Set ComBody Of hoEmail To "Secret contents."

    //  Encrypt with a password...
    Get ComAesEncrypt Of hoEmail "secret_password" To iSuccess

    //  ...then decrypt with the same password to restore the original email.
    Get ComAesDecrypt Of hoEmail "secret_password" To iSuccess

    Get ComBody Of hoEmail To sTemp1
    Showln "Body after decrypt = " sTemp1


End_Procedure