Discuss this help topic in SecureBlackbox Forum

XML: Use passwords or symmetric keys for encryption

To encrypt the data or the encryption key using a password or a secret (symmetric) key, you need to employ TElXMLKeyInfoSymmetricData class. All you need to do is

  1. create an instance of TElXMLKeyInfoSymmetricData class;
  2. create an instance of TElSymmetricKeyMaterial class and assign it to TElXMLKeyInfoSymmetricData.Key property;
  3. set TElSymmetricKeyMaterial.Algorithm property to the desired symmetric algorithm;
  4. set the TElSymmetricKeyMaterial.Key and TElSymmetricKeyMaterial.IV (if needed) properties

If you are using the instance of TElXMLKeyInfoSymmetricData class for encryption of the data, assign it to KeyData property of TElXMLEncryptor class.
If you are using the instance of TElXMLKeyInfoSymmetricData class for encryption of the encryption key (when TElXMLEncryptor.KeyEncryptionType property is set to xetKeyWrap), assign it to KeyEncryptionKeyData property.

C#:


  TElXMLEncryptor Encryptor = new TElXMLEncryptor(null);
  TElXMLKeyInfoSymmetricData SymKeyData = new TElXMLKeyInfoSymmetricData(true);
  try
  {
    Encryptor.EncryptionMethod = SBXMLSec.Unit.xemAES;

    // setup the key
    SymKeyData.Key.Key = AESKey;
    SymKeyData.Key.GenerateIV(16 * 8); // generate random IV (initialization vector)
    Encryptor.KeyData = SymKeyData;

    // encrypt the data
    Encryptor.Encrypt(...);
    ...
  }
  finally
  {
    Encryptor.Dispose();
    SymKeyData.Dispose();
  }
Delphi:

var
  Encryptor : TElXMLEncryptor;
  SymKeyData : TElXMLKeyInfoSymmetricData;
  ...
  Encryptor := TElXMLEncryptor.Create(nil);
  SymKeyData := TElXMLKeyInfoSymmetricData.Create(True);
  try
    Encryptor.EncryptionMethod := xemAES;

    // setup the key
    SymKeyData.Key.Key := AESKey;
    SymKeyData.Key.GenerateIV(16 * 8); // generate random IV (initialization vector)
    Encryptor.KeyData :=  SymKeyData;

    Encryptor.Encrypt(...);


  finally
    FreeAndNil(Encryptor);
    FreeAndNil(SymKeyData);
  end;
  ...

How To articles about XML encryption (XMLEnc)

Discuss this help topic in SecureBlackbox Forum