码迷,mamicode.com
首页 > 其他好文 > 详细

lua简单地异或加密文件

时间:2014-08-23 20:15:51      阅读:5713      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   文件   for   ar   div   

用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     end
10 
11     local temp = nil
12     local data = inf:read(1)
13     while data do
14         temp = bit.bxor(string.byte(data), string.byte(string.sub(key, 1, 1)))
15         for i = 2, string.len(key) do
16             temp = bit.bxor(temp, string.byte(string.sub(key, i, i)))
17         end
18         outf:write(string.char(temp))
19         data = inf:read(1)
20     end
21 
22     assert(inf:close())
23     assert(outf:close())
24 end
25 
26 local decode = function(inf, outf, key)
27     encode(inf, outf, key)
28 end
29 
30 ------------------------------------------------
31 -- interface
32 --
33 transform_xor = {
34     en = encode,
35     de = decode,
36 }

 

lua简单地异或加密文件

标签:style   blog   color   os   io   文件   for   ar   div   

原文地址:http://www.cnblogs.com/jiufangding/p/3931637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!