【lua教程】RC4加解密实现代码
require "import"import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题
activity.setTitle("RC4加解蜜")
--Copyright© Ayaka_Ago. All Rights Reserved.
--加解密函数由 凌云(3116817790) 开源
local minicrypto = {}--RC4加解密算法(lua实现)
local insert, concat, modf, tostring,char = table.insert, table.concat, math.modf,tostring, string.char
local function numberToBinStr(x)
local ret = {}
while x~=1 and x~=0 do
insert(ret, 1, x%2)
x=modf(x/2)
end
insert(ret, 1, x)
for i = 1, 8 - #ret do
insert(ret, 1, 0)
end
return concat(ret)
end
local function computeBinaryKey(str)
local t = {}
for i = #str, 1, -1 do
insert(t, numberToBinStr(str:byte(i,i)))
end
return concat(t)
end
local binaryKeys = setmetatable({}, {__mode = "k"})
local function binaryKey(key)
local v = binaryKeys
if v == nil then
v = computeBinaryKey(key)
binaryKeys = v
end
return v
end
local function initialize_state(key)
local S = {}; for i = 0, 255 do S = i end
key = binaryKey(key)
local j = 0
for i = 0, 255 do
local idx = (i % #key) +1
j = (j + S + tonumber(key:sub(idx, idx))) % 256
S, S = S, S
end
S.i = 0
S.j = 0
return S
end
local function encrypt_one(state, byt)
state.i = (state.i+1) % 256
state.j = (state.j + state) % 256
state, state = state, state
local K = state[(state + state) % 256]
return K ~ byt
end
function minicrypto.encrypt(text, key)
local state = initialize_state(key)
local encrypted = {}
for i = 1, #text do
encrypted = ("%02X"):format(encrypt_one(state, text:byte(i,i)))
end
return concat(encrypted)
end
function minicrypto.decrypt(text, key)
local state = initialize_state(key)
local decrypted = {}
for i = 1, #text, 2 do
insert(decrypted, char(encrypt_one(state, tonumber(text:sub(i, i+1), 16))))
end
return concat(decrypted)
end
this.setContentView(loadlayout({
LinearLayout,
orientation="vertical",
{
TextView,
text="RC4加解密",
layout_height="52dp",
paddingLeft="18dp",
textSize="18dp",
gravity="center",
textColor=图标色,
},
{
ScrollView,
layout_width="fill",
{
LinearLayout,
orientation="vertical",
padding="18dp",
layout_width="fill",
{
EditText,
layout_marginBottom="18dp",
layout_width="fill",
backgroundColor=0,
textSize="16dp",
textColor=文字色,
id="from",
gravity="top",
minLines=6,
hint="输入要加密/解密的内容",
hintTextColor=次要文字色,
},
{
CardView,
radius="20dp",
layout_width="fill",
backgroundColor=强调色,
layout_marginBottom="18dp",
{
LinearLayout,
layout_width="fill",
{
TextView,
textColor="#009688",
text="解密",
gravity="center",
layout_marginLeft="12dp",
layout_marginRight="12dp",
textSize="16dp",
layout_weight=1,
onClick=function()
if pcall(function() resul.text=minicrypto.decrypt(from.text,pass.text) end) then else
print("请输入密钥") end
end,
--backgroundDrawable=波纹(0x40000000),
},
{
EditText,
layout_weight=1,
backgroundColor=0,
textSize="16dp",
gravity="center",
textColor="#66ccff",
singleLine=true,
id="pass",
hint="输入密钥",
hintTextColor="#009688"
--hintTextColor=Color.LTGRAY,
},
{
TextView,
textColor="#009688",
layout_marginLeft="12dp",
layout_marginRight="12dp",
gravity="center",
text="加密",
textSize="16dp",
onClick=function()
if pcall(function() resul.text=minicrypto.encrypt(from.text,pass.text) end) then else
toast("请输入密钥") end
end,
--backgroundDrawable=波纹(0x40000000),
layout_weight=1,
},
},
},
{
TextView,
layout_width="fill",
textColor=文字色,
text="加密/解密后的文本显示在此处",
textSize="16dp",
padding="6dp",
id="resul",
textIsSelectable=true,
},
},
},
}))
页:
[1]