You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++哈希密码破解代码异常求助:无法获取对应明文

哈希破解代码问题排查指导

我尝试从文本文件中找到哈希密码对应的明文(正确明文应为tree),已编写哈希生成代码与破解代码,但输入哈希后输出的仍是哈希而非正确明文。

哈希生成代码

//To Compile: g++ -o our_crypt our_crypt.cpp -lcrypt

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
  string plain_pass="password";
  string salt="salt";
  cout << "Please enter a plaintext password:\n";
  cin >> plain_pass;
  cout <<"\nNow enter a salt: \n";
  cin >> salt;
  const char * arg1 = plain_pass.c_str();
  const char * arg2 = salt.c_str();
  string hash = crypt(arg1, arg2);
  cout << "The Hash is: " << hash <<"\n";
  return 0;
}

哈希破解代码

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
#include <cstring>
#include <string>
#include <iostream>
#include <string>

int main() 
{
  // :: is called the Scope Resolution Operator. It makes it clear to which namespace or class a symbol belongs.
        
  std::cout << "Enter the hash to break:\n";
  std::string passHash;
        
  std::cin >> passHash;
        
  std::string fileName;
        
  std::cout<< "Enter in file: \n";
  std::cin >> fileName;
        
  std::fstream inFile;
  inFile.open(fileName,std::ios::in); 
        
  if (inFile.is_open())
  {  //checking whether the file is open
    std::string nline;
    while(getline(inFile, nline))
    { //read data from file object and put it into string.
      if(nline == passHash) 
      {
        break;
      }
    }
    std::cout <<"The password is: \n" <<passHash <<"=" <<nline;
  }
  inFile.close();
        
  return 0;
}

终端输出

Enter the hash to break:
1vpZi631NyGhI

Enter in FIle: 
words.txt

The password is: 
1vpZi631NyGhI=    

排查指导要点

  • 哈希破解的核心逻辑是:用文件中的明文生成对应的哈希,再和目标哈希对比,而非直接拿明文与哈希做相等判断
  • 检查当前破解代码的判断条件:你现在是把文件里的明文和输入的哈希直接比较,这和哈希生成的逻辑完全不匹配
  • 回忆哈希生成代码的流程:生成哈希需要明文和盐,破解时是不是也需要用相同的盐对文件中的每个明文生成哈希,再和目标哈希对比?
  • 观察终端输出的nline为空,说明循环中没有找到匹配项,这是判断逻辑错误导致的,根本不可能找到符合条件的内容
  • 思考循环结束后的输出逻辑:当前不管是否找到匹配的明文,都会直接输出passHash和nline,应该先判断是否找到有效匹配,再输出对应的结果

内容的提问来源于stack exchange,提问作者NightCode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 11:45:36