DATA Encryption

Hi,

I will definitely not get out of this encryption problem.

Here my last request.

With this following code, I can encrypt a local file and send it via FTP.

require 'sched'
require 'print'


local pack 			= require 'pack'		-- library used for the print hex function 
local devicetree 	= require 'devicetree' 	-- access to system variables like eg serial number, UART reservations
local system     	= require 'system' 		-- system lib
local sched      	= require "sched"   	-- Lua scheduling and synchronization lib
local serial     	= require "serial"  	-- access to serial APIs
local os         	= require "os"      	-- Operating System lib
local airvantage  	= require "airvantage" 	-- AirVantage lib
local string 		= require "string"		-- String parsing lib
local ftp 			= require'socket.ftp'	-- FTP lib
local timer 		= require'timer'		-- timer lib
local lock          = require'sched.lock'   -- lock lib
local cipher        = require 'crypto.cipher'

local message = [[
DATE;TRAME
02/09/2015 00:00:00;681944AE0C1402301001077AE00001002F2F0F7F010101E600017216
02/09/2015 00:01:00;681B44AE0C9503201001077A5C0000002F2F0F7F0202010201A101013C16
02/09/2015 00:00:00;681944AE0C1402301001077AE00001002F2F0F7F010101E600017216
02/09/2015 00:01:00;681B44AE0C9503201001077A5C0000002F2F0F7F0202010201A101013C16
02/09/2015 00:00:00;681944AE0C1402301001077AE00001002F2F0F7F010101E600017216
02/09/2015 00:01:00;681B44AE0C9503201001077A5C0000002F2F0F7F0202010201A101013C16
02/09/2015 00:00:00;681944AE0C1402301001077AE00001002F2F0F7F010101E600017216
02/09/2015 00:01:00;681B44AE0C9503201001077A5C0000002F2F0F7F0202010201A101013C16
02/09/2015 00:00:00;681944AE0C1402301001077AE00001002F2F0F7F010101E600017216
02/09/2015 00:01:00;681B44AE0C9503201001077A5C0000002F2F0F7F0202010201A101013C16
02/09/2015 00:00:00;681944AE0C1402301001077AE00001002F2F0F7F010101E600017216
02/09/2015 00:01:00;681B44AE0C9503201001077A5C0000002F2F0F7F0202010201A101013C16
02/09/2015 00:00:00;681944AE0C1402301001077AE00001002F2F0F7F010101E600017216
02/09/2015 00:01:00;681B44AE0C9503201001077A5C0000002F2F0F7F0202010201A101013C16
]]

local encrypted_message

-- variable for radio link state 
local state

-- variable for signal strength
local signalstrength


function FTP(string)

	local content
	local file_to_send
	local Heure_FTP = os.date("%H")
	local FTP_Success = 0
	
	print("inside FTP, UART file selection: ", string)

	-- append time in file name
	local file_name = string .. '\95' .. ".csv"
	print("Nom de fichier:", file_name)

	-- create string argument for the FTP call
	local put_argument = "ftp://isiobox:c2MjBu@ftp.techosb.com/_TEST/MODEM/" .. file_name
		
	-- get rssi and radio state just before triggering FTP, and log them
	-- state equals 0 means: radio link is down
	-- state equals 1 means: radio link is up
	-- state equals 2 means: radio link is inactive
	state = devicetree.get('system.cellular.link.state')
	signalstrength = devicetree.get('system.cellular.link.rssi')
	print("radio link state just before triggering FTP: ", state)
	print("rssi just before triggering FTP: ", signalstrength)
	
	
	-- FTP the file

	
	local e, f = ftp.put(put_argument, encrypted_message) 
		

	sched.wait(15)
	
	-- e is the number of bytes of the FTP file
	print("FTP return e: ", e)
	-- f is nil in case of FTP success
	print("FTP return f: ", f)

	-- !!! foresee actions in case of error to the FTP call, eg retries, and prevent erasing file in below loop
	-- take care to memory limit of 8MB space if files are continuously appended with new data and not regularly flushed
end


local function main()

	devicetree.init()
	system.init()
    --initialize cipher aes-128 with chaining ctr to encrypt
    --key must be 16 characters (128 bits)
    --initial vector (iv) is must be 16 characters (128 bits)
    local aesctr_enc, err = cipher.new({name="aes", mode="enc", key="0123456789ABCDEF"},{name="ctr",iv="0123456789ABCDEF"})
    if not aesctr_enc then
        print(err)
        return
    end
    print("cipher OK")
    print(message)
    --encrypt
    encrypted_message = aesctr_enc:process(message)
    print(encrypted_message)
    
end

sched.run(main)
timer.new("03 * * * *", FTP, "LUA_Crypt")
sched.loop()

Then I can get back the file on the FTP on my computer (in local) and I can decrypt normaly the file with the following request :

openssl enc -d -aes-128-ctr -K 30313233343536373839414243444546 -iv 30313233343536373840414243444546 -in C:\Users\sebastien.vitrai\Desktop\Encryption\IN\LUA_Test_V1_150924_09.csv -out C:\Users\sebastien.vitrai\Desktop\Encryption\OUT\LUA_Encypt_OK.csv

This method works good.
But when I use my LUA program, I can’t decrypt a file.
My program consist to read data on the serial port and store it.
I write the data on two file in RAM. I d’ont want to use Flash Memory. See the following code :

local Fichier_1 = "DATE;TRAME"..'\10'
local Fichier_2 = "DATE;TRAME"..'\10'

function checkintegrity(buffer)

	
	
	....
		--From UART
			if string.byte(buffer,g+length+2) == 22 and string.byte(buffer,g+3) == 174 and string.byte(buffer,g+4) == 12 then
				then
				print("Product Enless and 16 found, Trame complete")
	 			local trame = tohex(string.sub(buffer, g, g+length+2))
	 			print("la trame est :", trame)
	 				if Heure%2 ~= 0 then 
	 					Fichier_1 = Fichier_1 .. os.date("%Y/%m/%d")..'\32'.. os.date("%H:%M:%S") ..';'.. tohex(string.sub(buffer, g, g+length+2)) ..'\10' 
						print ("Fichier 1:")
						print (Fichier_1)
	 					g = string.find (buffer, '\104', Fin_Trame)
	 					print ("Position dans le buffer :",g)
	 				else
	 					Fichier_2 = Fichier_2 .. os.date("%Y/%m/%d")..'\32'.. os.date("%H:%M:%S") ..';'.. tohex(string.sub(buffer, g, g+length+2)) ..'\10'
						print ("Fichier 2:")
						print (Fichier_2)
	 					g = string.find (buffer, '\104', Fin_Trame)
	 					print ("Position dans le buffer :",g)
	 				end

          ......
              end
end

I write in hexadecimal in the two file and when I use the same request to decrypt :

openssl enc -d -aes-128-ctr -K 30313233343536373839414243444546 -iv 30313233343536373840414243444546 -in C:\Users\sebastien.vitrai\Desktop\Encryption\IN\LUA_Test_V1_150924_09.csv -out C:\Users\sebastien.vitrai\Desktop\Encryption\OUT\LUA_Encypt_OK.csv

It doesn’t work. It decrypt only the header : “DATE;TRAME”.

I think it doesn’t work because I write in hexadecimale in my files, but what is the solution without use flash memory ?

If you have an other solution to encrypt, I’m more than interrested.

Thank you in advance,

Sebastien