sha1算法破解

去https://www.mysterytwisterc3.org/注册一个账号(字母大写),i注册的时候有个Captcha可能要FQ才能显示,账号密码记住以后要用到。
完成关卡Cracking SHA1-Hashed Passwords
https://www.mysterytwisterc3.org/en/challenges/level-ii/cracking-sha1-hashed-passwords
在这里插入图片描述

pdf中键盘上的指纹,它提示我们密码中可能出现的字符有“QWINqwin%(*=2468”,且密码经过sha1算法加密后的密文为:67ae1a64661ac8b4494666f58c4822408dd0a3e4,要求我们解出密。

代码的原理和第五题相似,都是sha1算法暴力破解。因为是先写的第五题,这题团里就不多说了。
源代码:

package sha1;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Callable;
import java.lang.Math;
/**
 * 文件注释:
 *
 * @Auther: 
 * @Date: 2018/2/27 18:30
 * @Description:
 */
public  class Sha1 implements Callable {

    private int threadNumber;
    Sha1(int threadNumber){
        this.threadNumber = threadNumber ;
    }

    /**
     * 重写call方法,分配每个线程的任务
     * @return 0或10
     * @throws Exception
     */
    @Override
    public Object call() throws Exception {
        /*可能的字符*/
        char [] encryptedText = ("QWINqwin%(*=2468").toCharArray();
        //char [] encryptedText = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*()[]|;',<>.?".toCharArray();
        /*长度为5的密文字符串*/
        String data = null;
        /*根据输入的线程编号threadNumber,分配相应的任务*/
        int [] temp = new int[8];
        for (int i = 0; i<temp.length ; i++) {
            int n = (int) Math.pow(2,i);
            if((threadNumber & n) > 0){
                temp[i] = 1;
            }
            else {
                temp[i] = 0;
            }
        }

        int length = encryptedText.length / 2;
        /*8个字符的组合*/
        for(int a = (length * temp[0]) ; a < (length * (temp[0]+1)); a++){
            for(int b = (length * temp[1]); b < (length * (temp[1]+1)); b++){
                for(int c = (length * temp[2]); c < (length * (temp[2]+1)); c++){
                    for(int d = (length * temp[3]); d < (length * (temp[3]+1)); d++){
                        for(int e = (length * temp[4]); e < (length * (temp[4]+1)); e++){
                            for(int f = (length * temp[5]); f < (length * (temp[5]+1)); f++){
                                for(int g = (length * temp[6]); g < (length * (temp[6]+1)); g++){
                                    for(int h = (length * temp[7]); h < (length * (temp[7]+1)); h++){
                                        /*获取不同的键盘字符组合*/
                                        data = ""+ encryptedText[a] + encryptedText[b] + encryptedText[c] + encryptedText[d] + encryptedText[e] + encryptedText[f]+ encryptedText[g]+ encryptedText[h];
                                        /*将字符加密*/
                                        String result = sha1(data);
                                        String decrypted = "67ae1a64661ac8b4494666f58c4822408dd0a3e4";
                                        //String decrypted = "4b58475789e60dbf1a28d638b556a938134644c8";
                                        /*如果字符加密后的sha1哈希值和所给的相等,即为找到了答案*/
                                        if(decrypted.equals(result)){
                                            //System.out.println("4b58475789e60dbf1a28d638b556a938134644c8的sha1算法解密结果为: "+data);
                                            System.out.println("67ae1a64661ac8b4494666f58c4822408dd0a3e4的sha1算法解密结果为: "+data);
                                            return 10;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return 0;
    }
    /**
     * sha1算法加密
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String sha1(String data) throws NoSuchAlgorithmException {
        //信息摘要器                                算法名称
        MessageDigest md = MessageDigest.getInstance("SHA1");
        //把字符串转为字节数组
        byte[] b = data.getBytes();
        //使用指定的字节来更新我们的摘要
        md.update(b);
        //获取密文  (完成摘要计算)
        byte[] b2 = md.digest();
        //获取计算的长度
        int len = b2.length;
        //16进制字符串
        String str = "0123456789abcdef";
        //把字符串转为字符串数组
        char[] ch = str.toCharArray();

        //创建一个40位长度的字节数组
        char[] chs = new char[len*2];
        //循环20次
        for(int i=0,k=0;i<len;i++) {
            //获取摘要计算后的字节数组中的每个字节
            byte b3 = b2[i];
            // >>>:无符号右移
            // &:按位与
            //0xf:0-15的数字
            chs[k++] = ch[b3 >>> 4 & 0xf];
            chs[k++] = ch[b3 & 0xf];
        }
        //字符数组转为字符串
        return new String(chs);
    }
}
package sha1;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
/*Future类包含线程池执行完毕之后存储的结果*/

/**
 * 文件注释:
 *
 * @Auther: 
 * @Date: 2018/2/27 18:33
 * @Description:
 */
public class Sha1Test {
   public static void main(String[] args) {
      // 执行线程池
      ThreadPoolExecutor executor=(ThreadPoolExecutor)Executors.newFixedThreadPool(32);

      //分成32个任务计算,提交任务
      for (int i=0; i<256; i++){
          Sha1 sha1 = new Sha1(i);
            /*Future类获取线程的返回值*/
            Future<Integer> result = executor.submit(sha1);
            try {
                /*将返回值转换成Interger对象*/
                Integer test = result.get();
                /*比较结果后终止线程池*/
                if(test.equals((Integer)10)){
                    // 关闭线程池
                    executor.shutdown();
                }
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
      }
      // 关闭线程池
      executor.shutdown();
   }
}

在线解密:
在这里插入图片描述

密文67ae1a64661ac8b4494666f58c4822408dd0a3e4最后的sha1算法解密结果为:(Q=win*5。提交通过。
在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值