Sample code for 30+ languages & platforms
Go Requires Chilkat v11.0.0+

Encrypt with RSA-OAEP and AES-128-CBC

See more Email Object Examples

Use this profile when the receiver requires AES-128-CBC content encryption and RSA-OAEP with SHA-256. The SHA-256 settings apply to OAEP and its mask generation function. Use the matching decryption example to inspect the result.

Chilkat Go Downloads

Go
    success := false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    email := chilkat.NewEmail()
    success = email.LoadEml("message.eml")
    if success != true {
        fmt.Println(email.LastErrorText())
        email.DisposeEmail()
        return
    }

    //  Use the recipient's public encryption certificate. Its private key stays with the recipient.
    cert := chilkat.NewCert()
    success = cert.LoadFromFile("recipient.cer")
    if success != true {
        fmt.Println(cert.LastErrorText())
        email.DisposeEmail()
        cert.DisposeCert()
        return
    }

    success = email.SetEncryptCert(cert)
    if success != true {
        fmt.Println(email.LastErrorText())
        email.DisposeEmail()
        cert.DisposeCert()
        return
    }

    email.SetPkcs7CryptAlg("aes")
    email.SetPkcs7KeyLength(128)
    email.SetOaepPadding(true)
    email.SetOaepHash("sha256")
    email.SetOaepMgfHash("sha256")
    email.SetSendEncrypted(true)
    mail := chilkat.NewMailMan()
    rendered := chilkat.NewBinData()
    success = mail.RenderToMimeBd(email,rendered)
    if success != true {
        fmt.Println(mail.LastErrorText())
        email.DisposeEmail()
        cert.DisposeCert()
        mail.DisposeMailMan()
        rendered.DisposeBinData()
        return
    }

    success = rendered.WriteFile("oaep-encrypted.eml")
    if success != true {
        fmt.Println("Failed: rendered.WriteFile.")
        email.DisposeEmail()
        cert.DisposeCert()
        mail.DisposeMailMan()
        rendered.DisposeBinData()
        return
    }

    fmt.Println("Saved oaep-encrypted.eml.")

    email.DisposeEmail()
    cert.DisposeCert()
    mail.DisposeMailMan()
    rendered.DisposeBinData()