请选择 进入手机版 | 继续访问电脑版

登录  | 立即注册

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

查看: 2821|回复: 0

【Java】元宵节

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-6-30 22:18:45 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. import java.io.UnsupportedEncodingException;/ s( D2 c% J# t- g0 K( r: `* S
  2. import java.nio.charset.StandardCharsets;0 B% ~. Y/ O9 F) ~4 l; w
  3. import java.net.URL;
    # H9 S4 @- S+ M! |
  4. import java.io.BufferedReader;
    8 ^7 O  i, n) s' A1 d3 ?' `
  5. import java.io.InputStreamReader;
    7 ]' W; E. E3 M4 q& k0 Y! w
  6. public class Main7 a- m0 Y0 n0 ~4 O5 j3 V2 e
  7. {
    / T. y' U0 _! Q- C# X& |
  8.         public static void main(String[] args)/ B0 Z6 X/ X! W" S' R$ `/ r
  9.         {6 K/ C) |; ~% j# K2 [# s
  10.                 Main t6=new Main();" n9 `8 K4 p- P+ f6 t5 m: \
  11.                 String htmls= t6.getPageSource("http://bmob-cdn-11629.b0.upaiyun.com/2019/02/19/1483728040e51b2c80b9ddb4ad3c6ced.txt","utf8");  
    8 K/ r. ]" q0 }1 Z
  12.                         String decryStr = RC4Util.decryRC4(htmls, "happy");
    & m+ b8 k, }8 M1 U% ^+ O3 [3 g$ F- ?
  13.                 System.out.println(decryStr);
    # y) \* p$ K# ^( }& u
  14.                         }8 x8 V: p) c4 X! m6 Z2 _$ A
  15.         public String getPageSource(String pageUrl,String encoding) {    ' Q7 S9 Y+ y$ D( t2 Y' l. o
  16.                 StringBuffer sb = new StringBuffer();   
    $ [, Z9 _' ^# d! L. r
  17.                 try {   
    $ Y( x$ a) S, i2 U7 t% p2 R
  18.                         URL url = new URL(pageUrl);    " B# g2 N3 P- A; L$ ?" n+ u
  19.                         BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), encoding));    8 z  d3 ~2 r/ P$ j7 {
  20.                         String line;    * t. B. T" ~7 @" h0 m+ q1 h9 q( t. q
  21.                         while((line = in.readLine())!=null){/ Q! F0 _# H! q1 X4 x
  22.                                 sb.append(line);# f! a7 T, x4 [4 J$ r2 ~: d- o
  23.                         }in.close();}: O6 Z: o, q0 l/ |+ z
  24.                 catch (Exception ex) {   
    : x, e" B+ _9 |, F* O4 b
  25.                         System.err.println(ex);   
    2 T. v" `2 Y4 Z" Y3 a4 \
  26.                 }    7 e( e4 K9 @; D* g! y
  27.                 return sb.toString();    ' }% W( g+ U1 K# f, K0 |
  28.         }   0 G# T) r: V1 |& e& Z  c
  29. }) m& u6 Z( v* D5 K
  30. class RC4Util {
    ! e" ]! r* e/ q
  31.         public static String decryRC4(String data, String key) {
    : _& g! ~3 V. F# c4 l8 ^
  32.                 if (data == null || key == null) {3 ^( ~# ]. H" j1 R) d  M
  33.                         return null;; z& ^# @% {0 B# [0 P, N3 R
  34.                 }
    7 S5 O3 \: n  H4 n
  35.                 return new String(RC4Base(HexString2Bytes(data), key), StandardCharsets.UTF_8);5 C& k, e2 i6 q+ T9 K
  36.         }
    ' C& i# B$ k/ |  {
  37.         private static byte[] initKey(String aKey) {
    * @/ @$ d; O  F9 f% T$ O& s- M
  38.                 byte[] bkey = aKey.getBytes();
    . d. k% e7 g( K( c0 g
  39.                 byte state[] = new byte[256];
    ; }  \% h# R) l/ T* Z# Q6 t% q

  40. & i- v; A8 Z% h& j& _
  41.                 for (int i = 0; i < 256; i++) {( W8 u3 _- W" A0 L: {4 H$ `0 a
  42.                         state[i] = (byte) i;) R6 |, H# |* f7 @
  43.                 }
    % m5 J$ I  z. C
  44.                 int index1 = 0;
    ( g# }. D; g$ E5 C: @0 z
  45.                 int index2 = 0;
      G) q" V: l' v3 O) L: g) v  A
  46.                 if (bkey.length == 0) {( r8 D1 f/ B5 _8 L
  47.                         return null;7 K: N* D$ T6 ?6 j& ~' L8 R
  48.                 }4 z( R# e1 _- I6 B- i
  49.                 for (int i = 0; i < 256; i++) {: q# [  p* y3 A) z6 b
  50.                         index2 = ((bkey[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;/ Z% s* ^) \0 \' g
  51.                         byte tmp = state[i];
    0 M  P1 S9 N/ f  m
  52.                         state[i] = state[index2];* \) O/ _: k- l4 b: k5 C; C5 o1 }
  53.                         state[index2] = tmp;
    9 \6 E+ i; q  h! F( ~& w
  54.                         index1 = (index1 + 1) % bkey.length;; r" ~) b4 H, g# o2 P$ I
  55.                 }
    % s  n+ Y! F" w* T5 ]# `$ G
  56.                 return state;( H- L$ {5 ?. g7 r1 Q. z- X. u) q
  57.         }2 p3 M# i  u3 R( z
  58.         private static byte[] HexString2Bytes(String src) {1 [+ A7 _3 G# j( b. R' P
  59.                 int size = src.length();+ O; ?$ W% M" @. K8 R' W- r! n% B
  60.                 byte[] ret = new byte[size / 2];
    5 X+ ~4 E: J' P1 M2 {2 w
  61.                 byte[] tmp = src.getBytes(StandardCharsets.UTF_8);: `' D- ]5 M5 m
  62.                 for (int i = 0; i < size / 2; i++) {
    7 w' G0 M/ k# k5 J, x8 v: \
  63.                         ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);6 d. Q4 o* O( G; R: E( v3 a
  64.                 }
    8 I& O$ P! d  g# X/ M* I
  65.                 return ret;
    - l1 A2 ~' {* F" y. I: F2 Z$ r
  66.         }
    0 Y% ^5 E  d( v9 {& L8 H0 K
  67.         private static byte uniteBytes(byte src0, byte src1) {
    # y# ~' p, I3 x* m& |: `0 W0 g
  68.                 char _b0 = (char) Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
    & Y1 a. g3 `) ^! V
  69.                 _b0 = (char) (_b0 << 4);
    6 r$ i5 t8 b1 S1 N# a
  70.                 char _b1 = (char) Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
    % c# w* p; S5 u1 b' ?! B
  71.                 return (byte) (_b0 ^ _b1);* i$ r$ X5 k* W7 ^
  72.         }8 C% J$ h) _; U
  73.         private static byte[] RC4Base(byte[] input, String mKkey) {
    ( S7 J& B% u3 a1 g
  74.                 int x = 0;1 u3 w( r8 Z- {- T
  75.                 int y = 0;
    & I( s6 ~. f8 @& ]. ?( ~
  76.                 byte key[] = initKey(mKkey);/ F) i, m; H. Q
  77.                 int xorIndex;
    . {. g3 b  }: W
  78.                 byte[] result = new byte[input.length];
    4 x8 }' Y; V5 Z9 T$ B- A
  79.                 for (int i = 0; i < input.length; i++) {2 F8 q( _" v1 F! |4 N% l1 ]
  80.                         x = (x + 1) & 0xff;
    1 @8 q( E: Q% b
  81.                         y = ((key[x] & 0xff) + y) & 0xff;5 x. p) ]  }  P3 n/ E
  82.                         byte tmp = key[x];3 F5 y( U, x) _9 o' z% P6 K
  83.                         key[x] = key[y];, e7 a6 S; Y  g! v3 l1 R2 U, L9 O
  84.                         key[y] = tmp;4 D% T5 @6 u! J
  85.                         xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;2 ]9 @! P' e$ U8 V1 D* D) M, F  @
  86.                         result[i] = (byte) (input[i] ^ key[xorIndex]);8 L+ ~$ j9 R0 ]1 H+ A. Z
  87.                 }
    7 b0 d, V3 o& E% `
  88.                 return result;
    " L+ ^  [; K4 R! y, L
  89.         }) m, p" o  P3 d/ x4 p" j2 T
  90. }1 i4 D& B; M) F% A0 ^; C6 Y4 M
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-3-29 04:33 , Processed in 0.048142 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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