分享

lua简单地异或加密文件

 tology 2020-02-12

用lua简单地异或加密文件,注意解密的key是加密key的倒序:

1 require bit 2 3 local encode = function(inpath, outpath, key) 4 local inf = assert(io.open(inpath, 'rb')) 5 local outf = assert(io.open(outpath, 'wb')) 6 7 if (type(key) ~= 'string') or (string.len(key) == 0) then 8 key = 'x' 9 end10 11 local temp = nil12 local data = inf:read(1)13 while data do14 temp = bit.bxor(string.byte(data), string.byte(string.sub(key, 1, 1)))15 for i = 2, string.len(key) do16 temp = bit.bxor(temp, string.byte(string.sub(key, i, i)))17 end18 outf:write(string.char(temp))19 data = inf:read(1)20 end21 22 assert(inf:close())23 assert(outf:close())24 end25 26 local decode = function(inf, outf, key)27 encode(inf, outf, key)28 end29 30 ------------------------------------------------31 -- interface32 --33 transform_xor = {34 en = encode,35 de = decode,36 }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约