c++c++17crypto++

Crypto++ generating AES key to a file


I generate key to a file using the following function

std::filesystem::path create_key(std::filesystem::path folder_path)
{

    CryptoPP::AutoSeededRandomPool prng;
    CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
    prng.GenerateBlock(key, sizeof(key));

    std::cout << "Size of key : " << sizeof(key) << std::endl;
    std::cout << "Key before hex encode : " << (CryptoPP::byte*)&key << std::endl;

    std::filesystem::path key_path = folder_path.concat("\key.aes");
    
    // Key hex encoded to file
    CryptoPP::StringSource key_s((CryptoPP::byte*)&key, sizeof(key), true,
        new CryptoPP::HexEncoder(
            new CryptoPP::FileSink(key_path.c_str())));


    std::cout << key_path << "\n" << std::endl;
    return key_path;

}

I use same function with few modifications for IV

I get the following results:

Size of key : 16
Key before hex encode : %
"C:\\Users\\User\\Desktop\\New folder\\key.aes"

Size of IV : 16
IV before hex encode : á▀┼┘ÅP⌐ûG→.JW╓‼Ñg79▓─G
"C:\\Users\\User\\Desktop\\New folder\\iv.aes"

Obviously after encoding the results look more readable , Does the key should use this characters ? How can I read the the key and iv using crypto++

I have tried reversing the function :

CryptoPP::FileSource key(key_path.c_str(), sizeof(key), true,
        new CryptoPP::HexDecoder(
            new CryptoPP::StringSink((CryptoPP::byte*)&key),key_path.c_str()));

And use it by loading keys and IV but it didn't worked

I checked the key.aes file and found out it's created in ANSI encoding which make difference in few characters is there a way to make it utf-8

Console prints : ╢╘r#|ÿ┴♀[ÉB!±L↨SXêu:▄

Key.aes file : ¶Ôr#|˜Á[B!ñLS

converting key.aes file to utf-8 results : ¶Ôr#|˜Á[B!ñLS


Solution

  • This function is valid and works , No changes need to be added in my scenario.