Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8241835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:57:50+00:00 2026-06-07T20:57:50+00:00

I can read an write my serialized class fine until I try to encrypt

  • 0

I can read an write my serialized class fine until I try to encrypt / decrypt… here is a snippet of my code:

public class ShelfCache
{
    public Shelf Data;
    public ShelfCache()
    {
        Data = new Shelf();
    }

    public void Write(string Filename)
    {
        XmlSerializer xsl = new XmlSerializer(typeof(Shelf));
        TextWriter xslWriter = new StreamWriter(Filename);
        xsl.Serialize(xslWriter, Data);
        xslWriter.Flush();
        xslWriter.Close();
    }

    public void Read(string Filename)
    {
        Data = new Shelf();
        XmlSerializer xsl = new XmlSerializer(typeof(Shelf));
        TextReader xslReader = new StreamReader(Filename);
        Data = (Shelf)xsl.Deserialize(xslReader);
        xslReader.Close();
    }

    public void WriteEncrypted(string Filename, string EncryptionKey = "")
    {
        string _Key = EncryptionKey + Environment.ExpandEnvironmentVariables("%USERNAME%%COMPUTERNAME%123456789ABCDEF0123456789abcdef").Substring(0, 32);
        string _IV = Environment.ExpandEnvironmentVariables("%COMPUTERNAME%123456789abcdef").Substring(0, 16);
        byte[] Key = Encoding.UTF8.GetBytes(_Key);
        byte[] IV = Encoding.UTF8.GetBytes(_IV);

        FileStream CacheStream = new FileStream(Filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
        RijndaelManaged CryptoProvider = new RijndaelManaged();
        ICryptoTransform CacheTransform = CryptoProvider.CreateEncryptor(Key, IV);
        CryptoStream EncryptionStream = new CryptoStream(CacheStream, CacheTransform, CryptoStreamMode.Write);
        XmlSerializer xsl = new XmlSerializer(typeof(Shelf));
        xsl.Serialize(EncryptionStream, Data);
        EncryptionStream.Flush();
        CacheStream.Close();
    }

    public void ReadEncrypted(string Filename, string EncryptionKey = "")
    {
        string _Key = EncryptionKey + Environment.ExpandEnvironmentVariables("%USERNAME%%COMPUTERNAME%123456789ABCDEF0123456789abcdef").Substring(0, 32);
        string _IV = Environment.ExpandEnvironmentVariables("%COMPUTERNAME%123456789abcdef").Substring(0, 16);
        byte[] Key = Encoding.UTF8.GetBytes(_Key);
        byte[] IV = Encoding.UTF8.GetBytes(_IV);

        FileStream CacheStream = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        RijndaelManaged CryptoProvider = new RijndaelManaged();
        ICryptoTransform CacheTransform = CryptoProvider.CreateEncryptor(Key, IV);
        CryptoStream DecryptionStream = new CryptoStream(CacheStream, CacheTransform, CryptoStreamMode.Read);
        XmlSerializer xsl = new XmlSerializer(typeof(Shelf));
        Data = (Shelf)xsl.Deserialize(DecryptionStream);
        CacheStream.Close();
    }

}

I get the exception “There is an error in XML document (1 1).” with an inner exception “Data at the root level is invalid. Line 1, position 1.” on the line:

        Data = (Shelf)xsl.Deserialize(DecryptionStream);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:57:52+00:00Added an answer on June 7, 2026 at 8:57 pm

    This is what I ended up doing (I don’t like the solution, but it works)…

    public class ShelfCache
    {
        public Shelf Data;
    
        public ShelfCache()
        {
            Data = new Shelf();
        }
    
        public void Write(string Filename)
        {           
            XmlSerializer CacheSerializer = new XmlSerializer(typeof(Shelf));
            TextWriter CacheWriter = new StreamWriter(Filename);
            CacheSerializer.Serialize(CacheWriter, Data);
            CacheWriter.Flush();
            CacheWriter.Close();
        }
    
