Zooming in: Base64Decoder

I’m zooming in the process of converting plaintext to a point in elliptic curve. And from the examples of Crypto++ usage, the plaintext is encoded to a base64 after it’s being encrypted, and decoded before it’s being decrypted.

The inheritance diagram for Base64Decoder:

From Research

The encoding process:

From Research

The codes:

// Encryption
Encryptor.Encrypt( rng, reinterpret_cast( PlainText.c_str() ), PlainTextLength, CipherText );

// Base 64 Encoding
CryptoPP::Base64Encoder Encoder;
Encoder.Put( CipherText, CipherTextLength );
Encoder.MessageEnd();

// Scratch for Base 64 Encoded Ciphertext
unsigned int EncodedTextLength = Encoder.MaxRetrievable();
byte* EncodedText = new byte[ EncodedTextLength + 1 ]; /* + 1 for NULL termination */
if( NULL == EncodedText ) { throw std::string( “Base64 EncodedText Allocation Failure” ); }
::memset( EncodedText, 0xFB, EncodedTextLength );
EncodedText[ EncodedTextLength ] = ”;

// Base 64 Ciphertext
Encoder.Get( EncodedText, EncodedTextLength );

// Diagnostics
std::cout << “Base 64 Encoded Ciphertext (” << EncodedTextLength << ” bytes):”;
std::cout << std::endl << EncodedText << std::endl;

// Base 64 Decoding
CryptoPP::Base64Decoder Decoder;
Decoder.Put( EncodedText, EncodedTextLength );
Decoder.MessageEnd();

// Scratch for Base 64 Decoded Ciphertext
unsigned int DecodedTextLength = Decoder.MaxRetrievable();
byte* DecodedText = new byte[ DecodedTextLength ];
if( NULL == DecodedText ) { throw std::string( “Base64 DecodedText Allocation Failure” ); }
::memset( DecodedText, 0xFB, DecodedTextLength );

// Ciphertext is no longer Encoded
Decoder.Get( DecodedText, DecodedTextLength );

//
// At this point, RecoveredText64 = CipherText
//
assert( DecodedTextLength == CipherTextLength );
assert( 0 == ::memcmp( DecodedText, CipherText, CipherTextLength ) );

// Scratch for Decryption
unsigned int RecoveredTextLength = Decryptor.MaxPlaintextLength( CipherTextLength );
if( 0 == RecoveredTextLength ) { throw std::string(“ciphertextLength is not valid (too long or too short)”); }

// Decryption Buffer
char* RecoveredText = new char[ RecoveredTextLength ];
if( NULL == RecoveredText ) { throw std::string( “RecoveredText CipherText Allocation Failure” ); }
::memset( RecoveredText, 0xFB, RecoveredTextLength );

// Decryption
Decryptor.Decrypt( rng, CipherText, CipherTextLength, reinterpret_cast( RecoveredText ) );

// Diagnostics
std::cout << “Recovered text (” << RecoveredTextLength << ” bytes):” << std::endl;
std::cout << “‘” << RecoveredText << “‘” << std::endl;

Questions:

  1. Why should the plaintext be encoded to base64?
  2. Why the plaintext is encoded after it’s encrypted not the vice versa?
Advertisements