1. --**************************************
  2. -- @package crypto.lib
  3. -- @author Valentin Schmidt
  4. -- @version 1.1
  5.  
  6. -- Simple encryption/decryption functions that are compatible with
  7. -- the PHP function counterparts in script 'crypto.lib.php' and the
  8. -- C# counterpart class in script 'Crypto.cs'.
  9. --
  10. -- Crypto Xtra AES specs:
  11. -- - AES-128 Encryption
  12. -- - Mode: CBC
  13. -- - IV prepended to encrypted data
  14. -- - optional 12 byte header starts with 'AES', byte 5 contains pad_length
  15. --**************************************
  16.  
  17. ----------------------------------------
  18. -- Encrypts a string with AES-128, returns result encoded with Base64
  19. --
  20. -- @param {string} input
  21. -- @param {string} key - 16-32 bytes (alternatively use raw md5 checksum)
  22. -- @param {bool} [addHeader] - optional, default=false
  23. -- @return {string} encrypted string, Base64 encoded
  24. ----------------------------------------
  25. on encrypt_string (input, key, addHeader)
  26.     -- key = cx_md5_string(key, true) -- optional, to allow arbitrary key length
  27.     if voidP(addHeader) then addHeader=false
  28.     ba = cx_aes_encrypt_string(input, key, addHeader)
  29.     return cx_base64_encode_string(ba)
  30. end
  31.  
  32. ----------------------------------------
  33. -- Decrypts a string that was encrypted with AES-128 and encoded with Base64
  34. --
  35. -- @param {string} input - encrypted string, Base64 encoded
  36. -- @param {string} key - 16-32 bytes (alternatively use raw md5 checksum)
  37. -- @returns {string}
  38. ----------------------------------------
  39. on decrypt_string (input, key)
  40.     -- key = cx_md5_string(key, true) -- optional, to allow arbitrary key length
  41.     input = cx_base64_decode_string(input)
  42.     ba = cx_aes_decrypt_string (input, key)
  43.     return ba.readRawString(ba.length)
  44. end
  45.  
  46. ----------------------------------------
  47. --
  48. ----------------------------------------
  49. --on test
  50. --  input = "Hello World!"
  51. --  key = "abcdefghijklpqrs"
  52. --  addHeader = true
  53. --
  54. --  -- encrypt
  55. --  encrypted = encrypt_string (input, key, addHeader)
  56. --  put "Encrypted:" && encrypted
  57. --
  58. --  -- decrypt
  59. --  decrypted = decrypt_string (encrypted, key)
  60. --  put "Decrypted:" && decrypted
  61. --end
  62.  
[raw code]