Вопрос

I am using crypto++ to encrypt and decrypt the string .The code is shown below. The code encrypts the username and password .But I don't know how to I decrypt it one again into string. What is the code to decrypt the encrypted SHA256 code into string. Can any one help me.

#include <cryptopp/hex.h>
#include <cryptopp/sha.h>
#include <cryptopp/base64.h>
#include <iostream>
#include <string>

int main()
{
  CryptoPP::SHA256 hash;
  byte digest[CryptoPP::SHA256::DIGESTSIZE];
  std::string username, password, salt, output;
  std::cout << "Enter username: ";
  std::getline(std::cin,username);
  std::cout << std::endl << "Enter password: ";
  std::getline(std::cin,password);
  salt = username + password;

  hash.CalculateDigest(digest,(const byte *)salt.c_str(),salt.size());

  CryptoPP::HexEncoder encoder;
  CryptoPP::StringSink *SS = new CryptoPP::StringSink(output);
  encoder.Attach(SS);
  encoder.Put(digest,sizeof(digest));
  encoder.MessageEnd();

  std::cout << "The username/password salted hash is => " << output << std::endl;
  return 0;
}
Это было полезно?

Решение

This code is not performing encryption, as the commentors already pointed out, but hashing. The central difference is that hashing, by design, is not reversible cheaply. This is important in password applications because you do explicitly not want to store the user's passwords in any accessible form, but only check against them.

So, in short words: You cannot "decrypt" your hash.

When you want to check a supplied password for correctness, you hash it again like in your code and compare the hash against the hash of the original password.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top