Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Can we encrypt a single file using AES cryptography? — Gideros Forum

Can we encrypt a single file using AES cryptography?

piepie Member
edited February 2023 in General questions
Hi, I need to test and debug sending stuff to other people and I make use of lualfs to copy a single file (that contains useful info) from |D| to a specific path. That file has to be sent back to me through email.

I was wondering if I could encrypt this file while copying it to keep its content hidden, instead of parsing and encrypting every single string inside it.


This is the working code, thanks to hgy29:
 
require "lfs"
 
 
local key = "GiderosGideros11"
local iv = "GiderosRules2023"
local paddingType = 1
 
 
local function cryptCopy(src, dst)
	local srcf = io.open(src, "rb")
	local dstf = io.open(dst, "wb")
	local size = 2^13 -- good buffer size (8K)
	while true do
		local block = srcf:read(size)
		if not block then break end
		block = Cryptography.aesEncrypt(block,key,iv,paddingType)
		dstf:write(block)
	end
	srcf:close()
	dstf:close()
end
 
local function decryptCopy(src, dst)
	local srcf = io.open(src, "rb")
	local dstf = io.open(dst, "wb")
	local size = 2^13 + 16 -- good buffer size (8K + 16 for padding)
	while true do
		local block = srcf:read(size)
		if not block then break end
		block = Cryptography.aesDecrypt(block,key,iv,paddingType)
		dstf:write(block)
	end
	srcf:close()
	dstf:close()
end
 
cryptCopy ("|D|myfile.sqlite", "C:/tmp/myfile.crypt")
decryptCopy ("C:/tmp/myfile.crypt", "C:/tmp/myfile.sqlite")

[edit: updated after success]

Likes: MoKaLux

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • hgy29hgy29 Maintainer
    You'll definitely want to use padding if your input blocks aren't a multiple of 16 bytes, since AES operates only with 16 bytes blocks.
    You use a 8k buffer, which is 16 byte aligned so your code should work, but you use "r" for reading your files, even the crypted one, while you should really use "rb", to avoid line endings to be converted by the operating system.

    Likes: MoKaLux, pie

    +1 -1 (+2 / -0 )Share on Facebook
  • Thank you @hgy29, that is indeed a good point

    Unfortunately I get the same exact error ( Invalid PKCS#7 padding value) using "rb" instead of "r" for reading files if I set paddingType as 0 or 1.
  • hgy29hgy29 Maintainer
    Accepted Answer
    Yes, it is obvious on second thought :) if you give a 16 byte aligned buffer and ask for padding, then gideros needs to add 16 more bytes of padding, because the padding byte indicates the padding size, so 0 isn't allowed. so you write blocks of 8k+16, but you only read 8k when decoding. Try to read buffers of 8k+16 in the decrypting function

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • It's obvious for you.. :smiley: it works!
    Thank you a lot, we're lucky for having you here!

    updating the code in the first post to use for anyone later

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • thank you guys it goes straight to the wiki :)

    Likes: pie

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.