Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# - encrypt class structure into file.

Posted on 2013-06-23
3
Medium Priority
?
706 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
[X]
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
  • 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 35

Accepted Solution

by:
sarabande earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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