Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# - encrypt class structure into file.

Posted on 2013-06-23
3
687 Views
Last Modified: 2013-06-24
Currently, I write an instance of class Setups into a file using XML serialization.  And I will read the instance back when my application startup.  I use result of the  XML serialization as the configuration file of my application.

Now I would like to encrypt the result of the xml serialization and able to read it back into a class instance.

Currently.

Save configuration
class instance -->XML Serialization --> write XML file
Read configuraiont
Read XML File  --> XML Serialization --> class instance

Now I am hoping to incorporate encrypt/decrypt

Save configuration
class instance  -->XML Serialization -->  encrypt --> write XML file
Read configuraiont
Read XML File  --> decrypt --> XML Serialization --> class instance

Is this the right way to be expected or what is the right steps? And any good tutorial and examples for this type of operation?

Below is the class I try to do.

   [XmlRootAttribute("Setups", IsNullable = false)]
    public class Setups
    {
        public int TradeType;

        //[XmlArrayItem]
        [XmlElement("Setup")]
        public Setup[] Setup;
     }

    public class Setup
    {

        [XmlAttribute("entry")]         public int _entry;
        [XmlAttribute("type")]          public string _typeString;
        [XmlIgnore]                             public string _type;
       [XmlElement("Name")]         public string _name;
       [XmlElement("Entry")]           public string _entryName;
    }
0
Comment
Question by:tommym121
  • 2
3 Comments
 

Author Comment

by:tommym121
ID: 39269817
This is how I serialize the object po

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            TextWriter writer = new StreamWriter(filename);
            serializer.Serialize(writer, po);

It seems that if there is a some type of encryptStreamWriter, it will be perfect.
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 39271742
if you do encryption after serialization, the xml syntax also was encrypted and therefore the xml file could not be written. so, in my opinion you have two choices:

first, you do encryption before or while serialization such that only the contents were encrypted.

second, you encrypt the xml-file.

for the first, you best would need to "hook" into the serialization method such that the serialized strings could be encrypted (note, the encryption must produce 'printable' output).
see http://msdn.microsoft.com/en-us/library/zhwtaz29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 for sample code.

for the second, you would need to decrypt the file before reading it with xml. the decryption should be done in memory and xml reading should happen in memory as well. otherwise you would have a security leak cause the decrypted data could be read from anyone while the temporary file exists.

Sara
0
 

Author Closing Comment

by:tommym121
ID: 39273615
Thanks
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question