【问题标题】:Convert CryptoPP::Integer to LPCTSTR [duplicate]将 CryptoPP::Integer 转换为 LPCTSTR [重复]
【发布时间】:2017-02-20 17:56:27
【问题描述】:

我找不到将 CryptoPP::Integer(从 RSA 密钥生成)转换为 LPCTSTR(我想将密钥存储在注册表中)的正确代码。你能帮帮我吗?

谢谢!

【问题讨论】:

    标签: c++ windows integer crypto++


    【解决方案1】:

    ... 将 CryptoPP::Integer(来自 RSA 密钥生成)转换为 LPCTSTR(我想将密钥存储在注册表中)。你能帮帮我吗?

    应该执行以下操作。 Integer 类在integer.h 中重载operator<<

    Integer n("0x0123456789012345678901234567890123456789");
    ostringstream oss;    
    oss << std::hex << n;
    
    string str(oss.str());
    LPCSTR ptr = str.c_str();
    

    Integer 类在使用插入运算符时总是打印后缀。在上面的代码中,由于std::hex,将附加一个h。所以你可能想添加:

    string str(oss.str());
    str.erase(str.end() - 1);
    

    另一种方法是使用来自misc.h 的函数IntToString&lt;Integer&gt;()。但是,它只适用于窄字符串,不适用于宽字符串。

    Integer n("0x0123456789012345678901234567890123456789");
    string val = IntToString(n, 16)
    

    IntToString 不打印后缀。但是,需要 hack 才能以大写形式打印字符串(如手册中所示)。

    【讨论】:

    • 您好 jww,首先,感谢您的回复。我不想在我的应用程序中使用流,因为我在多线程 (/MT) 中编译并且 oss 流占据了很多位置。无论如何,第二个选项似乎很好,在我拥有我的 std::string 之后,我只需要将它转换为 LPCTSTR。谢谢你的回复,真的,你帮了我很多,因为 CryptoPP::Integer 不是那么容易
    • 这里似乎不需要宽字符串,因为在任何整数表示中使用的所有字符都在基本 ASCII 范围内。只需转换为 LP(C)STR 并使用注册表 API 的窄版本,例如 RegGetValueA,它们将负责与 Unicode 的转换。
    猜你喜欢
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2016-06-07
    • 2018-06-02
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多