我正在使用 crypto++ 来加密和解密字符串。代码如下所示。该代码对用户名和密码进行加密。但我不知道如何将其再次解密为字符串。什么是将加密的SHA256代码解密成字符串的代码。谁能帮我。
#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;
}
Run Code Online (Sandbox Code Playgroud)
正如评论者已经指出的那样,这段代码没有执行加密,而是执行散列。主要区别在于散列在设计上不是可逆的。这在密码应用程序中很重要,因为您明确不想以任何可访问的形式存储用户的密码,而只是检查它们。
因此,简而言之:您无法“解密”您的哈希。
当您想检查提供的密码的正确性时,您可以像在代码中一样再次对其进行散列,并将散列与原始密码的散列进行比较。