Solved

C# - encrypt class structure into file.

Posted on 2013-06-23
3
671 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 32

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now