BERDecode

From Crypto++ Wiki
Jump to navigation Jump to search
BERDecode
Documentation
#include <cryptopp/cryptlib.h>

BERDecode decodes an object using ASN.1 BER decoding. Calling Load on a class object usually calls BERDecode. The companion to BERDecode is DEREncode.

Each class object has their own format for encoding. The documentation for the class usually specifies the standard used to encode an object.

Also see Keys and Formats wiki page.

Samples

#include "rsa.h"
#include "hex.h"
#include "integer.h"
#include "files.h"
#include "osrng.h"

#include <iostream>
#include <string>

using namespace CryptoPP;

void DumpPrivateKey( const RSAES_OAEP_SHA_Decryptor& key );
void DumpPublicKey( const RSAES_OAEP_SHA_Encryptor& key );

int main(int argc, char* argv[])
{
   try {

      // Grab a Generator
      AutoSeededRandomPool rng;

      // Specify modulus, accept e = 17
      RSAES_OAEP_SHA_Decryptor Decryptor( rng, 64 /*, e */ );
      RSAES_OAEP_SHA_Encryptor Encryptor( Decryptor );

      // BER Encoded Keys
      std::string pubkey, prvkey;

      std::cout << "=====================================" << std::endl;
      std::cout << "========= BER Encoding Keys =========" << std::endl;
      std::cout << "=====================================" << std::endl;

      Decryptor.AccessKey().Save(
         HexEncoder(
            new StringSink( prvkey )
         )
      );
 
      std::cout << "Dumping Private Key..." << std::endl;
      DumpPrivateKey( Decryptor );

      // BER Encode Public Key
      Encryptor.AccessKey().Save(
         HexEncoder(
            new StringSink( pubkey )
         )
      );
      
      std::cout << "Dumping Public Key..." << std::endl;
      DumpPublicKey( Encryptor );

      std::cout << "====================================" << std::endl;
      std::cout << "======== BER Decoding Keys =========" << std::endl;
      std::cout << "====================================" << std::endl;

      RSAES_OAEP_SHA_Encryptor temp2; // Public
      RSAES_OAEP_SHA_Decryptor temp1; // Private
      
      { // BER Decode Private Key
         HexDecoder decoder;
         decoder.Put( (byte*)prvkey.c_str(), prvkey.size() );
         decoder.MessageEnd();

         temp1.AccessKey().Load( decoder );

         std::cout << "Dumping Private Key..." << std::endl;
         DumpPrivateKey( temp1 );
      }

      { // BER Decode Public Key
         HexDecoder decoder;
         decoder.Put( (byte*)pubkey.c_str(), pubkey.size() );
         decoder.MessageEnd();

         temp2.AccessKey().Load( decoder );

         std::cout << "Dumping Public Key..." << std::endl;
         DumpPublicKey( temp2 );      
      }
   }

   catch( Exception&e )
   {
      std::cerr << "Error: " << e.what() << std::endl;
   }

   catch( ... )
   {
      std::cerr << "Unknown Error" << std::endl;
   }

   return 0;
}

void DumpPrivateKey( const RSAES_OAEP_SHA_Decryptor& key )
{
   std::cout << "n: " << key.GetTrapdoorFunction().GetModulus();
   std::cout << std::endl;

   std::cout << "d: " << key.GetTrapdoorFunction().GetPrivateExponent();
   std::cout << std::endl;
   std::cout << "e: " << key.GetTrapdoorFunction().GetPublicExponent();
   std::cout << std::endl;

   std::cout << "p: " << key.GetTrapdoorFunction().GetPrime1();
   std::cout << std::endl;
   std::cout << "q: " << key.GetTrapdoorFunction().GetPrime2();
   std::cout << std::endl;
}

void DumpPublicKey( const RSAES_OAEP_SHA_Encryptor& key )
{
   std::cout << "n: " << key.GetTrapdoorFunction().GetModulus();
   std::cout << std::endl;

   ////////////////////////////////////////////////////////////////
   // Not in a Public Key...
   // std::cout << "d: " << key.GetTrapdoorFunction().GetPrivateExponent();
   // std::cout << std::endl;
   std::cout << "e: " << key.GetTrapdoorFunction().GetPublicExponent();
   std::cout << std::endl;

   ////////////////////////////////////////////////////////////////
   // Not in a Public Key...
   // std::cout << "p: " << key.GetTrapdoorFunction().GetPrime1();
   // std::cout << std::endl;
   // std::cout << "q: " << key.GetTrapdoorFunction().GetPrime2();
   // std::cout << std::endl;
}

Downloads

BERTest.zip - Sample Program listed above