1

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 !

jww
  • 97,681
  • 90
  • 411
  • 885
123problem
  • 37
  • 6

1 Answers1

0

... 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).

jww
  • 97,681
  • 90
  • 411
  • 885
  • Hi jww, first, thanks for your reply. I don't want to use the stream in my app because i'm compiling in Multithread (/MT) and oss stream take a lot of place. Anyway, the second option seems to be nice, after I have my std::string, I just need to convert it in LPCTSTR. Thanks you for your reply, really, you helped me a lot because CryptoPP::Integer aren't so easy – 123problem Feb 20 '17 at 23:26
  • 1
    There doesn't appear to be any need for wide strings here, since all the characters used in any representation of the integer are found in the base ASCII range. Just convert to `LP(C)STR` and use the narrow version of the registry APIs, like `RegGetValueA`, they'll take care of conversion to and from Unicode. – Ben Voigt Feb 21 '17 at 19:07