|
- require "import"
/ k& k0 h/ M/ ?! S8 Q8 }: G! ? - import "android.app.*"
( J8 z5 i3 g9 c9 E - import "android.os.*"
/ W- ]! N3 G. c; g - import "android.widget.*"
) q: a! i2 ~" ^! ?5 n - import "android.view.*"% B5 h$ E) z9 D- Q
- ) X" D* }) F& j
- activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题( v! Z: @* ~- u! b
- activity.setTitle("RC4加解蜜")8 ~: j6 @& k6 b# _& m, }
- % W5 r/ [, `- G9 J: O! h( F
- --Copyright© Ayaka_Ago. All Rights Reserved.7 S; |6 ?( Q$ M. r
- --加解密函数由 凌云(3116817790) 开源
' V5 t+ p! b- M: }3 d
! Z) v2 W5 r( W" S- local minicrypto = {}--RC4加解密算法(lua实现)
# b3 }3 U" P' t2 ^ - local insert, concat, modf, tostring,char = table.insert, table.concat, math.modf,tostring, string.char
/ T% s) b6 d9 ]' r$ ]2 K - local function numberToBinStr(x)
8 t8 K* k( ^2 c - local ret = {}3 X) u7 _! A6 U& c/ h% j. A* `5 l
- while x~=1 and x~=0 do
+ G$ }. m& n u3 K5 E - insert(ret, 1, x%2)3 X+ A3 G; ^. {: ]3 l! ?, t
- x=modf(x/2)1 w0 }. G p" F/ v* C# ^6 z4 ?
- end
1 {5 L z* R2 r& ?& n - insert(ret, 1, x)
5 d- g O" _. D$ @& m# W. {* @ - for i = 1, 8 - #ret do
5 ]1 K! s! l1 ]/ q5 n - insert(ret, 1, 0)
3 Q: o/ V9 e j9 ^ - end
* z2 y- j3 p3 f( W. [2 Q! S6 z0 x - return concat(ret)& h# Y& B5 T: d
- end4 f8 P! w- G7 P6 O
- local function computeBinaryKey(str)
6 H8 l2 e4 K2 U3 ] - local t = {}
4 U) A3 j+ H: m6 K8 z3 \! Q' j2 @ - for i = #str, 1, -1 do9 x/ O2 y8 x d# W0 d6 T, r
- insert(t, numberToBinStr(str:byte(i,i)))- t4 y1 j# j- Y3 P, e
- end; h$ [. y+ x. x7 j6 d
- return concat(t)
1 N. G# |1 I% v$ \ - end
7 ~: T# `. C1 Q; ]) M$ Z - local binaryKeys = setmetatable({}, {__mode = "k"})
4 G8 H) A4 U7 g$ w1 q& K- t( n - local function binaryKey(key)
: Z0 i( }. o& I. L. U1 o - local v = binaryKeys[key]
& C9 c; }; J+ |. C" y+ ]/ T - if v == nil then6 {) F8 V4 O1 r* Z
- v = computeBinaryKey(key)8 A7 U# I& `1 N* D( s& {0 x
- binaryKeys[key] = v
8 C- C1 K+ F5 n0 n( D - end: u" C/ E/ h2 T' ]
- return v
# t) g2 b1 z! ]6 d- ]% i - end5 ~7 A3 C* N: p8 b% O
- local function initialize_state(key)
" U( h2 T o t, D, c - local S = {}; for i = 0, 255 do S[i] = i end( E6 J# k4 N5 J4 j
- key = binaryKey(key)' g$ N8 d+ O9 ?; t# r9 |
- local j = 0% ^. P" `; r% K3 a& v: S6 u
- for i = 0, 255 do, A+ U& }, p! T% Z1 i. r
- local idx = (i % #key) +1
* G' [2 ]: E' @% f& i - j = (j + S[i] + tonumber(key:sub(idx, idx))) % 256
^, t4 \) s7 S* V* e6 Q - S[i], S[j] = S[j], S[i]
5 I, z* B. [ q) \* Z& V - end2 `- P+ x& [: e
- S.i = 0) ?) b& Z5 u2 @3 V
- S.j = 00 f6 F% w; ]( {9 ~! U
- return S- V' x2 Z5 M9 i$ v3 y% |
- end
6 `$ B* R7 v- R+ {$ |- V: x9 y - local function encrypt_one(state, byt)
3 G6 Q& Y& C- \4 f0 k - state.i = (state.i+1) % 256
. Y L" g: p% [* m( ^ - state.j = (state.j + state[state.i]) % 2564 c$ \. V% {/ X4 v- @0 H' a
- state[state.i], state[state.j] = state[state.j], state[state.i]3 a# S" Q( t0 H$ C! l
- local K = state[(state[state.i] + state[state.j]) % 256]
, k4 h5 ^6 ~) L% s) M - return K ~ byt3 R9 W4 J" R L
- end
" q0 c7 f U& j, T - function minicrypto.encrypt(text, key)
' d ? H, N& T - local state = initialize_state(key)6 C1 R8 Y& n' G$ i7 s
- local encrypted = {}3 t7 ^# O) x. y) X( p
- for i = 1, #text do
, T. B5 W- z0 f" [: u/ J: `0 T5 q - encrypted[i] = ("%02X"):format(encrypt_one(state, text:byte(i,i)))
8 ~' m- W; c, k, _0 X2 K1 i - end+ k" o5 Y) p' x
- return concat(encrypted)( V+ Q7 ^( c2 ^/ U
- end
, D' d$ o" }7 I+ v6 B& E/ I" \ - function minicrypto.decrypt(text, key)- f$ s2 t$ @4 |& m+ |. {
- local state = initialize_state(key)
5 U. j* h$ \7 Q# _8 C$ ~( a - local decrypted = {}
6 @/ q. `) d: i8 _ - for i = 1, #text, 2 do' ?: B2 ~& x+ ~
- insert(decrypted, char(encrypt_one(state, tonumber(text:sub(i, i+1), 16))))0 m! q8 G- Y# t2 O4 v, Y9 h
- end
a6 Z# D$ [; J1 d" V( g - return concat(decrypted)
( Z; d) K/ P; M* P# g3 P7 R- [/ E+ P - end
0 P. u" }9 G( F/ s7 a* Z! Q. x - * S; m& V2 N0 X# `7 M" N7 ]
- this.setContentView(loadlayout({1 G& k7 S4 O; n1 l9 v
- LinearLayout,
6 V( N4 ], a3 A" E3 j - orientation="vertical",
) D P# Y6 M9 J - {
) L r2 c" g0 B) l7 z& U5 D# } - TextView,1 B5 ]: J% o( [/ k, V
- text="RC4加解密",( H ^7 N" B+ ]6 P2 D
- layout_height="52dp",9 N$ Z! P$ a0 ]% l' y
- paddingLeft="18dp",9 v9 G. x4 P, V
- textSize="18dp",1 L9 R5 x) V7 r V$ |, x
- gravity="center",
! j: D' @1 N2 \0 _2 D - textColor=图标色,3 }* Z) J, w, Y6 M
- },
5 ]7 N) J( f0 S$ [# l4 Y" L( m - {' f! v, ?- z& q! X
- ScrollView,: K' {& n( i: @ i1 I x
- layout_width="fill",; h4 _2 F" V) n
- {& w( R) c- h5 n/ ?
- LinearLayout,
: ?+ S8 S S0 z - orientation="vertical",
! Q* {& f6 Z8 [! v - padding="18dp",& t; Z5 Q% ~" g6 r; E3 f( _2 d2 x2 f
- layout_width="fill",
. {) a; _7 _# r7 D4 z9 `' W - {. z+ ^$ R5 }+ R1 j( Y, N' d
- EditText,
, {% C, B" r) d$ N) v# @ - layout_marginBottom="18dp",
, K/ B* D U. Y- e) F; c - layout_width="fill",
3 l9 |: Q1 p) H - backgroundColor=0,
- w9 Q) W0 _' B% b# W6 Z - textSize="16dp",
3 C+ L6 F7 Q4 a - textColor=文字色,1 d4 y# [8 F) I4 q( z! |7 @) _
- id="from",
8 H( s' R; s2 |' {* l# t5 D - gravity="top",* E- e3 Q7 B, `+ _" {" u
- minLines=6,
; E; S" A, v. I& s - hint="输入要加密/解密的内容",
5 v8 \* ?2 n5 k' { - hintTextColor=次要文字色,
( _& l' z5 @8 |( n6 w; ^( \ - },
$ q0 Z, ?) a: \ - {: ]% B8 v) h; ^6 R
- CardView,: R3 w& W( V0 a% A
- radius="20dp",
' O' R7 G' Q! G# C1 x- d2 S - layout_width="fill",
3 H# Q% F, | p9 M s9 X$ { - backgroundColor=强调色,
% S+ o& g% v0 I2 |6 z - layout_marginBottom="18dp",1 f! K8 o' r2 H
- {" a) Q }/ J- S1 C8 l
- LinearLayout,- j3 |0 ~: i1 _- f" z
- layout_width="fill",4 e9 a5 N; k* _0 J. p, E1 H, `
- {# e; K8 P1 S, Z
- TextView," J5 l n$ o/ u9 P% h$ l$ l
- textColor="#009688",
5 I8 G* [) B' [3 P - text="解密",
9 Z M) Y; l3 {' {+ d - gravity="center",- p# `+ M' ~2 b
- layout_marginLeft="12dp",
. e$ e$ J1 M6 m$ \7 Z& a& O - layout_marginRight="12dp",
; u+ L( W) T0 G/ v - textSize="16dp",
& ^2 [* U) `- c' o1 h - layout_weight=1,
( f9 j% m' [" Y# b2 a4 a9 ]8 s - onClick=function()2 W! d& J* b. T( Q+ {1 [
- if pcall(function() resul.text=minicrypto.decrypt(from.text,pass.text) end) then else' R. x! k8 _$ S* j% w
- print("请输入密钥") end& V0 M! n4 k( X' T! Y/ O/ U* K
- end,5 T/ O/ {3 `# V& X
- --backgroundDrawable=波纹(0x40000000),; g: E0 R3 G% [/ s- F2 W4 Z* d
- }, _$ h5 g+ G9 ~, E
- {
5 s! I2 A+ U9 [2 ~( I1 D5 m - EditText,5 k" U+ [3 D7 L! u( c) ?
- layout_weight=1,
7 u* ^2 G, `9 A5 r7 A - backgroundColor=0,
& f. b; u% \1 c6 K9 m - textSize="16dp",
$ {1 P/ {7 S' M3 W7 o - gravity="center",
. b; x6 g1 T; y1 F( y! l, S - textColor="#66ccff",
, M( M$ L5 D# ]' k7 _ L - singleLine=true,
- V% B* w( d y7 X& {9 ?& V, Q$ H2 g - id="pass",5 M! b$ C7 [7 {5 M
- , U3 M! _# C# |4 N
- hint="输入密钥",4 e! k# C# R5 r) r0 Y6 T8 M# T
- hintTextColor="#009688"$ L2 _1 B+ E" E7 v; C8 H [
- --hintTextColor=Color.LTGRAY,
9 H P7 a; W1 z% G7 G - },* z, x! }/ `8 u
- {/ n$ z |4 N/ Z: U, q
- TextView,
; t6 s( Z" b2 V o2 y9 [ - textColor="#009688",
6 L( E. B. ]; f: ` - layout_marginLeft="12dp",* o& m W& P, N: a" L
- layout_marginRight="12dp",- Q- [! K/ u1 Z
- gravity="center",
# {& H6 g7 \( Q9 a4 M0 E - text="加密",* N! A$ e- Q& k# c5 @: ?1 R4 e
- textSize="16dp",( e( R- [9 ?( ^# L+ _
- onClick=function()% O7 S: \: S/ G7 B" b
- if pcall(function() resul.text=minicrypto.encrypt(from.text,pass.text) end) then else% M, M* [; @, y5 b
- toast("请输入密钥") end) I8 I+ D3 H. }( N1 o- [7 A, \4 R0 x
- end,
) d4 w5 r- W4 y. ]& p - --backgroundDrawable=波纹(0x40000000),
% l; k; F2 w/ v9 }/ g% p - layout_weight=1,
' f. F% j8 G; F/ x% ~ - },6 S- K: `$ ^8 ^, U, I& v9 I
- },
; E0 w2 n. X8 z' P4 _& f - },9 i5 j! W( q1 h4 t, P
- {6 ?" e* F- j# \3 w
- TextView,3 K5 S) F* ~+ w5 o8 P1 g' B8 l
- layout_width="fill",$ B- b$ Q; m/ ~, R
- textColor=文字色,
; p; T1 D2 w) N. Z" ? - text="加密/解密后的文本显示在此处",! a9 o, k+ h5 |
- textSize="16dp",
" L' j3 m/ e! Z7 X - padding="6dp",
7 {- k5 u8 X5 C+ |! D4 E8 ?+ e' m( ]; q - id="resul",
, Z9 V7 ?0 z0 L - textIsSelectable=true,: W. O! X+ T: U& V, ?
- },
) v+ n% f5 y8 {( E" j p - },! M3 f2 j1 l; O% r6 S3 W) t0 d
- },
5 J. q2 Z' W" o6 u - })), v. w* n) g/ v7 [/ H
复制代码 |
|