登录  | 立即注册

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

查看: 2847|回复: 0

【Java】元宵节

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-6-30 22:18:45 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. import java.io.UnsupportedEncodingException;
    ; \) o, U+ h! d- l" P% O6 c0 r
  2. import java.nio.charset.StandardCharsets;+ X, h( y) y/ l2 J9 @. g
  3. import java.net.URL;& L" U  I2 o, ?& x9 P# b
  4. import java.io.BufferedReader;) y; Y/ P4 E0 |
  5. import java.io.InputStreamReader;
    * v9 x, Q( ?$ _7 A9 E
  6. public class Main+ M- j" Y% T- J8 Q8 t2 Z
  7. {
    ' z4 w0 C  _% Z. Q( u
  8.         public static void main(String[] args)
    8 l& t8 Q: }7 F; b% n
  9.         {7 x( J) b0 L# x+ l1 H- v
  10.                 Main t6=new Main();$ @- I8 L' T4 F, z2 R! s' q' Y& S
  11.                 String htmls= t6.getPageSource("http://bmob-cdn-11629.b0.upaiyun.com/2019/02/19/1483728040e51b2c80b9ddb4ad3c6ced.txt","utf8");  % |0 L: P, }4 w2 |# R: h5 c! F
  12.                         String decryStr = RC4Util.decryRC4(htmls, "happy");# Z3 Y) X& ]7 X
  13.                 System.out.println(decryStr);
    3 C8 X, w7 C9 Z* P7 P( F6 t
  14.                         }
    4 A3 Q- R  T7 i! ^, m
  15.         public String getPageSource(String pageUrl,String encoding) {    , }/ U- X! L& [
  16.                 StringBuffer sb = new StringBuffer();   
    ! O( _7 @  d2 `( B. J
  17.                 try {   
    7 @) h; r+ H8 O5 H) k6 W
  18.                         URL url = new URL(pageUrl);    # O6 i$ S4 f: s, s3 e# o8 e
  19.                         BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), encoding));   
    ) F0 B1 _; a1 z# F& G9 |
  20.                         String line;   
    8 \' u6 E# |' b% K8 g
  21.                         while((line = in.readLine())!=null){
    + {! R% ~; X$ x, e9 a
  22.                                 sb.append(line);4 Q5 P' X; N% d5 u2 |. K- V
  23.                         }in.close();}
    ! w- J2 V" H0 ^' @* Z
  24.                 catch (Exception ex) {   
    . ^. K5 m  H; {. Y1 b- c
  25.                         System.err.println(ex);    ) p8 `. T4 I- d9 m; V4 G
  26.                 }    4 P; [9 L8 t' G* |
  27.                 return sb.toString();   
    9 a1 F) G3 k) f& p$ e
  28.         }   
    8 `% f) i) ?3 Y. ^
  29. }
    9 w* ]7 [" D/ Q" {4 @
  30. class RC4Util {
    % y& U. @. L+ i! h( O; }# ?
  31.         public static String decryRC4(String data, String key) {
    9 Q; J# Y9 q% w% C8 M+ d  T
  32.                 if (data == null || key == null) {
    , f; ?+ U  u( d
  33.                         return null;
    8 w* @4 n  v* N& O% K
  34.                 }2 K( R6 P( B( b2 @2 c' ^
  35.                 return new String(RC4Base(HexString2Bytes(data), key), StandardCharsets.UTF_8);
    ) ]: s3 p8 o" @  W3 l! P: x7 z* U
  36.         }3 v9 r+ S- H+ r  y5 z1 ~7 j
  37.         private static byte[] initKey(String aKey) {" k7 y& ?0 H4 A0 O
  38.                 byte[] bkey = aKey.getBytes();% ?# o# X# L8 d
  39.                 byte state[] = new byte[256];* H. U( ?( d+ `/ |3 C. A' @

  40. 4 ]3 R  g* T$ P+ |: M! q. W0 o! L
  41.                 for (int i = 0; i < 256; i++) {# f5 _% r3 I; l% }/ g
  42.                         state[i] = (byte) i;& H* i0 o0 V; k+ W# `
  43.                 }
    0 }3 d' @6 t5 d: P$ p- j
  44.                 int index1 = 0;; a, P8 T- c' X0 ]5 C2 v; |. w
  45.                 int index2 = 0;
    ! K# q+ `3 s" t" T, |$ ]" Y; i
  46.                 if (bkey.length == 0) {
    8 x: u* A  T, E) G- w
  47.                         return null;  q( s* `4 |+ D5 w" L& A- x6 b
  48.                 }/ X! p1 [3 t4 l* ~+ W* r2 Q
  49.                 for (int i = 0; i < 256; i++) {
    - Z" ~( h  |6 E8 I( n
  50.                         index2 = ((bkey[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
    7 q7 Z4 y# p! P" Z, O: {: l8 ^5 ~
  51.                         byte tmp = state[i];6 b* S5 V% c+ B) i" q+ |* K! v
  52.                         state[i] = state[index2];) ^0 z, o5 c6 d% O8 q
  53.                         state[index2] = tmp;
    9 s$ M4 n* ?1 i
  54.                         index1 = (index1 + 1) % bkey.length;! u4 b# ~1 U8 j' @4 d# E
  55.                 }- Y2 V' c9 D) g' F" t; b2 E" T4 z, M  [
  56.                 return state;
    4 z  _3 |: q2 y" u2 S0 Z) l$ ~8 \) ^
  57.         }
    2 w2 t, `8 J4 t; y5 w
  58.         private static byte[] HexString2Bytes(String src) {: L. S1 U' \8 [, |7 J
  59.                 int size = src.length();& V5 |! C3 v, l0 w+ q0 r
  60.                 byte[] ret = new byte[size / 2];
    % {8 H+ H7 V; g& q
  61.                 byte[] tmp = src.getBytes(StandardCharsets.UTF_8);
    ; T( F0 v) Y/ v+ x; E
  62.                 for (int i = 0; i < size / 2; i++) {7 x9 Q3 g7 |3 t( [+ o+ |8 i( p
  63.                         ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
    4 Q- I. Z4 D8 r, D% R
  64.                 }8 R+ P0 b/ r% o
  65.                 return ret;( W* j- }( S! Q1 P; s' S% k' S
  66.         }
    2 v0 l) x% V/ v( W6 X8 a
  67.         private static byte uniteBytes(byte src0, byte src1) {
    . Y2 H4 s& Y1 \( t. B/ a- N
  68.                 char _b0 = (char) Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
    0 {7 Y+ A" Y9 Q) i" _: [
  69.                 _b0 = (char) (_b0 << 4);
    - ~9 ~% O) a; ^. P3 d
  70.                 char _b1 = (char) Byte.decode("0x" + new String(new byte[]{src1})).byteValue();' N" e; J' P% R7 T+ v
  71.                 return (byte) (_b0 ^ _b1);
      P8 p: U) h4 C7 Q' n, r
  72.         }
    - J( |! M3 D. w" E/ g
  73.         private static byte[] RC4Base(byte[] input, String mKkey) {
    # k3 ^" Y6 u% d! V( F
  74.                 int x = 0;
    ( Q6 S# a2 g! z
  75.                 int y = 0;  m. @  @& E% ?7 J
  76.                 byte key[] = initKey(mKkey);
    5 ]' i9 m7 t0 h. x" Y
  77.                 int xorIndex;7 q7 z/ W8 H' F: e# w. n' e5 d. T5 g
  78.                 byte[] result = new byte[input.length];
    3 x  D1 U% H$ q/ w
  79.                 for (int i = 0; i < input.length; i++) {
    3 f. P- g' c& |" ~* R6 N
  80.                         x = (x + 1) & 0xff;
    + ]% ~( c7 |- s- A; c
  81.                         y = ((key[x] & 0xff) + y) & 0xff;
    & s! g8 |. ~: }" d  D8 b
  82.                         byte tmp = key[x];
    % ]8 N2 S. b7 N; S% }1 n: t3 x
  83.                         key[x] = key[y];$ t, y$ b8 {) }+ `5 G6 K
  84.                         key[y] = tmp;& U' a1 k( b; }, _0 A- w0 |
  85.                         xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;$ ~4 J7 ^) @" s- J) ~6 m
  86.                         result[i] = (byte) (input[i] ^ key[xorIndex]);
    ( h. X3 d; T; }% Y
  87.                 }+ a- |9 M" i1 Z- ^' N+ l
  88.                 return result;3 Q% g  E# _1 ?! d1 @
  89.         }! \/ {  S, b! Q* y) c9 h
  90. }
    8 S' u  H5 A2 F
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-4-20 22:25 , Processed in 0.050406 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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