FIX: I think the dumping of data is correct now in tnp3.lua. MD5 string vs bytearrays in lua are tricky

ADD: utils.lua some functions to convert between ascii, bytes and strings.
This commit is contained in:
iceman1001 2014-11-09 19:29:47 +01:00
commit cd5767d43d
2 changed files with 46 additions and 25 deletions

View file

@ -46,21 +46,46 @@ local Utils =
end, end,
--- ---
-- Convert Byte array to string of hex -- Convert Byte array to string of hex
ConvertBytes2String = function(bytes) ConvertBytes2HexString = function(bytes)
if #bytes == 0 then
return ''
end
local s={} local s={}
for i = 1, #(bytes) do for i = 1, #(bytes) do
s[i] = string.format("%02X",bytes[i]) s[i] = string.format("%02X",bytes[i])
end end
return table.concat(s) return table.concat(s)
end, end,
-- Convert byte array to string with ascii
ConvertStringToBytes = function(s) ConvertBytesToAsciiString = function(bytes)
if #bytes == 0 then
return ''
end
local s={}
for i = 1, #(bytes) do
s[i] = string.char(bytes[i])
end
return table.concat(s)
end,
ConvertHexStringToBytes = function(s)
local t={} local t={}
if s == nil then return t end
if #s == 0 then return t end
for k in s:gmatch"(%x%x)" do for k in s:gmatch"(%x%x)" do
table.insert(t,tonumber(k,16)) table.insert(t,tonumber(k,16))
end end
return t return t
end, end,
ConvertAsciiStringToBytes = function(s)
local t={}
if s == nil then return t end
if #s == 0 then return t end
for k in s:gmatch"(.)" do
table.insert(t, string.byte(k))
end
return t
end,
-- function convertStringToBytes(str) -- function convertStringToBytes(str)
-- local bytes = {} -- local bytes = {}

View file

@ -93,7 +93,7 @@ end
local function main(args) local function main(args)
print( string.rep('--',20) ) print( string.rep('--',20) )
--print( string.rep('--',20) ) print( string.rep('--',20) )
--print() --print()
local keyA local keyA
@ -187,35 +187,30 @@ local function main(args)
local blockdata, err = waitCmd() local blockdata, err = waitCmd()
if err then return oops(err) end if err then return oops(err) end
local b = blockNo%4 if blockNo%4 ~= 3 then
if b ~= 3 then
if blockNo < 8 then if blockNo < 8 then
-- Block 0-7 not encrypted -- Block 0-7 not encrypted
blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata) blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
else else
local base = ('%s%s%d%s'):format(block0, block1, blockNo, hashconstant) local md5hash = md5.sumhexa(base) local base = ('%s%s%02d%s'):format(block0, block1, blockNo, hashconstant)
local baseArr = utils.ConvertHexStringToBytes(base)
local baseStr = utils.ConvertBytesToAsciiString(baseArr)
local md5hash = md5.sumhexa(baseStr)
local aestest = core.aes(md5hash, blockdata) local aestest = core.aes(md5hash, blockdata)
--print(aestest, type(aestest))
local _,hex = bin.unpack(("H%d"):format(16),aestest) local hex = utils.ConvertAsciiStringToBytes(aestest)
hex = utils.ConvertBytes2HexString(hex)
-- local hexascii = string.gsub(hex, '(%x%x)', --local _,hex = bin.unpack(("H%d"):format(16),aestest)
-- function(value)
-- return string.char(tonumber(value, 16))
-- end
-- )
if string.find(blockdata, '^0+$') then if string.find(blockdata, '^0+$') then
blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata) blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
else else
--blocks[blockNo+1] = ('%02d :: %s :: %s :: %s '):format(blockNo,key,md5hash,hex) blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,hex)
blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,hex)
end end
end end
else else
-- Sectorblocks, not encrypted -- Sectorblocks, not encrypted
blocks[blockNo+1] = ('%02d :: %s :: %s'):format(blockNo,blockdata,blockdata) blocks[blockNo+1] = ('%02d :: %s'):format(blockNo,blockdata)
end end
end end
@ -226,10 +221,11 @@ local function main(args)
print( (' UID : %s'):format(uid) ) print( (' UID : %s'):format(uid) )
print( (' ITEM TYPE : %s - %s'):format(itemtype, toyNames[itemtype]) ) print( (' ITEM TYPE : %s - %s'):format(itemtype, toyNames[itemtype]) )
print( (' CARDID : %s'):format(cardid ) ) print( (' CARDID : %s'):format(cardid ) )
print('BLK :: DATA DECRYPTED' ) print('BLK :: Decrypted Ascii' )
print( string.rep('--',36) ) print( string.rep('--',36) )
for _,s in pairs(blocks) do for _,s in pairs(blocks) do
print( s ) local arr = utils.ConvertHexStringToBytes(s:sub(7,#s))
print( s, utils.ConvertBytesToAsciiString(arr) )
end end
end end