Search code examples
c++windowsintegercrypto++

Convert CryptoPP::Integer to LPCTSTR


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 !


Solution

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