登录  | 立即注册

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

查看: 1038|回复: 0

【Java】简单加解密

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-6-30 22:18:12 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. public class JavaCrackMe03 {     5 {, B2 S9 E1 N2 j% J4 c3 L- Z
  2. public static char[] szSrc = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    # l+ \$ `4 w+ z7 X1 A& W; N
  3.    public static char[] szKey = new char[]{'b', 'f', 'd', 'j', 'z', 'e', 'r', 'm', 'i', 'a', 'k', 'x', 'v', 'h', 'c', 'g', 'o', 'p', 'l', 'q', 's', 'n', 't', 'u', 'w', 'y'};      
    ) L" B# ~5 i9 Z  s+ C
  4. public static int getIndex(char[] arr, char ch) { 7 h, {, Q7 j2 \, E  s
  5.        for(int i = 0; i < arr.length; ++i) {         
    ( m/ W1 {, j/ {1 b! }" t
  6.         if(ch == arr[i]) {
    2 _; ^" G  R0 ?# T- U4 }
  7.                return i;           
    , ^& @3 s( A; d1 D6 S6 p4 F6 h1 p
  8. }       % x2 \7 q6 x+ f1 G8 ]
  9. }         : }# |; t- S1 ?4 A4 j
  10. return -1;    % Z+ Q/ }3 ^, S5 w! r9 d
  11. }      8 ?9 ~* \7 e' \2 g% v
  12. public static String encode(String string) { 9 a, K- W( }/ H
  13.        StringBuffer stringbuffer = new StringBuffer();        
    & M( a/ D' K& V; k
  14.         for(int i = 0; i < string.length(); ++i) {0 g# H8 k. }! k- W) J
  15.             char ch = string.charAt(i); 1 c: F! g) a1 k1 D- l
  16.            int m = getIndex(szSrc, ch);5 x7 f5 M2 ~0 g7 Q% t
  17.             stringbuffer.append(szKey[m]); # N3 H% L3 S5 e
  18.        }  
    7 d; z0 K6 [) J5 b6 E# _# B; A
  19.         System.out.println("密文:" + stringbuffer);
    & d5 R  i; C! J3 O& D7 ?& h: K
  20.                 return new String(stringbuffer);, o* U5 p+ Y" C7 G0 `4 L" U& l3 T& d
  21.                     }  , R2 y, s; }+ g; |9 B& e5 E
  22.     public static String DeCode(String string) {
    1 M6 P) J8 K. `4 a: R
  23.        StringBuffer stringbuffer = new StringBuffer();
    , e) x8 s' l  i8 R* J& u! s( E
  24.          for(int i = 0; i < string.length(); ++i) {
      Z& W1 M- Y$ J9 n
  25.            char ch = string.charAt(i);
    * ?7 d5 C# v' X2 U8 K
  26.             int m = getIndex(szKey, ch);   
    3 Y# }$ V' f6 Q1 i
  27.          stringbuffer.append(szSrc[m]);  
    ' `& T  T% U+ Z7 p: S0 x: H
  28.       }      $ m/ M# t- d/ Y( B. D( i( H& Q
  29.     System.out.println("明文:" + stringbuffer);$ x/ _& H+ o* u% l( Y0 G  \
  30.             return new String(stringbuffer);
    9 Q) f* D7 p# |0 H$ T6 ^8 g
  31.    }    ' `* \/ V- C1 V) P4 P9 k
  32.   public static void main(String []args)  
    / {" V2 _- w- a
  33.   {        ( t" O8 l* w% M6 A0 Z, q
  34.         encode("java");
    / e4 ]6 t7 A: K' R9 M
  35.         DeCode("abnb");
    7 f" \$ l; o3 Q$ _$ M$ ?
  36.    }- i$ d/ F+ c+ t- V  Y
  37.    }
    " j: E4 E8 F1 {. Q
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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