Encryption 使用共享密钥的Lua加密

Encryption 使用共享密钥的Lua加密,encryption,lua,key,base64,Encryption,Lua,Key,Base64,我一直在使用这个开源函数通过base64方法对字符串进行加密和解密,我想知道是否有一种方法可以让我和一些朋友共享一个特定的“密钥”,从而使它能够以只有拥有这个“密钥”的人才能正确加密或解密消息的方式工作 -- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de> -- licensed under the terms of the LGPL2 -- character table string local

我一直在使用这个开源函数通过base64方法对字符串进行加密和解密,我想知道是否有一种方法可以让我和一些朋友共享一个特定的“密钥”,从而使它能够以只有拥有这个“密钥”的人才能正确加密或解密消息的方式工作

-- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de>
-- licensed under the terms of the LGPL2

-- character table string
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

-- encoding
function 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 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
Alex Kloss的Lua 5.1+base64 v3.0(c)2009 --根据LGPL2的条款获得许可 --字符表字符串 本地b='ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789+/' --编码 功能enc(数据) 返回((数据:gsub('.',函数(x) 局部r,b='',x:byte() 对于i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0和“1”或“0”)结束 返回r; 结束)“'0000'”:gsub(“%d%d%d?%d?%d?%d?”,函数(x) 如果(#x<6),则返回“”结束 局部c=0 对于i=1,6doc=c+(x:sub(i,i)='1'和2^(6-i)或0)结束 返回b:sub(c+1,c+1) 结束)…({“”、'='、'='})[#数据%3+1]) 结束 --解码 功能dec(数据) data=string.gsub(数据,'[^'..b..'=]','') 返回(数据:gsub('.',函数(x) 如果(x=='='),则返回''end' 局部r,f='',(b:find(x)-1) 对于i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0和“1”或“0”)结束 返回r; 结束):gsub(“%d%d%d?%d?%d?%d?%d?%d?”,函数(x) 如果(#x~=8),则返回“”结束 局部c=0 对于i=1,8doc=c+(x:sub(i,i)='1'和2^(8-i)或0)结束 返回字符串.char(c) 完) 结束
所以,假设我和三个朋友得到了一个类似的函数,我们都有一个名为“flibble”的私有字符串密钥。。。我们如何才能共享他人无法识别的消息?

不,不能与base 64共享。Base 64不是加密,而是编码。Base 64不接受键作为参数,它只接受二进制并将其转换为可打印的ASCII

当然,有一些技巧可以让base 64看起来更像密文:只需输入一个拥挤的字母表(在变量
b
中)。然而,这是常见的替代;因此,应该将其视为模糊处理而不是加密。我可以向一个随机的高中生解释如何破解它

通常,您需要首先使用分组密码+操作模式进行加密,然后执行编码。您将需要AES之类的东西来确保消息的机密性,而HMAC之类的东西来确保消息的完整性和真实性

-- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de>
-- licensed under the terms of the LGPL2

-- character table string
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

-- encoding
function 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 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
我想推荐类似的东西。您确实不希望仅出于性能原因而使用高级语言(如Lua)执行加密。许多Lua库只提供AES或HMAC,但不能同时提供两者,而且许多似乎是单人项目,而不是支持/维护良好的库,因此请仔细选择

本地a=[[ 你的剧本在这里 ]]

复制粘贴到此处Base64编码和解码

对于编码=打印(enc(a))
对于decode=print(dec(a))

Base64不是一种加密算法。它用于将字节序列转换为可打印的ASCII字符。它不需要钥匙。