【问题标题】:Code to decrypt the encrypted SHA256 code into string in crypto++在crypto++中将加密的SHA256代码解密为字符串的代码
【发布时间】:2015-11-17 15:07:13
【问题描述】:

我正在使用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++ crypto++


【解决方案1】:

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

所以,简而言之:你不能“解密”你的哈希值。

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

【讨论】:

  • "...并将哈希值与原始密码的哈希值进行比较" - 请务必使用 Crypto++ 的VerifyBufsEqual 以避免对比较的时间攻击。
猜你喜欢
  • 2013-04-17
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 2010-12-13
  • 2019-10-22
  • 2012-11-03
相关资源
最近更新 更多