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 3984162
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:44:12+00:00 2026-05-20T05:44:12+00:00

I’m quite new to C# (I’m using .NET 4.0) so please bear with me.

  • 0

I’m quite new to C# (I’m using .NET 4.0) so please bear with me. I need to save some object properties (their properties are int, double, String, boolean, datetime) to a file. But I want to encrypt the files using my own encryption, so I can’t use FileStream to convert to binary. Also I don’t want to use object serialization, because of performance issues. The idea is simple, first I need to somehow convert objects (their properties) to binary (array), then encrypt (some sort of xor) the array and append it to the end of the file. When reading first decrypt the array and then somehow convert the binary array back to object properties (from which I’ll generate objects).

I know (roughly =) ) how to convert these things by hand and I could code it, but it would be useless (too slow). I think the best way would be just to get properties’ representation in memory and save that. But I don’t know how to do it using C# (maybe using pointers?). Also I though about using MemoryStream but again I think it would be inefficient. I am thinking about class Converter, but it does not support toByte(datetime) (documentation says it always throws exception).

For converting back I think the only options is class Converter.

Note: I know the structure of objects and they will not change, also the maximum String length is also known.

Thank you for all your ideas and time.

EDIT: I will be storing only parts of objects, in some cases also parts of different objects (a couple of properties from one object type and a couple from another), thus I think that serialization is not an option for me.

  • 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-05-20T05:44:12+00:00Added an answer on May 20, 2026 at 5:44 am

    There are many different ways to do this. Perhaps the easiest, and performance should be adequate, would be to write each record to a MemoryStream, get the resulting array of bytes, do your encryption on that array, and then write the array to file using FileStream and a BinaryWriter.

    FileStream fs = new FileStream(...);
    BinaryWriter fileWriter = new BinaryWriter(fs);
    
    // Allocate byte array big enough to hold the longest record
    byte[] RecordBuffer = new byte[MaxRecordSize];
    int recordLength;  // to get the number of bytes in a record.
    
    // for each record
    using (var ms = new MemoryStream())
    {
        using (var memWriter = new BinaryWriter(ms, Encoding.UTF8))
        {
            // write things to the memory stream
            memWriter.Write(myStringValue);
            memWriter.Write(myLongValue);
            // ... etc.
            // Now get the number of bytes written
            memWriter.Flush();
            recordLength = memWriter.Position;
        }
    }
    
    // The record is now serialized in RecordBuffer, from 0 to recordLength-1
    // Do your encryption.
    // Then write a count of bytes, and the buffer:
    fileWriter.Write(recordLength);
    fileWriter.Write(RecordBuffer, 0, recordLength);
    

    Reading that back in is quite easy. You set up a BinaryReader on the file, call ReadInt32 to get the record length, call Read(RecordBuffer, 0, length) to get the record bytes, do your decryption, and then use a MemoryStream to de-serialize.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.