0

I am trying to find the plain text password of a hashed password that is in the text file. I have done the code and everything, but when I input my hash it just prints it the same hash.

Here is the code that produces the hash:

//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;
}

Here is my code that should retrieve the hashed password in from a text file:

#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;
}

Here is the output from my terminal:

Enter the hash to break:
1vpZi631NyGhI

Enter in FIle: 
words.txt

The password is: 
1vpZi631NyGhI=    

As you can see, it should display the password which is "tree" and not the hash.

I am asking for assistance for educational purposes, and not giving me the answer.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
NightCode
  • 29
  • 5
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 05 '22 at 19:35
  • You are reading a line from a text file and that's it. You are never attempting to break the hash. You'll need to write recursive code that runs for multiple years in order to break the hash. Where is that code? – Thomas Weller Oct 05 '22 at 19:38
  • Hashes are 1-way. The whole point of hashing a password is so that you CAN'T RECOVER THE PASSWORD from the hash itself. The only way to do it is to brute force re-hash every possible password until you encounter a matching hash – Remy Lebeau Oct 05 '22 at 19:38
  • 3
    The traditional way to reverse a hash is 1) Guess the password. 2) Hash that guess. 3) If the hashes don't match, guess again. – Drew Dormann Oct 05 '22 at 19:42
  • 1
    The code in question is simply reading in each line from the file until a matching hash is found, and then lastly prints the entered hash itself and the last line that was read from the file (which will be blank if no match was found). At no point is this code attempting to actually crack the hash. – Remy Lebeau Oct 05 '22 at 19:46
  • 1
    "As you can see" No, I'm sorry but I can't see anything like that. Could you explain which line(s) of your code is/are responsible for deciding that `tree` hashes to `1vpZi631NyGhI`? Let's assume for simplicity that the file only contains one word which is `tree`. – n. m. could be an AI Oct 05 '22 at 19:50

0 Answers0