Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.71+

IMAP Login Secure

Demonstrates how to use the LoginSecure method.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local imap = chilkat.Imap.new()

-- Connect to an IMAP server..
imap.ssl = true
imap.port = 993
local success = imap:connect("imap.mail.me.com")
if not success then
    print(imap.last_error_text)
    return
end

-- Imagine we've previously saved our encrypted login and password within a JSON config file
-- that contains this:

-- {
--   "imap_login": "fLkxsfnVaIWLiL/R32jo6g==",
--   "imap_password": "/NbaFBoCftBLFf8WBU9Xtw=="
-- }

local json = chilkat.JsonObject.new()
json:load_file("qa_data/passwords/imap.json")

local crypt = chilkat.Crypt2.new()

-- These are the encryption settings we previously used to encrypt the credentials within the JSON config file.
crypt.crypt_algorithm = "aes"
crypt.cipher_mode = "cbc"
crypt.key_length = 128
crypt:set_encoded_key("000102030405060708090A0B0C0D0E0F", "hex")
crypt:set_encoded_iv("000102030405060708090A0B0C0D0E0F", "hex")
crypt.encoding_mode = "base64"

local ssLogin = chilkat.SecureString.new()
local ssPassword = chilkat.SecureString.new()

-- Decrypt to the secure string.  (the strings will still held in memory encrypted, but are now encrypted using
-- a randomly generated session key.)
crypt:decrypt_secure_enc(json:string_of("imap_login"), ssLogin)
crypt:decrypt_secure_enc(json:string_of("imap_password"), ssPassword)

-- Pass the credentials to the LoginSecure method:
success = imap:login_secure(ssLogin, ssPassword)
if not success then
    print(imap.last_error_text)
    return
end

-- ...

imap:disconnect()