• Java 多线程 破解密码 demo


    功能要求:

    破解密码 要求具体类:

    Decrypt  测试类,用来启动破解和日志线程

    DecryptThread 破解线程类,用来生成测试的字符串,并暴力破解

    LogThread 日志类,将输出每次生成的字符串结果集,并且设置为守护线程,等DecryptThread线程运行结束,也将停止运行

    package decrypt;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class Decrypt {
        
        // 生成长度为3的随机字符串
        private static String getRandomStr() {
            char []chs = new char[3];
            Random rd = new Random();
            for(int i=0;i<3;i++) {
                // 生成 [0,10) 的数
                chs[i] =(char) (rd.nextInt(10)+'0');
            }
            return new String(chs);
        }
        
        public static void main(String[] args) {
            String password = getRandomStr();
            List<String> list = new ArrayList<String>();
            DecryptThread dec = new DecryptThread(list, password);
            LogThread log = new LogThread(list);
            dec.start(); 
            log.start();
        }
    
        
    }
    decrypt类
    package decrypt;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class DecryptThread extends Thread {
        boolean ok = false;
        private List<String> list;
        private String password;
        public DecryptThread() {}
        public DecryptThread(List<String> list,String password) {
            this.list = list;
            this.password = password;
        }
        
        @Override
        public void run() {
            char []chs = new char[3];
            String str=null;
            for(int i=0;i<=9;i++) {
                for(int j=0;j<=9;j++) {
                    for(int k=0;k<=9;k++) {
                        chs[0]=(char)(i+'0');
                        chs[1]=(char)(j+'0');
                        chs[2]=(char)(k+'0');
                        str = new String(chs);
                        list.add(str);
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if(str.equals(password)) {
                            System.out.printf("成功匹配到密码,密码是%s%n",str);
                            ok=true;
                            return ;
                        }
                    }
                }
            }
        }
    }
    DecryptThread类
    package decrypt;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class LogThread extends Thread{
        private List<String> list;
        LogThread(){}
        
        LogThread(List<String> list) {
            this.list = list;
            // 日志线程设置为 守护线程
            this.setDaemon(true);
        }
        
        @Override
        public void run() {
            while(true) {
                while (list.isEmpty()) {
                    try {
                        Thread.sleep(50);    
                    }catch(Exception e) {
                    e.printStackTrace();
                    }
                }
                String password = list.remove(0);
                System.out.printf("生成的字符串是%s%n", password);
            }
        }
        
    }
    LogThread

    可改进功能:

      1.字符串长度增加,并且不仅有数字还有字母以及 特殊字符

      2.可以将log日志得到的结果 输出到文件,通过缓存来减少IO次数

      3.可以将正确密码 加密 存到 数据库中,练习数据库的操作

      4.暂时还没想到,嘿嘿

  • 相关阅读:
    原 java调整数据顺序是奇数位于偶数的前面(思路与实现)
    队列和栈是什么,列出它们的区别?
    python 虚拟环境创建
    系统架构知识
    Git 管理代码
    继承类构造方法使用
    if __name__=="__main__"
    django 项目创建
    python unittest的使用
    python ddt/paramunittest的使用
  • 原文地址:https://www.cnblogs.com/Draymonder/p/9567463.html
Copyright © 2020-2023  润新知