I downloaded a sample code in crypto++ library. I tested that code and it returns a group of HEX code as a result. I want to send users id as a encrypted text to browser. When I get request for viewing particular user details I need to decrypt the request text and find the user id and process the data of that user and send the response to browser. But When I use the following code I get chipper text as group of HEX values. I want this chipper text as like SHA value around 20 to 25 length of string. How can I change the chipper text like SHA value.
//Key and IV setup
//AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-
//bit). This key is secretly exchanged between two parties before communication
//begins. DEFAULT_KEYLENGTH= 16 bytes
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
//
// String and Sink setup
//
std::string plaintext = "Now is the time for all good men to come to the aide...";
std::string ciphertext;
std::string decryptedtext;
//
// Dump Plain Text
//
std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;
//
// Create Cipher Text
//
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();
//
// Dump Cipher Text
//
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for( int i = 0; i < ciphertext.size(); i++ ) {
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
std::cout << std::endl << std::endl;
//
// Decrypt
//
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();
//
// Dump Decrypted Text
//
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;
Output:
Plain Text (55 bytes)
Now is the time for all good men to come to the aide...
Cipher Text (64 bytes)
0x7f 0xf8 0xb3 0xea 0x8a 0x2 0xb3 0x7a 0x3d 0x28 0x66 0x9c 0x97 0x13 0xa7 0xb3 0xf 0xa2 0x50 0x25 0x80 0xd5 0xd2 0x32 0xce 0xe8 0xa 0x57 0x33 0xef 0x70 0xff 0x48 0xe9 0xe8 0x4 0x98 0xa9 0x4 0xc2 0x5e 0xa7 0xb0 0x40 0x43 0xa1 0xfc 0x23 0xb1 0xa1 0xeb 0x1e 0xb2 0xf6 0x97 0x62 0x70 0xa1 0x81 0xca 0x6e 0x78 0x80 0x90
Decrypted Text:
Now is the time for all good men to come to the aide...
But I want chipper text as,
kdHkekdKLI!kdheGHWewqef
But I want chipper text as
kdHkekdKLI!kdheGHWewqef
I think you want either a Base64Encoder or Base64URLEncoder. Its not clear to me which alphabet you want since they are very similar.
You can change the following:
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for( int i = 0; i < ciphertext.size(); i++ )
{
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
To something like the following using a Pipeline. A pipeline allows data to flow from a source to a sink.
std::string encoded;
StringSource ss(ciphertext, true, new Base64Encoder(new StringSink(encoded)));
You can also do it manually with Put
and Get
, which is more C-ish. Under the hood, this is what the pipeline does for you (with some hand waiving):
std::string encoded;
Base64Encoder encoder;
encoder.Put((const byte*)ciphertext.data(), ciphertext.size());
encoder.MessageEnd();
size_t ready = encoder.MaxRetrievable();
if (ready)
{
encoded.resize(ready);
encoder.Get((byte*)&encoded[0], ready);
}
std::cout << encoded << std::endl;
Finally, you can perform the base64 encoding in the StreamTransformationFilter
pipeline with the following:
StreamTransformationFilter stfEncryptor(cbcEncryption, new Base64Encoder(new StringSink(ciphertext)));