Using CryptoPP::HexDecoder( )

Using CryptoPP::HexDecoder( )

本文关键字:HexDecoder Using CryptoPP      更新时间:2023-10-16

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

#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");
}
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输出为空。