Results 1 to 2 of 2

Thread: Asymmetric Decryption Problem

  1. #1
    Join Date
    Sep 2010
    Posts
    1

    Asymmetric Decryption Problem

    Hi,

    I have written some code that has encrypted an XML config file containing user credentials, and also the code to decrypt that file. When I run the encryption and decryption together on my local machine it works as expected. However, when I deploy the program, with only the decrypt code, the xml file will not decrypt. I get a cryptographic Exception: Bad Data?
    Here is my code:


    Code:
    public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, string EncryptionElementID, RSA Alg, string Keyname)
            {
                if (Doc == null)
                    throw new ArgumentNullException("Doc");
                if (ElementToEncrypt == null)
                    throw new ArgumentNullException("Element to Encrypt");
                if (EncryptionElementID == null)
                    throw new ArgumentNullException("EncryptionElementID");
                if (Alg == null)
                    throw new ArgumentNullException("ALG");
                //specify which xml elements to encrypt
                XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;
    
                if (elementToEncrypt == null)
                    throw new XmlException("The specified element was not found");
                try
                {
                    //create session key
                    RijndaelManaged sessionkey = new RijndaelManaged();
                    sessionkey.KeySize = 256;
    
                    //encrypt using Encrypted exml object and hold in byte array
                    EncryptedXml exml = new EncryptedXml();
                    byte[] encryptedElement = exml.EncryptData(elementToEncrypt, sessionkey, false);
    
                    //Construct an EncryptedData object and populate
                    // it with the desired encryption information.
    
                    EncryptedData edElement = new EncryptedData();
                    edElement.Type = EncryptedXml.XmlEncElementUrl;
                    edElement.Id = EncryptionElementID;
    
                    edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
                    //encrypt the session key and add it encrypted key element
                    EncryptedKey ek = new EncryptedKey();
    
                    byte[] encryptedKey = EncryptedXml.EncryptKey(sessionkey.Key, Alg, false);
    
                    ek.CipherData = new CipherData(encryptedKey);
                    ek.EncryptionMethod = new 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 = new DataReference();
    
                    // Specify the EncryptedData URI.
                    dRef.Uri = "#" + EncryptionElementID;
    
    
                   //add data reference to encrypted key
    
                    ek.AddReference(dRef);
                    //Add the encrypted key to the
                    // EncryptedData object.
    
                    edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
    
                 // Create a new KeyInfoName element.
                KeyInfoName kin = new KeyInfoName();
    
               
    
                // 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)
                {
                    throw e;
                }
            }
    
    
            public static string Decrypt()
            {
                    //create XML documentobject and load config file
                    XmlDocument xmlDoc = new XmlDocument();
    
                    try
                    {
                        xmlDoc.Load("config.xml");
                    }
                    catch (FileNotFoundException e)
                    {
                        Console.WriteLine(e.Message);
                        Console.ReadLine();
    
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.ReadLine();
                    }
    
                    //create container for key
                    CspParameters cspParam = new CspParameters();
                    cspParam.KeyContainerName = "XML_RSA_FTP_KEY";
                    cspParam.Flags = CspProviderFlags.UseMachineKeyStore;
                    //create key and store in container
                    RSACryptoServiceProvider ftpkey = new RSACryptoServiceProvider(cspParam);
                  
    
                    //add keyname mapping qnd decrypt the document
                    EncryptedXml exml = new EncryptedXml(xmlDoc);
                    exml.AddKeyNameMapping("ftpkey", ftpkey);
                    exml.DecryptDocument();
                    
                    //pass decrypted document to extract credentials method
                    string details =  Extract_Credentials(xmlDoc);
    
                    //return decrypted log in details
                    return details;
    
            }
    Any help would be appreciated. Thanks, Darren.

  2. #2
    Join Date
    Feb 2010
    Posts
    644

    Re: Asymmetric Decryption Problem

    That shows decrypting, but you necessitate to also illustrating how you encrypted the data to start with. If you could construct a single short but complete program which demonstrates that, so that we could reproduce it, that would be immense.

Similar Threads

  1. DES encryption and decryption using C or Java
    By sayanmaji in forum Software Development
    Replies: 5
    Last Post: 26-11-2010, 03:20 AM
  2. XML encryption and decryption
    By Anirvinya in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 03:31 AM
  3. RSA decryption problem in C#
    By taher in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:30 PM
  4. Do you know difference between encryption and decryption?
    By rooki in forum Networking & Security
    Replies: 4
    Last Post: 30-11-2009, 12:24 PM
  5. Encryption in JavaScript, Decryption in PHP
    By Rail racer in forum Software Development
    Replies: 6
    Last Post: 11-10-2008, 03:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,631,853.01597 seconds with 16 queries