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

RSA Encrypting Symmetric Secret Key

See more RSA Examples

The RSA encryption algorithm is computationally expensive. It is not the best choice for encrypting large amounts of data. Symmetric encryption algorithms such as AES (i.e. Rijndael) or Blowfish are much more efficient. A typical application scenario is that you want to send encrypted messages to a partner, but you don't want to send the symmetric key unprotected. A solution is to generate a public/private RSA key pair and provide your partner with the public key (in advance). You may then encrypt the symmetric algorithm's key using the RSA private key. Next, encrypt the message using the symmetric algorithm, and send your partner both the encrypted key and encrypted message. Your partner decrypts by first RSA decrypting the key, and then uses the decrypted symmetric key to decrypt the message content.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local rsa = chilkat.Rsa.new()

-- Load a public/private key pair from a .snk key file.
-- Assume you have already provided your partner with
-- the public key part of the key pair.
local xmlKey = rsa:snk_to_xml("qa_data/rsaKeys/test.snk")
if not rsa.last_method_success then
    print(rsa.last_error_text)
    return
end

local pubKey = chilkat.PublicKey.new()
local success = pubKey:load_from_string(xmlKey)
if not success then
    print(pubKey.last_error_text)
    return
end

rsa:use_public_key(pubKey)

-- Our message data will be encrypted using 128-bit AES
-- encryption, using CBC (cipher-block chaining).
local crypt = chilkat.Crypt2.new()
crypt.crypt_algorithm = "aes"
crypt.cipher_mode = "cbc"
crypt.key_length = 128

-- Generate 128-bit (16 bytes) secret key and return as a hex string.
crypt.encoding_mode = "hex"
local secretKey = crypt:gen_random_bytes_enc(16)
print("Unencrypted Key: " .. secretKey)

-- Use the key we generated:
crypt:set_encoded_key(secretKey, "hex")

-- RSA encrypt the secret key and return as a hex string:
rsa.encoding_mode = "hex"
local bUsePrivateKey = false
local encryptedKey = rsa:encrypt_string_enc(secretKey, bUsePrivateKey)

-- Symmetric encrypt a message.  For this example the message
-- is very short, but typically this is where a large amount
-- of data may be encrypted.
crypt.encoding_mode = "base64"
local encryptedText = crypt:encrypt_string_enc("Hello World!")

-- Show our encrypted key and encrypted text:
print("Encrypted Key: " .. encryptedKey)
print("Encrypted Text: " .. encryptedText)

-- Assume we sent these strings to our partner...

-- Here's what we do at the partner end:
local privKey = chilkat.PrivateKey.new()
success = privKey:load_xml(xmlKey)
if not success then
    print(privKey.last_error_text)
    return
end

rsa:use_private_key(privKey)

-- First, decrypt the encryptedKey:
bUsePrivateKey = true
local decryptedKey = rsa:decrypt_string_enc(encryptedKey, bUsePrivateKey)
print("Decrypted Key: " .. decryptedKey)

-- Set our crypt object's properties and secret key:
local crypt2 = chilkat.Crypt2.new()
crypt2.crypt_algorithm = "aes"
crypt2.cipher_mode = "cbc"
crypt2.key_length = 128
crypt2.encoding_mode = "base64"
crypt2:set_encoded_key(decryptedKey, "hex")

-- Decrypt the message:
local decryptedText = crypt2:decrypt_string_enc(encryptedText)
print("Decrypted Text: " .. decryptedText)