Convert CryptoPP::Integer to LPCTSTR [duplicate]

自古美人都是妖i 提交于 2019-12-24 06:08:09

问题


I can't find the right code to convert a CryptoPP::Integer (from a RSA key generation) to a LPCTSTR (I want to store the key in the registry). Could you help me ?

Thanks you !


回答1:


... convert a CryptoPP::Integer (from a RSA key generation) to a LPCTSTR (I want to store the key in the registry). Could you help me ?

Something like the following should do. The Integer class overloads operator<< in integer.h:

Integer n("0x0123456789012345678901234567890123456789");
ostringstream oss;    
oss << std::hex << n;

string str(oss.str());
LPCSTR ptr = str.c_str();

The Integer class always prints a suffix when using the insertion operator. In the code above, a h will be appended because of std::hex. So you might want to add:

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

Another way to do it is use the function IntToString<Integer>() from misc.h. However, it only works on narrow strings, and not wide strings.

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

IntToString does not print the suffix. However, hacks are needed to print the string in uppercase (as shown in the manual).



来源:https://stackoverflow.com/questions/42351335/convert-cryptoppinteger-to-lpctstr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!