Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aes-ecb-128 #42

Open
1770447014 opened this issue Jan 15, 2023 · 1 comment
Open

aes-ecb-128 #42

1770447014 opened this issue Jan 15, 2023 · 1 comment

Comments

@1770447014
Copy link

lockbox/padding/pkcs7.lua

openssl:
str = abcabcabcqweqweq1234567890123456
key = abcabcabcqweqweq

aes:
<\�V�m�=�V�P�_㧼3��> yj���9�:�z5��rF�|92 ��2V

base64:
PFyEVsRtlT2wVqJQpV/jp7wzp/U+DHlq4g/zOc86zno1AP26cgBG33w5Mg3nEzJW

lockbox:
str = abcabcabcqweqweq1234567890123456
key = abcabcabcqweqweq

aes:
<\�V�m�=�V�P�_㧼3��> yj���9�:�z

base64:
PFyEVsRtlT2wVqJQpV/jp7wzp/U+DHlq4g/zOc86zno=

16的整数不匹配,补位正常,只有整数异常

@1770447014
Copy link
Author

base64 = {}

local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -- You will need this for encoding/decoding

-- encoding
function base64.enc(data)
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end

-- decoding
function base64.dec(data)
data = string.gsub(data, '[^'..b..'=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r,f='',(b:find(x)-1)
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
return r;
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
if (#x ~= 8) then return '' end
local c=0
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
return string.char(c)
end))
end

local String = require("string");

local Array = require("lockbox.util.array");
local Stream = require("lockbox.util.stream");

local ECBMode = require("lockbox.cipher.mode.ecb");
local CBCMode = require("lockbox.cipher.mode.cbc");
local CFBMode = require("lockbox.cipher.mode.cfb");
local OFBMode = require("lockbox.cipher.mode.ofb");
local CTRMode = require("lockbox.cipher.mode.ctr");
local IGEMode = require("lockbox.cipher.mode.ige");

local ZeroPadding = require("lockbox.padding.zero");
local Pkcs7Padding = require("lockbox.padding.pkcs7");

local AES128Cipher = require("lockbox.cipher.aes128");

function string.tohex(str)
return str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end)
end

function string.fromhex(str)
return str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end)
end

function aes_encode(str, key)

local Index = string.len(str) / 16
local a , b = math.modf(Index);

if b == 0 then
str = str .. "����������������"
end

local cipher = ECBMode.Cipher()
.setKey(Array.fromHex(string.tohex(key)))
.setBlockCipher(AES128Cipher)
.setPadding(Pkcs7Padding);

local cipherOutput = cipher
.init()
.update(Stream.fromArray(Array.fromHex("")))
.update(Stream.fromArray(Array.fromHex(string.tohex(str))))
.finish()
.asHex();

print(cipherOutput)
print(string.fromhex(cipherOutput))
print(base64.enc(string.fromhex(cipherOutput)))
end

aes_encode("abcabcabcqweqweq", "abcabcabcqweqweq")

--[[
local decipher = ECBMode.Decipher()
.setKey(Array.fromHex(string.tohex("abcabcabcqweqweq")))
.setBlockCipher(AES128Cipher)
.setPadding(Pkcs7Padding);

local plainOutput = decipher
.init()
.update(Stream.fromArray(Array.fromHex("")))
.update(Stream.fromArray(Array.fromHex("3C5C8456C46D953DB056A250A55FE3A73500FDBA720046DF7C39320DE7133256")))
.finish()
.asHex();
print(plainOutput)
print(string.fromhex(plainOutput))
--]]

--[[
<\�V�m�=�V�P�_�5��rF�|92 ��2V
PFyEVsRtlT2wVqJQpV/jpzUA/bpyAEbffDkyDecTMlY=

<\�V�m�=�V�P�_� 16
PFyEVsRtlT2wVqJQpV/jpw==

<\�V�m�=�V�P�_�5���r�F�|92
��2V 32
PFyEVsRtlT2wVqJQpV/jpzUA/bpyAEbffDkyDecTMlY=
--]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant