Compression, encryption, encoding and the reverse ...

Posted by Nick Gammon on Thu 09 Dec 2004 03:40 AM — 2 posts, 10,109 views.

Australia Forum Administrator #0
Putting it all together now, we can make a simple function that is handy for sending encrypted messages to friends.


function compress_encrypt_encode (s, k)
  return utils.base64encode (aes.encrypt (utils.compress (s), k), true)
end -- compress_encrypt_encode 

function decode_decrypt_decompress (s, k)
  return utils.decompress (aes.decrypt (utils.base64decode (s), k))
end  -- decode_decrypt_decompress 


The function compress_encrypt_encode:

  • Compresses the text you supply
  • Encrypts it with the supplied key
  • Base-64 encodes it, suitable for putting in an email or "tell" or similar (change "true" to "false" if you want a single-line rather than breaking every 76 columns)


Example:


print (compress_encrypt_encode (
[[
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
]], "swordfish"))

Output

D4TNSdaDQhfnozmTc8bm9atmofRB+sxmJTUnThGMAvPnKjAPYX+jCJ08cB+uAynt5kX97hHiDma5
EQm14twOsY4hIylmQYtcquZjgCWCu2/sN/lumDYa6IDJiVr+7OI3ajsqR5TPK1BPHe4SrdTt1ntj
XnkDA6GmAI6DJQ7tprRfRZKqO8czeC1dju/dktvlHFZSrmj06HfIsaXyVwwMxFGMMR/RcjyC9Zwq
x3qRLx/vOt1rIbApY4CyNRP+AmfU346sOkG90Yh5OO9DnUU6M+gliRlplQ6+DGdoLe61+tQwaXQj
YOm9v2Qw+PnYLGiyv/ElrKo1n9+tGuD8fCDHMJOHizUrGCBloQMVoVG3h9hlgWJPMBHmxSwn3Go2
wjgFV9SWpznXkngXSt1F5YPgkA==





The function decode_decrypt_decompress:

  • Decodes the base-64 encoded text
  • Decrypts it with the supplied key
  • Decompresses it


Example (using above results as input):


/print (decode_decrypt_decompress ([[
D4TNSdaDQhfnozmTc8bm9atmofRB+sxmJTUnThGMAvPnKjAPYX+jCJ08cB+uAynt5kX97hHiDma5
EQm14twOsY4hIylmQYtcquZjgCWCu2/sN/lumDYa6IDJiVr+7OI3ajsqR5TPK1BPHe4SrdTt1ntj
XnkDA6GmAI6DJQ7tprRfRZKqO8czeC1dju/dktvlHFZSrmj06HfIsaXyVwwMxFGMMR/RcjyC9Zwq
x3qRLx/vOt1rIbApY4CyNRP+AmfU346sOkG90Yh5OO9DnUU6M+gliRlplQ6+DGdoLe61+tQwaXQj
YOm9v2Qw+PnYLGiyv/ElrKo1n9+tGuD8fCDHMJOHizUrGCBloQMVoVG3h9hlgWJPMBHmxSwn3Go2
wjgFV9SWpznXkngXSt1F5YPgkA==
]], "swordfish"))

Output

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Amended on Thu 09 Dec 2004 03:47 AM by Nick Gammon
Australia Forum Administrator #1
This is where Lua's multi-line quotes [[ and ]] come into handy. You can just use those and copy and paste swags of text inbetween them.