Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrypt a password generated in crypt()?

Tags:

c

crypt

What I am searching for is a decrypt function to the crypt(3) function. Reading the manual they only refer me to see login(1), passwd(1), encrypt(3), getpass(3), passwd(5), but as far as I am aware, non of them can be used to decrypt the string.

I wrote together a small program to show my point, the function I am looking for is the somefunctogetbackplaintext(...)

#define _XOPEN_SOURCE
#include <unistd.h>
#include <string.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
  char *cryptated = crypt(argv[1], "aa"); // Password and salt
  if(strcmp("somepassword", somefunctogetbackplaintext(argv[1], cryptated, "aa"))) //Plain text, cryptated string, salt
    {
      printf("Success!\n");
    }
  else
    {
      printf("Not a success!\n");
    }

  return 0;
}
like image 772
Salviati Avatar asked Feb 02 '19 19:02

Salviati


People also ask

Can a password be decrypted?

Encryption means the data (such as the password) can be decrypted if you have the right key. Most passwords, however, cannot be decrypted since they weren't encrypted in the first place. Instead, one might be able to recover them by running a lengthy attack.

Can we decrypt bcrypt password?

No, there is no way to get the original string without exhaustively trying all possible inputs. This is the entire point of password hashes like bcrypt.


3 Answers

crypt does not encrypt passwords (so there is no way to decrypt them). Instead it hashes a given password, producing a string that is impossible to reverse to the original password (because the hash function loses information in the process). The most practical way to attack crypt and recover passwords from their hashes is probably some sort of dictionary attack.

However, none of that is necessary to check whether a given password is correct:

const char *password_and_salt = ...;  // e.g. from getpwent or a database
const char *input = argv[1];
if (strcmp(crypt(input, password_and_salt), password_and_salt) == 0) {
    printf("your password is correct\n");
}

In other words, you pass the user input to crypt and check whether it matches the known result of an earlier crypt. If so, the passwords match.

like image 72
melpomene Avatar answered Oct 12 '22 17:10

melpomene


Here is a summary excerpt from this article distinguishing between the concepts of encryption and Hashing:

Passwords remain the primary means for online authentication and must be protected when stored on a server. Encryption is an option, but it has an inherent weakness in this application because the server authenticating the password must have the key to decrypt it. An attacker who steals a file of encrypted passwords might also steal the key.

Hashing is a better option, especially with the judicious use of salt, according to mathematician Andrew Regenscheid and computer scientist John Kelsey of the National Institute of Standards and Technology’s Computer Security Division.

Encryption is a two-way function; what is encrypted can be decrypted with the proper key. Hashing, however, is a one-way function that scrambles plain text to produce a unique message digest. With a properly designed algorithm, there is no way to reverse the hashing process to reveal the original password. An attacker who steals a file of hashed passwords must then guess the password.
(emphasis mine)

Also (from comments) this link plainly states: crypt is the library function which is used to compute a password hash...

like image 29
ryyker Avatar answered Oct 12 '22 17:10

ryyker


As wikipedia article about crypt states:

Excerpt 1:

crypt is the library function which is used to compute a password hash that can be used to store user account passwords while keeping them relatively secure (a passwd file).

Excerpt 2:

This is technically not encryption since the data (all bits zero) is not being kept secret; it's widely known to all in advance. However, one of the properties of DES is that it's very resistant to key recovery even in the face of known plaintext situations. It is theoretically possible that two different passwords could result in exactly the same hash. Thus the password is never "decrypted": it is merely used to compute a result, and the matching results are presumed to be proof that the passwords were "the same."

So that is the answer to question: "the password is never "decrypted""

like image 21
Alex Yu Avatar answered Oct 12 '22 17:10

Alex Yu