public: static void Encrypt(XmlDocument ^Doc, String ^ElementToEncrypt, String ^EncryptionElementID, RSA ^Alg, String ^KeyName)
{
// Check the arguments.
if (Doc == nullptr)
throw gcnew ArgumentNullException("Doc");
if (ElementToEncrypt == nullptr)
throw gcnew ArgumentNullException("ElementToEncrypt");
if (EncryptionElementID == nullptr)
throw gcnew ArgumentNullException("EncryptionElementID");
if (Alg == nullptr)
throw gcnew ArgumentNullException("Alg");
if (KeyName == nullptr)
throw gcnew ArgumentNullException("KeyName");
////////////////////////////////////////////////
// Find the specified element in the XmlDocument
// object and create a new XmlElemnt object.
////////////////////////////////////////////////
//XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;
XmlElement ^elementToEncrypt = dynamic_cast<XmlElement^>(Doc->GetElementsByTagName(ElementToEncrypt)[0]);
// Throw an XmlException if the element was not found.
if (elementToEncrypt == nullptr)
{
throw gcnew XmlException("The specified element was not found");
}
RijndaelManaged ^sessionKey = nullptr;
try
{
//////////////////////////////////////////////////
// Create a new instance of the EncryptedXml class
// and use it to encrypt the XmlElement with the
// a new random symmetric key.
//////////////////////////////////////////////////
// Create a 256 bit Rijndael key.
sessionKey = gcnew RijndaelManaged();
sessionKey->KeySize = 256;
EncryptedXml ^eXml = gcnew EncryptedXml();
array<System::Byte> ^encryptedElement = eXml->EncryptData(elementToEncrypt, sessionKey, false);
//byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);
////////////////////////////////////////////////
// Construct an EncryptedData object and populate
// it with the desired encryption information.
////////////////////////////////////////////////
EncryptedData ^edElement = gcnew EncryptedData();
edElement->Type = EncryptedXml::XmlEncElementUrl;
edElement->Id = EncryptionElementID;
// Create an EncryptionMethod element so that the
// receiver knows which algorithm to use for decryption.
edElement->EncryptionMethod = gcnew EncryptionMethod(EncryptedXml::XmlEncAES256Url);
// Encrypt the session key and add it to an EncryptedKey element.
EncryptedKey ^ek = gcnew EncryptedKey();
array<System::Byte> ^encryptedKey = EncryptedXml::EncryptKey(sessionKey->Key, Alg, false);
// byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);
ek->CipherData = gcnew CipherData(encryptedKey);
ek->EncryptionMethod = gcnew EncryptionMethod(EncryptedXml::XmlEncRSA15Url);
// Create a new DataReference element
// for the KeyInfo element. This optional
// element specifies which EncryptedData
// uses this key. An XML document can have
// multiple EncryptedData elements that use
// different keys.
DataReference ^dRef = gcnew DataReference();
// Specify the EncryptedData URI.
dRef->Uri = "#" + EncryptionElementID;
// Add the DataReference to the EncryptedKey.
ek->AddReference(dRef);
// Add the encrypted key to the
// EncryptedData object.
edElement->KeyInfo->AddClause(gcnew KeyInfoEncryptedKey(ek));
// Set the KeyInfo element to specify the
// name of the RSA key.
// Create a new KeyInfoName element.
KeyInfoName ^kin = gcnew KeyInfoName();
// Specify a name for the key.
kin->Value = KeyName;
// Add the KeyInfoName element to the
// EncryptedKey object.
ek->KeyInfo->AddClause(kin);
// Add the encrypted element data to the
// EncryptedData object.
edElement->CipherData->CipherValue = encryptedElement;
////////////////////////////////////////////////////
// Replace the element from the original XmlDocument
// object with the EncryptedData element.
////////////////////////////////////////////////////
EncryptedXml::ReplaceElement(elementToEncrypt, edElement, false);
}
catch (Exception ^e)
{
// re-throw the exception.
throw e;
}
finally
{
if (sessionKey != nullptr)
{
sessionKey->Clear();
}
}
}