Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AES-128 API Test Program
- -- Recommended Name: "encrypt"
- -- You might also want to look at my "FileHider" program; it should be available on my Pastebin.
- -- USAGE: encrypt [filename]
- -- Variables:
- local args = {...} -- Command-line parameters
- local data = {} -- File data (in bytes)
- local key = {} -- Our symmetric key.
- local iv = {} -- Our initalization vector.
- os.loadAPI("AES")
- -- Let's generate a key and IV pair now:
- for i=1, 16 do -- Iterate 16 times.
- key[i] = math.random(0, 0xFF) -- Every value in a block must be between 0 and 255 (0xFF).
- iv[i] = math.random(0, 0xFF)
- end
- -- For the unaware:
- -- 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.
- -- For AES, "blocks" are 16 bytes long.
- -- Now, let's read our file:
- local inputHandle = fs.open(args[1], "rb") -- Here, we're using binary mode simply for convenience.
- while true do
- local byte = inputHandle.read() -- Try to read a byte.
- if byte then -- Did we read a byte?
- table.insert(data, byte) -- If so, then add it to our "data" table.
- else
- break -- Otherwise, exit.
- end
- end
- inputHandle.close()
- local encryptedData = AES.encrypt_bytestream(data, key, iv) -- Encrypt the data we read with our random key and IV.
- -- 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.
- -- 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.
- -- Now, let's write the encrypted data:
- 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.
- for i=1, #encryptedData do
- outputHandle.write( encryptedData[i] )
- end
- outputHandle.close()
- -- Now, let's write the our key and IV pair, so we can decrypt the data later:
- 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.
- -- Write the key and the IV in sequence:
- for i=1, 16 do
- keyFileHandle.write( key[i] )
- end
- for i=1, 16 do
- keyFileHandle.write( iv[i] )
- end
- keyFileHandle.close()
- -- Print our key and IV pair:
- -- Variables to hold the hex representations of the key and IV:
- local key_str = ""
- local iv_str = ""
- for i=1, 16 do
- key_str = key_str..string.format("%X", key[i]) -- string.format("%X", i) converts a number to its hexadecimal representation (without the leading "0x")
- iv_str = iv_str..string.format("%X", iv[i])
- end
- print("File encrypted.")
- print("Key: "..key_str)
- print("IV: "..iv_str)
- -- I'm not going to write a decryptor program.
- -- If you want to decrypt data that was encrypted with this, then write the decryptor yourself.
- -- It should be a good test, or at least something to do.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement