用于将加密的SHA256代码解密为crypto++中的字符串的代码

Code to decrypt the encrypted SHA256 code into string in crypto++

本文关键字:代码 crypto++ 字符串 解密 SHA256 用于 加密      更新时间:2023-10-16

我使用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;
}

正如评论者已经指出的,此代码不是执行加密,而是执行hashing。主要的区别在于,散列在设计上是不可逆的。这在密码应用程序中很重要,因为您明确不希望以任何可访问的形式存储用户的密码,而只希望对其进行检查。

所以,简而言之:你不能"解密"你的散列。

当你想检查提供的密码是否正确时,你可以像在代码中一样再次对其进行散列,并将散列与原始密码的散列进行比较。