Sample code for 30+ languages & platforms
DataFlex

MIME S/MIME Encryption Algorithm Properties

See more MIME Examples

Demonstrates the properties that control S/MIME encryption: Pkcs7CryptAlg (the symmetric content-encryption algorithm), Pkcs7KeyLength (the content-encryption key length in bits), OaepPadding (whether RSAES-OAEP key transport is used instead of RSAES-PKCS1-v1_5), and the OAEP hash settings OaepHash and OaepMgfHash. The properties are configured before calling Encrypt.

Background. CMS/PKCS #7 encryption uses a symmetric algorithm to protect the content and an RSA key-transport scheme to protect the symmetric key. The defaults (aes, 128-bit, PKCS1-v1_5 padding) work broadly; these properties tune the algorithms and padding.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMime
    Variant vCert
    Handle hoCert
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get ComSetBodyFromPlainText Of hoMime "This message will be encrypted." To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Pkcs7CryptAlg is the symmetric content-encryption algorithm.  Supported values are aes, aes-gcm,
    //  des, 3des, and rc2.  The default is aes.
    Set ComPkcs7CryptAlg Of hoMime To "aes"

    //  Pkcs7KeyLength is the content-encryption key length in bits.  The default is 128.
    Set ComPkcs7KeyLength Of hoMime To 256

    //  OaepPadding selects the RSA key-transport padding.  The default is False (RSAES-PKCS1-v1_5).
    //  Set True to use RSAES-OAEP, in which case the OAEP hash settings apply.
    Set ComOaepPadding Of hoMime To True
    Set ComOaepHash Of hoMime To "sha256"
    Set ComOaepMgfHash Of hoMime To "sha256"

    //  Load the recipient certificate (public key is sufficient for encrypting).
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/recipient.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Encrypt using the algorithm settings configured above.
    Get pvComObject of hoCert to vCert
    Get ComEncrypt Of hoMime vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComPkcs7CryptAlg Of hoMime To sTemp1
    Get ComPkcs7KeyLength Of hoMime To iTemp1
    Showln "Encrypted with " sTemp1 " " iTemp1 "-bit key."


End_Procedure