program when decrypting XML - Unable to retrieve the decryption key

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elliot

    program when decrypting XML - Unable to retrieve the decryption key

    When decrypt the xml, output "Unable to retrieve the decryption key."
    Can anyone help me solve the problem?

    I got the code from http://msdn.microsoft.com/en-us/library/ms229746.aspx


    using System;
    using System.Xml;
    using System.Security .Cryptography;
    using System.Security .Cryptography.X ml;

    class Program
    {
    static void Main(string[] args)
    {
    // Create an XmlDocument object.
    XmlDocument xmlDoc = new XmlDocument();

    // Load an XML file into the XmlDocument object.
    try
    {
    xmlDoc.Preserve Whitespace = true;
    xmlDoc.Load("te st.xml");
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }

    // Create a new CspParameters object to specify
    // a key container.
    CspParameters cspParams = new CspParameters() ;
    cspParams.KeyCo ntainerName = "XML_ENC_RSA_KE Y";

    // Create a new RSA key and save it in the container. This key will
    encrypt
    // a symmetric key, which will then be encryped in the XML document.
    RSACryptoServic eProvider rsaKey = new
    RSACryptoServic eProvider(cspPa rams);

    try
    {
    // Encrypt the "creditcard " element.
    Encrypt(xmlDoc, "creditcard ", "EncryptedEleme nt1", rsaKey,
    "rsaKey");


    // Save the XML document.
    xmlDoc.Save("te st_Encrypted.xm l");

    // Display the encrypted XML to the console.
    Console.WriteLi ne("Encrypted XML:");
    Console.WriteLi ne();
    Console.WriteLi ne(xmlDoc.Outer Xml);

    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }
    finally
    {
    // Clear the RSA key.
    rsaKey.Clear();
    }


    // Create an XmlDocument object.
    xmlDoc = new XmlDocument();

    // Load an XML file into the XmlDocument object.
    try
    {
    xmlDoc.Preserve Whitespace = true;
    xmlDoc.Load("te st_Encrypted.xm l");
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }
    cspParams = new CspParameters() ;
    cspParams.KeyCo ntainerName = "XML_ENC_RSA_KE Y";

    // Get the RSA key from the key container. This key will decrypt
    // a symmetric key that was imbedded in the XML document.
    rsaKey = new RSACryptoServic eProvider(cspPa rams);

    try
    {

    // Decrypt the elements.
    Decrypt(xmlDoc, rsaKey, "rsaKey");


    // Display the encrypted XML to the console.
    Console.WriteLi ne();
    Console.WriteLi ne("Decrypted XML:");
    Console.WriteLi ne();
    Console.WriteLi ne(xmlDoc.Outer Xml);
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }
    finally
    {
    // Clear the RSA key.
    rsaKey.Clear();
    }

    Console.ReadLin e();
    }

    public static void Encrypt(XmlDocu ment Doc, string ElementToEncryp t,
    string EncryptionEleme ntID, RSA Alg, string KeyName)
    {
    // Check the arguments.
    if (Doc == null)
    throw new ArgumentNullExc eption("Doc");
    if (ElementToEncry pt == null)
    throw new ArgumentNullExc eption("Element ToEncrypt");
    if (EncryptionElem entID == null)
    throw new ArgumentNullExc eption("Encrypt ionElementID");
    if (Alg == null)
    throw new ArgumentNullExc eption("Alg");
    if (KeyName == null)
    throw new ArgumentNullExc eption("KeyName ");

    ////////////////////////////////////////////////
    // Find the specified element in the XmlDocument
    // object and create a new XmlElemnt object.
    ////////////////////////////////////////////////
    XmlElement elementToEncryp t =
    Doc.GetElements ByTagName(Eleme ntToEncrypt)[0] as XmlElement;

    // Throw an XmlException if the element was not found.
    if (elementToEncry pt == null)
    {
    throw new XmlException("T he specified element was not found");

    }
    RijndaelManaged sessionKey = null;

    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 = new RijndaelManaged ();
    sessionKey.KeyS ize = 256;

    EncryptedXml eXml = new EncryptedXml();

    byte[] encryptedElemen t = eXml.EncryptDat a(elementToEncr ypt,
    sessionKey, false);
    ////////////////////////////////////////////////
    // Construct an EncryptedData object and populate
    // it with the desired encryption information.
    ////////////////////////////////////////////////

    EncryptedData edElement = new EncryptedData() ;
    edElement.Type = EncryptedXml.Xm lEncElementUrl;
    edElement.Id = EncryptionEleme ntID;
    // Create an EncryptionMetho d element so that the
    // receiver knows which algorithm to use for decryption.

    edElement.Encry ptionMethod = new
    EncryptionMetho d(EncryptedXml. XmlEncAES256Url );
    // Encrypt the session key and add it to an EncryptedKey
    element.
    EncryptedKey ek = new EncryptedKey();

    byte[] encryptedKey = EncryptedXml.En cryptKey(sessio nKey.Key,
    Alg, false);

    ek.CipherData = new CipherData(encr yptedKey);

    ek.EncryptionMe thod = new
    EncryptionMetho d(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 = new DataReference() ;

    // Specify the EncryptedData URI.
    dRef.Uri = "#" + EncryptionEleme ntID;

    // Add the DataReference to the EncryptedKey.
    ek.AddReference (dRef);
    // Add the encrypted key to the
    // EncryptedData object.

    edElement.KeyIn fo.AddClause(ne w KeyInfoEncrypte dKey(ek));
    // Set the KeyInfo element to specify the
    // name of the RSA key.

    // Create a new KeyInfo element.
    edElement.KeyIn fo = new KeyInfo();

    // Create a new KeyInfoName element.
    KeyInfoName kin = new KeyInfoName();

    // Specify a name for the key.
    kin.Value = KeyName;

    // Add the KeyInfoName element to the
    // EncryptedKey object.
    ek.KeyInfo.AddC lause(kin);
    // Add the encrypted element data to the
    // EncryptedData object.
    edElement.Ciphe rData.CipherVal ue = encryptedElemen t;
    ////////////////////////////////////////////////////
    // Replace the element from the original XmlDocument
    // object with the EncryptedData element.
    ////////////////////////////////////////////////////
    EncryptedXml.Re placeElement(el ementToEncrypt, edElement, false);
    }
    catch (Exception e)
    {
    // re-throw the exception.
    throw e;
    }
    finally
    {
    if (sessionKey != null)
    {
    sessionKey.Clea r();
    }

    }

    }


    public static void Decrypt(XmlDocu ment Doc, RSA Alg, string KeyName)
    {
    // Check the arguments.
    if (Doc == null)
    throw new ArgumentNullExc eption("Doc");
    if (Alg == null)
    throw new ArgumentNullExc eption("Alg");
    if (KeyName == null)
    throw new ArgumentNullExc eption("KeyName ");
    // Create a new EncryptedXml object.
    EncryptedXml exml = new EncryptedXml(Do c);

    // Add a key-name mapping.
    // This method can only decrypt documents
    // that present the specified key name.
    exml.AddKeyName Mapping(KeyName , Alg);

    // Decrypt the element.
    exml.DecryptDoc ument();

    }

    }


  • Elliot

    #2
    Re: program when decrypting XML - Unable to retrieve the decryption key

    Any idea?


    "Elliot" <elliot_barclay @hotmail.co.ukw rote in message
    news:EF147447-E721-4E78-886F-7BAA2E27FF6E@mi crosoft.com...
    When decrypt the xml, output "Unable to retrieve the decryption key."
    Can anyone help me solve the problem?
    >
    I got the code from http://msdn.microsoft.com/en-us/library/ms229746.aspx
    >
    >
    using System;
    using System.Xml;
    using System.Security .Cryptography;
    using System.Security .Cryptography.X ml;
    >
    class Program
    {
    static void Main(string[] args)
    {
    // Create an XmlDocument object.
    XmlDocument xmlDoc = new XmlDocument();
    >
    // Load an XML file into the XmlDocument object.
    try
    {
    xmlDoc.Preserve Whitespace = true;
    xmlDoc.Load("te st.xml");
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }
    >
    // Create a new CspParameters object to specify
    // a key container.
    CspParameters cspParams = new CspParameters() ;
    cspParams.KeyCo ntainerName = "XML_ENC_RSA_KE Y";
    >
    // Create a new RSA key and save it in the container. This key
    will encrypt
    // a symmetric key, which will then be encryped in the XML
    document.
    RSACryptoServic eProvider rsaKey = new
    RSACryptoServic eProvider(cspPa rams);
    >
    try
    {
    // Encrypt the "creditcard " element.
    Encrypt(xmlDoc, "creditcard ", "EncryptedEleme nt1", rsaKey,
    "rsaKey");
    >
    >
    // Save the XML document.
    xmlDoc.Save("te st_Encrypted.xm l");
    >
    // Display the encrypted XML to the console.
    Console.WriteLi ne("Encrypted XML:");
    Console.WriteLi ne();
    Console.WriteLi ne(xmlDoc.Outer Xml);
    >
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }
    finally
    {
    // Clear the RSA key.
    rsaKey.Clear();
    }
    >
    >
    // Create an XmlDocument object.
    xmlDoc = new XmlDocument();
    >
    // Load an XML file into the XmlDocument object.
    try
    {
    xmlDoc.Preserve Whitespace = true;
    xmlDoc.Load("te st_Encrypted.xm l");
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }
    cspParams = new CspParameters() ;
    cspParams.KeyCo ntainerName = "XML_ENC_RSA_KE Y";
    >
    // Get the RSA key from the key container. This key will decrypt
    // a symmetric key that was imbedded in the XML document.
    rsaKey = new RSACryptoServic eProvider(cspPa rams);
    >
    try
    {
    >
    // Decrypt the elements.
    Decrypt(xmlDoc, rsaKey, "rsaKey");
    >
    >
    // Display the encrypted XML to the console.
    Console.WriteLi ne();
    Console.WriteLi ne("Decrypted XML:");
    Console.WriteLi ne();
    Console.WriteLi ne(xmlDoc.Outer Xml);
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }
    finally
    {
    // Clear the RSA key.
    rsaKey.Clear();
    }
    >
    Console.ReadLin e();
    }
    >
    public static void Encrypt(XmlDocu ment Doc, string ElementToEncryp t,
    string EncryptionEleme ntID, RSA Alg, string KeyName)
    {
    // Check the arguments.
    if (Doc == null)
    throw new ArgumentNullExc eption("Doc");
    if (ElementToEncry pt == null)
    throw new ArgumentNullExc eption("Element ToEncrypt");
    if (EncryptionElem entID == null)
    throw new ArgumentNullExc eption("Encrypt ionElementID");
    if (Alg == null)
    throw new ArgumentNullExc eption("Alg");
    if (KeyName == null)
    throw new ArgumentNullExc eption("KeyName ");
    >
    ////////////////////////////////////////////////
    // Find the specified element in the XmlDocument
    // object and create a new XmlElemnt object.
    ////////////////////////////////////////////////
    XmlElement elementToEncryp t =
    Doc.GetElements ByTagName(Eleme ntToEncrypt)[0] as XmlElement;
    >
    // Throw an XmlException if the element was not found.
    if (elementToEncry pt == null)
    {
    throw new XmlException("T he specified element was not found");
    >
    }
    RijndaelManaged sessionKey = null;
    >
    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 = new RijndaelManaged ();
    sessionKey.KeyS ize = 256;
    >
    EncryptedXml eXml = new EncryptedXml();
    >
    byte[] encryptedElemen t = eXml.EncryptDat a(elementToEncr ypt,
    sessionKey, false);
    ////////////////////////////////////////////////
    // Construct an EncryptedData object and populate
    // it with the desired encryption information.
    ////////////////////////////////////////////////
    >
    EncryptedData edElement = new EncryptedData() ;
    edElement.Type = EncryptedXml.Xm lEncElementUrl;
    edElement.Id = EncryptionEleme ntID;
    // Create an EncryptionMetho d element so that the
    // receiver knows which algorithm to use for decryption.
    >
    edElement.Encry ptionMethod = new
    EncryptionMetho d(EncryptedXml. XmlEncAES256Url );
    // Encrypt the session key and add it to an EncryptedKey
    element.
    EncryptedKey ek = new EncryptedKey();
    >
    byte[] encryptedKey = EncryptedXml.En cryptKey(sessio nKey.Key,
    Alg, false);
    >
    ek.CipherData = new CipherData(encr yptedKey);
    >
    ek.EncryptionMe thod = new
    EncryptionMetho d(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 = new DataReference() ;
    >
    // Specify the EncryptedData URI.
    dRef.Uri = "#" + EncryptionEleme ntID;
    >
    // Add the DataReference to the EncryptedKey.
    ek.AddReference (dRef);
    // Add the encrypted key to the
    // EncryptedData object.
    >
    edElement.KeyIn fo.AddClause(ne w KeyInfoEncrypte dKey(ek));
    // Set the KeyInfo element to specify the
    // name of the RSA key.
    >
    // Create a new KeyInfo element.
    edElement.KeyIn fo = new KeyInfo();
    >
    // Create a new KeyInfoName element.
    KeyInfoName kin = new KeyInfoName();
    >
    // Specify a name for the key.
    kin.Value = KeyName;
    >
    // Add the KeyInfoName element to the
    // EncryptedKey object.
    ek.KeyInfo.AddC lause(kin);
    // Add the encrypted element data to the
    // EncryptedData object.
    edElement.Ciphe rData.CipherVal ue = encryptedElemen t;
    ////////////////////////////////////////////////////
    // Replace the element from the original XmlDocument
    // object with the EncryptedData element.
    ////////////////////////////////////////////////////
    EncryptedXml.Re placeElement(el ementToEncrypt, edElement,
    false);
    }
    catch (Exception e)
    {
    // re-throw the exception.
    throw e;
    }
    finally
    {
    if (sessionKey != null)
    {
    sessionKey.Clea r();
    }
    >
    }
    >
    }
    >
    >
    public static void Decrypt(XmlDocu ment Doc, RSA Alg, string KeyName)
    {
    // Check the arguments.
    if (Doc == null)
    throw new ArgumentNullExc eption("Doc");
    if (Alg == null)
    throw new ArgumentNullExc eption("Alg");
    if (KeyName == null)
    throw new ArgumentNullExc eption("KeyName ");
    // Create a new EncryptedXml object.
    EncryptedXml exml = new EncryptedXml(Do c);
    >
    // Add a key-name mapping.
    // This method can only decrypt documents
    // that present the specified key name.
    exml.AddKeyName Mapping(KeyName , Alg);
    >
    // Decrypt the element.
    exml.DecryptDoc ument();
    >
    }
    >
    }
    >
    >

    Comment

    Working...