        public void Read(string Filename)
        {
            Data = new Shelf();
            XmlSerializer CacheSerializer = new XmlSerializer(typeof(Shelf));
            TextReader CacheReader = new StreamReader(Filename);
            Data = (Shelf)CacheSerializer.Deserialize(CacheReader);
            CacheReader.Close();
        }
    
        public void WriteEncrypted(string Filename, string EncryptionKey = "")
        {
            string TempFile = System.IO.Path.GetTempFileName();
            Write(TempFile);
            EncryptFile(TempFile, Filename, EncryptionKey);
            File.Delete(TempFile);
        }
    
        public void ReadEncrypted(string Filename, string EncryptionKey = "")
        {
            string TempFile = System.IO.Path.GetTempFileName();
            DecryptFile(Filename, TempFile, EncryptionKey);
            Read(TempFile);
            File.Delete(TempFile);
        }
    
        private RijndaelManaged GetEncryptor(string Key = "", string Salt = "")
        {
            Key += Environment.ExpandEnvironmentVariables("%USERNAME%");
            Salt += Environment.ExpandEnvironmentVariables("%COMPUTERNAME%");
            Rfc2898DeriveBytes derivedKey = new Rfc2898DeriveBytes(Key, Encoding.Unicode.GetBytes(Salt));
            RijndaelManaged rijndaelCSP = new RijndaelManaged();
            rijndaelCSP.Key = derivedKey.GetBytes(rijndaelCSP.KeySize / 8);
            rijndaelCSP.IV = derivedKey.GetBytes(rijndaelCSP.BlockSize / 8);
            return rijndaelCSP;
        }
    
        private void EncryptFile(string Source, string Target, string EncryptionKey)
        {
            RijndaelManaged EncryptionProvider = GetEncryptor(EncryptionKey);
            ICryptoTransform Encryptor = EncryptionProvider.CreateEncryptor();
    
            FileStream SourceStream = new FileStream(Source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            byte[] SourceBytes = new byte[(int)SourceStream.Length];
            SourceStream.Read(SourceBytes, 0, (int)SourceStream.Length);
            FileStream TargetStream = new FileStream(Target, FileMode.Create, FileAccess.Write);
            CryptoStream EncryptionStream = new CryptoStream(TargetStream, Encryptor, CryptoStreamMode.Write);
            EncryptionStream.Write(SourceBytes, 0, (int)SourceStream.Length);
            EncryptionStream.FlushFinalBlock();
    
            EncryptionProvider.Clear();
            EncryptionStream.Close();
            SourceStream.Close();
            TargetStream.Close();
        }
    
        private void DecryptFile(string Source, string Target, string EncryptionKey)
        {
            RijndaelManaged EncryptionProvider = GetEncryptor(EncryptionKey);
            ICryptoTransform Decryptor = EncryptionProvider.CreateDecryptor();
    
            FileStream SourceStream = new FileStream(Source, FileMode.Open, FileAccess.Read);
            CryptoStream DecryptionStream = new CryptoStream(SourceStream, Decryptor, CryptoStreamMode.Read);
            byte[] SourceBytes = new byte[(int)SourceStream.Length];
            int DecryptionLength = DecryptionStream.Read(SourceBytes, 0, (int)SourceStream.Length);
            FileStream TargetStream = new FileStream(Target, FileMode.Create, FileAccess.Write);
            TargetStream.Write(SourceBytes, 0, DecryptionLength);
            TargetStream.Flush();
    
            EncryptionProvider.Clear();
            DecryptionStream.Close();
            SourceStream.Close();
            TargetStream.Close();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing in python a file class that can read and write a
I want to write the code, that can deserialize the class even if the
Is there any class in the .NET framework that can read/write standard .ini files:
I created a SQLite database on Android device. The program can read/write to database
I'm looking for an editor that can read and write remote PHP files via
How can I read and write comments in a Zip file, using Java? I
With D and Tango library can I read and write in the ANSI encoding
How can I read from and write to my Galaxy Nexus phone, using MTP
How can i check the read/ write permission of the file storing media? ie
Can anybody give any working example of how to read/write Unicode text files using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.