【问题标题】:Using CryptoPP::HexDecoder( )使用 CryptoPP::HexDecoder( )
【发布时间】:2014-11-02 20:32:28
【问题描述】:

我第一次玩 Cryptopp,我找到了一个可以编码为 Hex 的示例……一切都很好。 现在我想将产生的 std::string 解码为原始字符串,但我得到的只是空字符串。

#include "stdafx.h"

#include "../cryptopp562/sha.h"
#include "../cryptopp562/filters.h"
#include "../cryptopp562/hex.h"
#include <iostream>
#include <string>

int main() {
    CryptoPP::SHA1 sha1;
    std::string source = "Panawara";  
    std::string hash = "";
    std::string original= "" ;

    CryptoPP::StringSource(source, true, new CryptoPP::HashFilter(sha1, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));
    std::cout << hash;
    std::cout << "\n";

    CryptoPP::StringSource (hash, new CryptoPP::HexDecoder(new CryptoPP::StringSink(original)));  // the result is always empty String
    std::cout << original;
    std::cout << "\n";

    system("pause");
}

【问题讨论】:

  • 当然,有点晚了,但我只想问:你知道吗,SHA只是一个哈希生成器,而不是像AES这样的对称加密算法?查看更多here

标签: c++ visual-c++ encryption hex crypto++


【解决方案1】:
CryptoPP::StringSource(source, true,
    new CryptoPP::HashFilter(sha1,
        new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));

不要使用匿名声明。命名你的变量。改用这个:

CryptoPP::StringSource ss(source, true,
    new CryptoPP::HashFilter(sha1,
        new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash)
    )));

为所有人做。

另见CryptoPP HexDecoder output empty

【讨论】:

    猜你喜欢
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多