使用 CryptoPP::HexDecoder()

xte*_*408 5 c++ encryption hex crypto++ visual-c++

我第一次玩 Cryptopp,我找到了一个编码为十六进制的例子......一切都很好。现在我想将生成的 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");
}
Run Code Online (Sandbox Code Playgroud)

jww*_*jww 2

CryptoPP::StringSource(source, true,
    new CryptoPP::HashFilter(sha1,
        new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));
Run Code Online (Sandbox Code Playgroud)

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

CryptoPP::StringSource ss(source, true,
    new CryptoPP::HashFilter(sha1,
        new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash)
    )));
Run Code Online (Sandbox Code Playgroud)

为他们所有人做这件事。

另请参阅CryptoPP HexDecoder 输出为空