使用CryptoPP :: HexDecoder()

时间:2014-09-09 12:11:06

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

我第一次玩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");
}

1 个答案:

答案 0 :(得分: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