Decrypting encrypted messages


The following code sample demonstrates decryption of a decrypted CMS message using PKIFv2.

Supported Languages

C++

void DecryptingEncryptedMessages()
{
      //Make sure the EnvelopedData object creation function has be executed
      //(i.e. make sure the global SignedData buffer is populated)
      if(g_envelopedDataBuffer == (CPKIFBuffer*)NULL)
      {
            cout << "No buffer to verify" << endl;
            return;
      } 

      //Decode the message
      CPKIFContentInfo contentInfo;
      contentInfo.Decode(
            (unsigned char*)g_envelopedDataBuffer->GetBuffer(), g_envelopedDataBuffer->GetLength()); 

      CPKIFBufferPtr content = contentInfo.GetContent();
      CPKIFOIDPtr contentType = contentInfo.GetContentType(); 

      if(*contentType != *g_envelopedData)
      {
            cout << endl <<  "Message does not contain an enveloped message.  Exiting..." << endl;
            return;
      } 

      //Create a mediator object and pass it to a CPKIFEnvelopedData object
      //via the AddMediator function.
      IPKIFMediatorPtr mediator = MakeDefaultMediator(); 

      CPKIFEnvelopedData envelopedData;
      envelopedData.AddMediator(mediator); 

      //Decrypt the data (decryption key will be auto-discovered, if available)
      CPKIFCredentialPtr cred;
      try
      {
            //The Decode function simply decodes the encoded EnvelopedData message.
            envelopedData.Decode(content); 

            //The Decrypt function decrypts the encrypted contents.
            CPKIFBufferPtr decData = envelopedData.Decrypt(cred); 

            //Display the plaintext
            cout << "Decrypted message: " << decData->GetBuffer() << endl;
      }
      catch(CPKIFException& e)
      {
            cout << "Unexpected exception thrown by CreatingEncryptedMessages: ";
            cout << e.print()->c_str() << endl; 

            return;
      }
}

C#

public void DecryptingEncryptedMessages()
{
    //Make sure the EnvelopedData object creation function has be executed
    //(i.e. make sure the global SignedData buffer is populated)
    if (g_envelopedDataBuffer.get() == null)
    {
        Console.WriteLine("No buffer to verify");
        return;
    }

    //Decode the message
    CPKIFContentInfo contentInfo = new CPKIFContentInfo();
    contentInfo.Decode(g_envelopedDataBuffer.GetBuffer(), Convert.ToInt32(g_envelopedDataBuffer.GetLength())); 

    CPKIFBufferPtr content = contentInfo.GetContent();
    CPKIFOIDPtr contentType = contentInfo.GetContentType(); 

    if (contentType.ToString() != pkif_module.g_envelopedData.ToString())
    {
        Console.WriteLine();
        Console.WriteLine("Message does not contain an enveloped message.  Exiting...");
        return;
    } 

    //Create a mediator object and pass it to a CPKIFEnvelopedData object
    //via the AddMediator function.
    IPKIFColleaguePtr mediator = pkif_module.MakeDefaultMediator(); 

    CPKIFEnvelopedData envelopedData = new CPKIFEnvelopedData();
    envelopedData.AddMediator(mediator); 

    //Decrypt the data (decryption key will be auto-discovered, if available)
    CPKIFCredentialPtr cred = pkif_module.make_Null_CPKIFCredentialPtr();
    try
    {
        //The Decode function simply decodes the encoded EnvelopedData message.
        envelopedData.Decode(content); 

        //The Decrypt function decrypts the encrypted contents.
        CPKIFBufferPtr decData = envelopedData.Decrypt(cred);
        int t = Convert.ToInt32(decData.GetLength());
        string tmp = pkif_module.GetBufferAsString(decData); 

        //Display the plaintext
        Console.WriteLine("Decrypted message: " + pkif_module.GetBufferAsString(decData));
    }
    catch (Exception e)
    {
        Console.WriteLine("Unexpected exception thrown by CreatingEncryptedMessages: ");
        Console.WriteLine(e.Message);
        return;
    } 
}

JAVA

public void DecryptingEncryptedMessages()
{
   
//Make sure the EnvelopedData object creation function has be executed
    //(i.e. make sure the global SignedData buffer is populated)
    if (g_envelopedDataBuffer.get() == null)
    {
        System.
out.println("No buffer to verify");
       
return;
    }
 

    //Decode the message
    CPKIFContentInfo contentInfo = new CPKIFContentInfo();
    Long l =
g_envelopedDataBuffer.GetLength();
    contentInfo.Decode(
g_envelopedDataBuffer.GetBuffer(), l.intValue()); 

    CPKIFBufferPtr content = contentInfo.GetContent();
    CPKIFOIDPtr contentType = contentInfo.GetContentType();
 

    if (contentType.ToString().compareTo(pkif_module.getG_envelopedData().ToString()) != 0)
    {
        System.
out.println();
        System.
out.println("Message does not contain an enveloped message.  Exiting...");
       
return;
    }
 

    //Create a mediator object and pass it to a CPKIFEnvelopedData object 
    //via the AddMediator function.
    IPKIFColleaguePtr mediator = pkif_module.MakeDefaultMediator(); 

    CPKIFEnvelopedData envelopedData = new CPKIFEnvelopedData();
    envelopedData.AddMediator(mediator);
 

    //Decrypt the data (decryption key will be auto-discovered, if available)
    CPKIFCredentialPtr cred = pkif_module.make_Null_CPKIFCredentialPtr();
   
try
    {

        //The Decode function simply decodes the encoded EnvelopedData message.
        envelopedData.Decode(content);

        //The Decrypt function decrypts the encrypted contents.
        CPKIFBufferPtr decData = envelopedData.Decrypt(cred);
        Long l2 = decData.GetLength();
       
int t = l2.intValue();
        String tmp = pkif_module.GetBufferAsString(decData);

        //Display the plaintext
        System.out.println("Decrypted message: " + tmp);
    }
   
catch (Exception e)
    {
        System.
out.println("Unexpected exception thrown by CreatingEncryptedMessages: ");
        System.
out.println(e.getMessage());
        return;
    }
}