--**************************************
-- @package crypto.lib
-- @author Valentin Schmidt
-- @version 1.1
-- Simple encryption/decryption functions that are compatible with
-- the PHP function counterparts in script 'crypto.lib.php' and the
-- C# counterpart class in script 'Crypto.cs'.
--
-- Crypto Xtra AES specs:
-- - AES-128 Encryption
-- - Mode: CBC
-- - IV prepended to encrypted data
-- - optional 12 byte header starts with 'AES', byte 5 contains pad_length
--**************************************
----------------------------------------
-- Encrypts a string with AES-128, returns result encoded with Base64
--
-- @param {string} input
-- @param {string} key - 16-32 bytes (alternatively use raw md5 checksum)
-- @param {bool} [addHeader] - optional, default=false
-- @return {string} encrypted string, Base64 encoded
----------------------------------------
on encrypt_string (input, key, addHeader)
-- key = cx_md5_string(key, true) -- optional, to allow arbitrary key length
if voidP(addHeader) then addHeader=false
ba = cx_aes_encrypt_string(input, key, addHeader)
return cx_base64_encode_string(ba)
end
----------------------------------------
-- Decrypts a string that was encrypted with AES-128 and encoded with Base64
--
-- @param {string} input - encrypted string, Base64 encoded
-- @param {string} key - 16-32 bytes (alternatively use raw md5 checksum)
-- @returns {string}
----------------------------------------
on decrypt_string (input, key)
-- key = cx_md5_string(key, true) -- optional, to allow arbitrary key length
input = cx_base64_decode_string(input)
ba = cx_aes_decrypt_string (input, key)
return ba.readRawString(ba.length)
end
----------------------------------------
--
----------------------------------------
--on test
-- input = "Hello World!"
-- key = "abcdefghijklpqrs"
-- addHeader = true
--
-- -- encrypt
-- encrypted = encrypt_string (input, key, addHeader)
-- put "Encrypted:" && encrypted
--
-- -- decrypt
-- decrypted = decrypt_string (encrypted, key)
-- put "Decrypted:" && decrypted
--end