登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 861|回复: 0

[lua教程] 【lua教程】RC4加解密实现代码

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-6-30 23:16:48 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. require "import"
    / k& k0 h/ M/ ?! S8 Q8 }: G! ?
  2. import "android.app.*"
    ( J8 z5 i3 g9 c9 E
  3. import "android.os.*"
    / W- ]! N3 G. c; g
  4. import "android.widget.*"
    ) q: a! i2 ~" ^! ?5 n
  5. import "android.view.*"% B5 h$ E) z9 D- Q
  6. ) X" D* }) F& j
  7. activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题( v! Z: @* ~- u! b
  8. activity.setTitle("RC4加解蜜")8 ~: j6 @& k6 b# _& m, }
  9. % W5 r/ [, `- G9 J: O! h( F
  10. --Copyright© Ayaka_Ago. All Rights Reserved.7 S; |6 ?( Q$ M. r
  11. --加解密函数由 凌云(3116817790) 开源
    ' V5 t+ p! b- M: }3 d

  12. ! Z) v2 W5 r( W" S
  13. local minicrypto = {}--RC4加解密算法(lua实现)
    # b3 }3 U" P' t2 ^
  14. local insert, concat, modf, tostring,char = table.insert, table.concat, math.modf,tostring, string.char
    / T% s) b6 d9 ]' r$ ]2 K
  15. local function numberToBinStr(x)
    8 t8 K* k( ^2 c
  16.   local ret = {}3 X) u7 _! A6 U& c/ h% j. A* `5 l
  17.   while x~=1 and x~=0 do
    + G$ }. m& n  u3 K5 E
  18.     insert(ret, 1, x%2)3 X+ A3 G; ^. {: ]3 l! ?, t
  19.     x=modf(x/2)1 w0 }. G  p" F/ v* C# ^6 z4 ?
  20.   end
    1 {5 L  z* R2 r& ?& n
  21.   insert(ret, 1, x)
    5 d- g  O" _. D$ @& m# W. {* @
  22.   for i = 1, 8 - #ret do
    5 ]1 K! s! l1 ]/ q5 n
  23.     insert(ret, 1, 0)
    3 Q: o/ V9 e  j9 ^
  24.   end
    * z2 y- j3 p3 f( W. [2 Q! S6 z0 x
  25.   return concat(ret)& h# Y& B5 T: d
  26. end4 f8 P! w- G7 P6 O
  27. local function computeBinaryKey(str)
    6 H8 l2 e4 K2 U3 ]
  28.   local t = {}
    4 U) A3 j+ H: m6 K8 z3 \! Q' j2 @
  29.   for i = #str, 1, -1 do9 x/ O2 y8 x  d# W0 d6 T, r
  30.     insert(t, numberToBinStr(str:byte(i,i)))- t4 y1 j# j- Y3 P, e
  31.   end; h$ [. y+ x. x7 j6 d
  32.   return concat(t)
    1 N. G# |1 I% v$ \
  33. end
    7 ~: T# `. C1 Q; ]) M$ Z
  34. local binaryKeys = setmetatable({}, {__mode = "k"})
    4 G8 H) A4 U7 g$ w1 q& K- t( n
  35. local function binaryKey(key)
    : Z0 i( }. o& I. L. U1 o
  36.   local v = binaryKeys[key]
    & C9 c; }; J+ |. C" y+ ]/ T
  37.   if v == nil then6 {) F8 V4 O1 r* Z
  38.     v = computeBinaryKey(key)8 A7 U# I& `1 N* D( s& {0 x
  39.     binaryKeys[key] = v
    8 C- C1 K+ F5 n0 n( D
  40.   end: u" C/ E/ h2 T' ]
  41.   return v
    # t) g2 b1 z! ]6 d- ]% i
  42. end5 ~7 A3 C* N: p8 b% O
  43. local function initialize_state(key)
    " U( h2 T  o  t, D, c
  44.   local S = {}; for i = 0, 255 do S[i] = i end( E6 J# k4 N5 J4 j
  45.   key = binaryKey(key)' g$ N8 d+ O9 ?; t# r9 |
  46.   local j = 0% ^. P" `; r% K3 a& v: S6 u
  47.   for i = 0, 255 do, A+ U& }, p! T% Z1 i. r
  48.     local idx = (i % #key) +1
    * G' [2 ]: E' @% f& i
  49.     j = (j + S[i] + tonumber(key:sub(idx, idx))) % 256
      ^, t4 \) s7 S* V* e6 Q
  50.     S[i], S[j] = S[j], S[i]
    5 I, z* B. [  q) \* Z& V
  51.   end2 `- P+ x& [: e
  52.   S.i = 0) ?) b& Z5 u2 @3 V
  53.   S.j = 00 f6 F% w; ]( {9 ~! U
  54.   return S- V' x2 Z5 M9 i$ v3 y% |
  55. end
    6 `$ B* R7 v- R+ {$ |- V: x9 y
  56. local function encrypt_one(state, byt)
    3 G6 Q& Y& C- \4 f0 k
  57.   state.i = (state.i+1) % 256
    . Y  L" g: p% [* m( ^
  58.   state.j = (state.j + state[state.i]) % 2564 c$ \. V% {/ X4 v- @0 H' a
  59.   state[state.i], state[state.j] = state[state.j], state[state.i]3 a# S" Q( t0 H$ C! l
  60.   local K = state[(state[state.i] + state[state.j]) % 256]
    , k4 h5 ^6 ~) L% s) M
  61.   return K ~ byt3 R9 W4 J" R  L
  62. end
    " q0 c7 f  U& j, T
  63. function minicrypto.encrypt(text, key)
    ' d  ?  H, N& T
  64.   local state = initialize_state(key)6 C1 R8 Y& n' G$ i7 s
  65.   local encrypted = {}3 t7 ^# O) x. y) X( p
  66.   for i = 1, #text do
    , T. B5 W- z0 f" [: u/ J: `0 T5 q
  67.     encrypted[i] = ("%02X"):format(encrypt_one(state, text:byte(i,i)))
    8 ~' m- W; c, k, _0 X2 K1 i
  68.   end+ k" o5 Y) p' x
  69.   return concat(encrypted)( V+ Q7 ^( c2 ^/ U
  70. end
    , D' d$ o" }7 I+ v6 B& E/ I" \
  71. function minicrypto.decrypt(text, key)- f$ s2 t$ @4 |& m+ |. {
  72.   local state = initialize_state(key)
    5 U. j* h$ \7 Q# _8 C$ ~( a
  73.   local decrypted = {}
    6 @/ q. `) d: i8 _
  74.   for i = 1, #text, 2 do' ?: B2 ~& x+ ~
  75.     insert(decrypted, char(encrypt_one(state, tonumber(text:sub(i, i+1), 16))))0 m! q8 G- Y# t2 O4 v, Y9 h
  76.   end
      a6 Z# D$ [; J1 d" V( g
  77.   return concat(decrypted)
    ( Z; d) K/ P; M* P# g3 P7 R- [/ E+ P
  78. end
    0 P. u" }9 G( F/ s7 a* Z! Q. x
  79. * S; m& V2 N0 X# `7 M" N7 ]
  80. this.setContentView(loadlayout({1 G& k7 S4 O; n1 l9 v
  81.   LinearLayout,
    6 V( N4 ], a3 A" E3 j
  82.   orientation="vertical",
    ) D  P# Y6 M9 J
  83.   {
    ) L  r2 c" g0 B) l7 z& U5 D# }
  84.     TextView,1 B5 ]: J% o( [/ k, V
  85.     text="RC4加解密",( H  ^7 N" B+ ]6 P2 D
  86.     layout_height="52dp",9 N$ Z! P$ a0 ]% l' y
  87.     paddingLeft="18dp",9 v9 G. x4 P, V
  88.     textSize="18dp",1 L9 R5 x) V7 r  V$ |, x
  89.     gravity="center",
    ! j: D' @1 N2 \0 _2 D
  90.     textColor=图标色,3 }* Z) J, w, Y6 M
  91.   },
    5 ]7 N) J( f0 S$ [# l4 Y" L( m
  92.   {' f! v, ?- z& q! X
  93.     ScrollView,: K' {& n( i: @  i1 I  x
  94.     layout_width="fill",; h4 _2 F" V) n
  95.     {& w( R) c- h5 n/ ?
  96.       LinearLayout,
    : ?+ S8 S  S0 z
  97.       orientation="vertical",
    ! Q* {& f6 Z8 [! v
  98.       padding="18dp",& t; Z5 Q% ~" g6 r; E3 f( _2 d2 x2 f
  99.       layout_width="fill",
    . {) a; _7 _# r7 D4 z9 `' W
  100.       {. z+ ^$ R5 }+ R1 j( Y, N' d
  101.         EditText,
    , {% C, B" r) d$ N) v# @
  102.         layout_marginBottom="18dp",
    , K/ B* D  U. Y- e) F; c
  103.         layout_width="fill",
    3 l9 |: Q1 p) H
  104.         backgroundColor=0,
    - w9 Q) W0 _' B% b# W6 Z
  105.         textSize="16dp",
    3 C+ L6 F7 Q4 a
  106.         textColor=文字色,1 d4 y# [8 F) I4 q( z! |7 @) _
  107.         id="from",
    8 H( s' R; s2 |' {* l# t5 D
  108.         gravity="top",* E- e3 Q7 B, `+ _" {" u
  109.         minLines=6,
    ; E; S" A, v. I& s
  110.         hint="输入要加密/解密的内容",
    5 v8 \* ?2 n5 k' {
  111.         hintTextColor=次要文字色,
    ( _& l' z5 @8 |( n6 w; ^( \
  112.       },
    $ q0 Z, ?) a: \
  113.       {: ]% B8 v) h; ^6 R
  114.         CardView,: R3 w& W( V0 a% A
  115.         radius="20dp",
    ' O' R7 G' Q! G# C1 x- d2 S
  116.         layout_width="fill",
    3 H# Q% F, |  p9 M  s9 X$ {
  117.         backgroundColor=强调色,
    % S+ o& g% v0 I2 |6 z
  118.         layout_marginBottom="18dp",1 f! K8 o' r2 H
  119.         {" a) Q  }/ J- S1 C8 l
  120.           LinearLayout,- j3 |0 ~: i1 _- f" z
  121.           layout_width="fill",4 e9 a5 N; k* _0 J. p, E1 H, `
  122.           {# e; K8 P1 S, Z
  123.             TextView," J5 l  n$ o/ u9 P% h$ l$ l
  124.             textColor="#009688",
    5 I8 G* [) B' [3 P
  125.             text="解密",
    9 Z  M) Y; l3 {' {+ d
  126.             gravity="center",- p# `+ M' ~2 b
  127.             layout_marginLeft="12dp",
    . e$ e$ J1 M6 m$ \7 Z& a& O
  128.             layout_marginRight="12dp",
    ; u+ L( W) T0 G/ v
  129.             textSize="16dp",
    & ^2 [* U) `- c' o1 h
  130.             layout_weight=1,
    ( f9 j% m' [" Y# b2 a4 a9 ]8 s
  131.             onClick=function()2 W! d& J* b. T( Q+ {1 [
  132.               if pcall(function() resul.text=minicrypto.decrypt(from.text,pass.text) end) then else' R. x! k8 _$ S* j% w
  133.                 print("请输入密钥") end& V0 M! n4 k( X' T! Y/ O/ U* K
  134.             end,5 T/ O/ {3 `# V& X
  135.             --backgroundDrawable=波纹(0x40000000),; g: E0 R3 G% [/ s- F2 W4 Z* d
  136.           },  _$ h5 g+ G9 ~, E
  137.           {
    5 s! I2 A+ U9 [2 ~( I1 D5 m
  138.             EditText,5 k" U+ [3 D7 L! u( c) ?
  139.             layout_weight=1,
    7 u* ^2 G, `9 A5 r7 A
  140.             backgroundColor=0,
    & f. b; u% \1 c6 K9 m
  141.             textSize="16dp",
    $ {1 P/ {7 S' M3 W7 o
  142.             gravity="center",
    . b; x6 g1 T; y1 F( y! l, S
  143.             textColor="#66ccff",
    , M( M$ L5 D# ]' k7 _  L
  144.             singleLine=true,
    - V% B* w( d  y7 X& {9 ?& V, Q$ H2 g
  145.             id="pass",5 M! b$ C7 [7 {5 M
  146. , U3 M! _# C# |4 N
  147.             hint="输入密钥",4 e! k# C# R5 r) r0 Y6 T8 M# T
  148.             hintTextColor="#009688"$ L2 _1 B+ E" E7 v; C8 H  [
  149.             --hintTextColor=Color.LTGRAY,
    9 H  P7 a; W1 z% G7 G
  150.           },* z, x! }/ `8 u
  151.           {/ n$ z  |4 N/ Z: U, q
  152.             TextView,
    ; t6 s( Z" b2 V  o2 y9 [
  153.             textColor="#009688",
    6 L( E. B. ]; f: `
  154.             layout_marginLeft="12dp",* o& m  W& P, N: a" L
  155.             layout_marginRight="12dp",- Q- [! K/ u1 Z
  156.             gravity="center",
    # {& H6 g7 \( Q9 a4 M0 E
  157.             text="加密",* N! A$ e- Q& k# c5 @: ?1 R4 e
  158.             textSize="16dp",( e( R- [9 ?( ^# L+ _
  159.             onClick=function()% O7 S: \: S/ G7 B" b
  160.               if pcall(function() resul.text=minicrypto.encrypt(from.text,pass.text) end) then else% M, M* [; @, y5 b
  161.                 toast("请输入密钥") end) I8 I+ D3 H. }( N1 o- [7 A, \4 R0 x
  162.             end,
    ) d4 w5 r- W4 y. ]& p
  163.             --backgroundDrawable=波纹(0x40000000),
    % l; k; F2 w/ v9 }/ g% p
  164.             layout_weight=1,
    ' f. F% j8 G; F/ x% ~
  165.           },6 S- K: `$ ^8 ^, U, I& v9 I
  166.         },
    ; E0 w2 n. X8 z' P4 _& f
  167.       },9 i5 j! W( q1 h4 t, P
  168.       {6 ?" e* F- j# \3 w
  169.         TextView,3 K5 S) F* ~+ w5 o8 P1 g' B8 l
  170.         layout_width="fill",$ B- b$ Q; m/ ~, R
  171.         textColor=文字色,
    ; p; T1 D2 w) N. Z" ?
  172.         text="加密/解密后的文本显示在此处",! a9 o, k+ h5 |
  173.         textSize="16dp",
    " L' j3 m/ e! Z7 X
  174.         padding="6dp",
    7 {- k5 u8 X5 C+ |! D4 E8 ?+ e' m( ]; q
  175.         id="resul",
    , Z9 V7 ?0 z0 L
  176.         textIsSelectable=true,: W. O! X+ T: U& V, ?
  177.       },
    ) v+ n% f5 y8 {( E" j  p
  178.     },! M3 f2 j1 l; O% r6 S3 W) t0 d
  179.   },
    5 J. q2 Z' W" o6 u
  180. })), v. w* n) g/ v7 [/ H
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|星空社区 |网站地图

GMT+8, 2024-4-28 13:43 , Processed in 0.053342 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表