Performing symmetric key encryption operations

PKIFv2 can be used to perform symmetric key encryption operations using the DES, Triple DES, AES algorithms.  AES algorithm only works with NSS crypto provider.

Supported Languages

C++

void PerformingSymmetricKeyOperations()
{
      //Declare a crypto mediator with default colleagues (support
      //for symmetric key opeations will be provided by the CPKIFCAPIRaw colleague)
      CPKIFCryptoMediator2 cm(true);
      cm.InitializeMediator(NULL); 

      //Alternatively, create a mediator via MakeDefaultMediator and access the
      //necessary interfaces via GetMediator, efor example:
      //IPKIFMediatorPtr m = MakeDefaultMediator();
      //IPKIFCryptoRawOperations* cRaw = m->GetMediator<IPKIFCryptoRawOperations>();
 

      //create a buffer containing data to encrypt and a buffer to hold
      //the result (result will always be PKCS5 padded)
      unsigned char dataToEnc[] = {0x01,0x02,0x03};
      int dataToEncLen = 3;
      unsigned char encData[16];
      int encDataLen = 16;
      unsigned char decData[16];
      int decDataLen = 16; 

      unsigned char symKey[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
      int symKeyLen = 8; 

      CPKIFKeyMaterial key;
      key.SetSymmetricKey(symKey, symKeyLen);
      key.SetSymmetricKeyAlgorithm(DES);
      key.SetMode(ECB); 

      //create a hash context for the desired algorithm
      IPKIFRawCryptContext* ci = cm.CryptInit(key); 

      //invoke Encrypt (may be invoked repeatedly passing false for the last parameter
      //indicating that this not the final block)
      cm.Encrypt(ci, dataToEnc, dataToEncLen, encData, &encDataLen, true);
      delete ci;
      ci = cm.CryptInit(key);
      cm.Decrypt(ci, encData, encDataLen, decData, &decDataLen, true);
      delete ci; 

      cout << endl;
      if(decDataLen == dataToEncLen && 0 == memcmp(decData, dataToEnc, decDataLen))
            cout << "Successfully encrypted and decrypted data" << endl;
      else
            cout << "Failed to encrypt and decrypt data" << endl;

}

C#

public void PerformingSymmetricKeyOperations()
{
    //Declare a crypto mediator with default colleagues (support
    //for symmetric key opeations will be provided by the CPKIFCAPIRaw colleague)
    IPKIFColleaguePtr cmInter = pkif_module.make_NewCPKIFCryptoMediator2(true);
    CPKIFCryptoMediator2 cm = pkif_module.Get_CryptoMediator(cmInter);
    cm.InitializeMediator(null); 

    //Alternatively, create a mediator via MakeDefaultMediator and access the
    //necessary interfaces via GetMediator, for example:
    //IPKIFColleaguePtr m = pkif_module.MakeDefaultMediator();
    //IPKIFCryptoRawOperations cRaw = pkif_module.Get_IPKIFCryptoRawOperations(m);
 

    //create a buffer containing data to encrypt and a buffer to hold
    //the result (result will always be PKCS5 padded)
    byte[] dataToEnc = {0x01,0x02,0x03};
    byte[] encData = new byte[8];
    byte[] decData  = new byte[8]; 

    SWIGTYPE_p_int encDataLen = pkif_module.Make_int(8);
    SWIGTYPE_p_int decDataLen = pkif_module.Make_int(8); 

    byte[] symKey = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; 

    CPKIFKeyMaterial key = new CPKIFKeyMaterial();
    key.SetSymmetricKey(symKey, symKey.Length);
    key.SetSymmetricKeyAlgorithm(SYMKEY_ALG.DES);
    key.SetMode(SYMKEY_MODE.ECB); 

    //create a hash context for the desired algorithm
    IPKIFRawCryptContext ci = cm.CryptInit(key);

 
    //invoke Encrypt (may be invoked repeatedly passing false for the last parameter
    //indicating that this not the final block)
    cm.Encrypt(ci, dataToEnc, dataToEnc.Length, encData, encDataLen, true);
    pkif_module.Delete_IPKIFRawCryptContext(ci); 

    ci = cm.CryptInit(key);
    cm.Decrypt(ci, encData, encData.Length, decData, decDataLen, true);
    pkif_module.Delete_IPKIFRawCryptContext(ci);
 

    Console.WriteLine();
    if (decData[0] == dataToEnc[0] && decData[1] == dataToEnc[1] && decData[2] == dataToEnc[2])
        Console.WriteLine("Successfully encrypted and decrypted data");
    else
        Console.WriteLine("Failed to encrypt and decrypt data");
}

JAVA

public void PerformingSymmetricKeyOperations()
{
   
//Declare a crypto mediator with default colleagues (support
    //for symmetric key opeations will be provided by the CPKIFCAPIRaw colleague)
    IPKIFColleaguePtr cmInter = pkif_module.make_NewCPKIFCryptoMediator2(true);
    CPKIFCryptoMediator2 cm = pkif_module.Get_CryptoMediator(cmInter);
    cm.InitializeMediator(
null); 

    //Alternatively, create a mediator via MakeDefaultMediator and access the
    //necessary interfaces via GetMediator, for example:
    //IPKIFColleaguePtr m = pkif_module.MakeDefaultMediator();
    //IPKIFCryptoRawOperations cRaw = pkif_module.Get_IPKIFCryptoRawOperations(m); 

    //create a buffer containing data to encrypt and a buffer to hold
    //the result (result will always be PKCS5 padded)
    byte[] dataToEnc = {0x01,0x02,0x03};
   
byte[] encData = new byte[8];
   
byte[] decData  = new byte[8]; 

    SWIGTYPE_p_int encDataLen = pkif_module.Make_int(8);
    SWIGTYPE_p_int decDataLen = pkif_module.Make_int(8);
 

    byte[] symKey = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; 

    CPKIFKeyMaterial key = new CPKIFKeyMaterial();
    key.SetSymmetricKey(symKey, symKey.
length);
    key.SetSymmetricKeyAlgorithm(SYMKEY_ALG.
DES);
    key.SetMode(SYMKEY_MODE.
ECB); 

    //create a hash context for the desired algorithm
    IPKIFRawCryptContext ci = cm.CryptInit(key); 

 
    //invoke Encrypt (may be invoked repeatedly passing false for the last parameter
    //indicating that this not the final block)
    cm.Encrypt(ci, dataToEnc, dataToEnc.length, encData, encDataLen, true);
    pkif_module.Delete_IPKIFRawCryptContext(ci);
 

    ci = cm.CryptInit(key);
    cm.Decrypt(ci, encData, encData.
length, decData, decDataLen, true);
    pkif_module.Delete_IPKIFRawCryptContext(ci);
 

    System.out.println();
   
if (decData[0] == dataToEnc[0] && decData[1] == dataToEnc[1] && decData[2] == dataToEnc[2])
        System.
out.println("Successfully encrypted and decrypted data");
   
else
        System.out.println("Failed to encrypt and decrypt data"); 

}