Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMime::ckSetBodyFromPlainText(mime,"This message will be encrypted.")
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ;  Pkcs7CryptAlg is the symmetric content-encryption algorithm.  Supported values are aes, aes-gcm,
    ;  des, 3des, and rc2.  The default is aes.
    CkMime::setCkPkcs7CryptAlg(mime, "aes")

    ;  Pkcs7KeyLength is the content-encryption key length in bits.  The default is 128.
    CkMime::setCkPkcs7KeyLength(mime, 256)

    ;  OaepPadding selects the RSA key-transport padding.  The default is 0 (RSAES-PKCS1-v1_5).
    ;  Set 1 to use RSAES-OAEP, in which case the OAEP hash settings apply.
    CkMime::setCkOaepPadding(mime, 1)
    CkMime::setCkOaepHash(mime, "sha256")
    CkMime::setCkOaepMgfHash(mime, "sha256")

    ;  Load the recipient certificate (public key is sufficient for encrypting).
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadFromFile(cert,"qa_data/recipient.cer")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ;  Encrypt using the algorithm settings configured above.
    success = CkMime::ckEncrypt(mime,cert)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    Debug "Encrypted with " + CkMime::ckPkcs7CryptAlg(mime) + " " + Str(CkMime::ckPkcs7KeyLength(mime)) + "-bit key."


    CkMime::ckDispose(mime)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure