c++ - 将加密的 SHA256 代码解密为 crypto++ 中的字符串的代码

标签 c++ crypto++

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

最佳答案

正如评论者已经指出的那样,此代码并未执行加密,而是散列。主要区别在于,从设计上讲,散列法是不可逆的。这在密码应用程序中很重要,因为您明确不想以任何可访问的形式存储用户的密码,而只是检查它们。

因此,简而言之:您无法“解密”您的哈希值。

当您想检查提供的密码的正确性时,您可以像在您的代码中一样再次对其进行哈希处理,并将该哈希值与原始密码的哈希值进行比较。

关于c++ - 将加密的 SHA256 代码解密为 crypto++ 中的字符串的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985664/

相关文章:

c++ - C++ 中的右值绑定(bind)混淆

c++ - std::string 到 SecByteBlock 的转换

c++ - 解密后出现“消息哈希或 MAC 无效”异常

c++ - 将 Crypto++ 代码集成到 Linux 中的 Qt 应用程序

c++ - 将 2D char* 数组传递给函数时出错

c++ - C 和 C++ : data file with error "Expected unqualified-id"

c++ - MATLAB Mex 函数并不比常规函数快

c++ - 扫描目录以查找并打开文件

c++ - 使用 Crypto++ Base64 编码器时避免换行

c++ - 使用 AES 使用 Common Crypto 和 Crypto++ 加密时的不同结果