Lua simply excludes or encrypts files

Source: Internet
Author: User

Use Lua to simply encrypt or decrypt files. Note that the decrypted key is the reverse order of the encrypted 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 }

 

Lua simply excludes or encrypts files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.