Advertisement
Tatantyler

AES-128 Example Program

May 5th, 2013
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. -- AES-128 API Test Program
  2. -- Recommended Name: "encrypt"
  3.  
  4. -- You might also want to look at my "FileHider" program; it should be available on my Pastebin.
  5.  
  6. -- USAGE: encrypt [filename]
  7.  
  8. -- Variables:
  9. local args = {...} -- Command-line parameters
  10. local data = {} -- File data (in bytes)
  11. local key = {} -- Our symmetric key.
  12. local iv = {} -- Our initalization vector.
  13.  
  14. os.loadAPI("AES")
  15.  
  16. -- Let's generate a key and IV pair now:
  17. for i=1, 16 do -- Iterate 16 times.
  18.     key[i] = math.random(0, 0xFF) -- Every value in a block must be between 0 and 255 (0xFF).
  19.     iv[i] = math.random(0, 0xFF)
  20. end
  21.  
  22. -- For the unaware:
  23. -- When using AES (or any block cipher, for that matter), the input plaintext (i.e the data you want to encrypt) is broken up into "blocks", each with the same size and format.
  24. -- For AES, "blocks" are 16 bytes long.
  25.  
  26. -- Now, let's read our file:
  27. local inputHandle = fs.open(args[1], "rb") -- Here, we're using binary mode simply for convenience.
  28. while true do
  29.     local byte = inputHandle.read() -- Try to read a byte.
  30.     if byte then -- Did we read a byte?
  31.         table.insert(data, byte) -- If so, then add it to our "data" table.
  32.     else
  33.         break -- Otherwise, exit.
  34.     end
  35. end
  36. inputHandle.close()
  37.  
  38. local encryptedData = AES.encrypt_bytestream(data, key, iv) -- Encrypt the data we read with our random key and IV.
  39.  
  40. -- If you're doing something like this (encrypting a file), you might want to include a way to show that the file was successfully decrypted.
  41. -- For example, the FileHider program inserts 0x44, 0x41, 0x54, 0x41 into the plaintext before encrypting. Then, when decrypting, we can simply check the first 4 bytes of the decrypted plaintext. If they match, then the decryption was successful.
  42.  
  43. -- Now, let's write the encrypted data:
  44. local outputHandle = fs.open(args[1], "wb") -- We need to write the file in binary mode; Text-mode formats what you write to the file, which may destroy data.
  45. for i=1, #encryptedData do
  46.     outputHandle.write( encryptedData[i] )
  47. end
  48. outputHandle.close()
  49.  
  50. -- Now, let's write the our key and IV pair, so we can decrypt the data later:
  51. local keyFileHandle = fs.open(args[1]..".keys", "wb") -- This time, simply write to [filename].keys; the user can rename the file from the commandline later if they wish.
  52. -- Write the key and the IV in sequence:
  53. for i=1, 16 do
  54.     keyFileHandle.write( key[i] )
  55. end
  56.  
  57. for i=1, 16 do
  58.     keyFileHandle.write( iv[i] )
  59. end
  60. keyFileHandle.close()
  61.  
  62. -- Print our key and IV pair:
  63.  
  64. -- Variables to hold the hex representations of the key and IV:
  65. local key_str = ""
  66. local iv_str = ""
  67. for i=1, 16 do
  68.     key_str = key_str..string.format("%X", key[i]) -- string.format("%X", i) converts a number to its hexadecimal representation (without the leading "0x")
  69.     iv_str = iv_str..string.format("%X", iv[i])
  70. end
  71.  
  72. print("File encrypted.")
  73. print("Key: "..key_str)
  74. print("IV: "..iv_str)
  75.  
  76. -- I'm not going to write a decryptor program.
  77. -- If you want to decrypt data that was encrypted with this, then write the decryptor yourself.
  78. -- It should be a good test, or at least something to do.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement