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

  • Home
  • SEARCH
  • 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 8719477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:48:28+00:00 2026-06-13T06:48:28+00:00

Afternoon, So I’m fairly new to all these encryption methods and usages. I’ve just

  • 0

Afternoon,

So I’m fairly new to all these encryption methods and usages. I’ve just started building an Encryption Implementation. Now My method of encryption and decryption seems to be working fine.. Till I read I failed horribly for using a Hardcoded IV in the RijndaelManaged class.

I’m reading everywhere that It’s better(and safer) to have each encrypted file’s IV be uniquely generated at “Encryption time” and saved at the front/beginning of the File where as the rest of encrypted bytes are saved directly after this.
(If I understand this correctly)

I’ve been having a real hard time figuring out my problem, because each time I Encrypt a file, I write the IV to the first 16 bytes(As the class, MSDN, Google and some folk on this forum suggested) of the file and directly after that I write the rest of the Encrypted bytes to the filestream. (The IV is saved in plaintext..Or well unencrypted bytes < Also is this okey?)

My problem is when I try to decrypt the file using precisely the same Key it fails: Before I start reading chunks of the file for decryption(Buffering) I try to read the File’s first 16bytes for the plaintexted IV and use it in the RijndaelManaged Class instance.

This seems to fail. I checked, I think I may be making some schoolboy error somewhere: Because I can’t seem to read the same 16 bytes from the Encrypted file once I wish to decrypt it.

This is the Code I use to do the File handling PRIOR to Encryption(Not the encryption it self)


using (RijndaelManaged RM = new RijndaelManaged())
using (FileStream StreamIN = new FileStream(FileIN, FileMode.Open, FileAccess.Read))
using (FileStream StreamOUT = new FileStream(FileOUT, FileMode.Append, FileAccess.Write))
{
    //Setup Variable ChunkSize and The Total bytes Read into the streams
    int ChunkSize = 1024 * 1024 * 32; 

    RM.GenerateIV();

    foreach (var item in RM.IV)
    {
        Console.Write("[ " + item + "] ");
    }

    StreamOUT.Seek(0, SeekOrigin.Begin);
    StreamOUT.Write(RM.IV, 0, RM.IV.Length);
    StreamOUT.Seek(RM.IV.Length, SeekOrigin.Begin);

    int TotalBytesRead = 0;                       

    //File Buffer Implementation
    for (long i = 0; i < StreamIN.Length; i += ChunkSize)
    {
        //ChunkData byte array determined by ChunkSize
        byte[] ChunkData = new byte[ChunkSize];
        //Current Bytes Read from the IN Stream
        int BytesRead = 0;
        while ((BytesRead = StreamIN.Read(ChunkData, 0, ChunkSize)) > 0)
        {
            //Call Encryption from local Class

            Console.WriteLine("Bytes Read: {0}", TotalBytesRead += BytesRead);

            ChunkData = Crypt.Encrypt(ChunkData, KeyBytes, RM.IV);

            //Write byte array to File
            StreamOUT.Write(ChunkData, 0, BytesRead);
        }
    }
}

And this is my code for File handling PRIOR to Decryption(Not Decryption itself)

using (RijndaelManaged RM = new RijndaelManaged())
using (FileStream StreamIN = new FileStream(FileIN, FileMode.Open, FileAccess.Read))
using (FileStream StreamOUT = new FileStream(FileOUT, FileMode.Append, FileAccess.Write))
{
    //Setup Variable ChunkSize and The Total bytes Read into the streams
    int ChunkSize = 1024 * 1024 * 32; 


    StreamIN.Seek(0, SeekOrigin.Begin);
    int Read = StreamIN.Read(RM.IV, 0, 16);
    StreamIN.Seek(RM.IV.Length, SeekOrigin.Begin);

    int TotalBytesRead = 0;

    foreach (var item in RM.IV)
    {
        Console.Write("[ " + item + "] ");
    }

    //File Buffer Implementation
    for (long i = 0; i < StreamIN.Length; i += ChunkSize)
    {
        //ChunkData byte array determined by ChunkSize
        byte[] ChunkData = new byte[ChunkSize];
        //Current Bytes Read from the IN Stream
        int BytesRead = 0;

        while ((BytesRead = StreamIN.Read(ChunkData, 0, ChunkSize)) > 0)
        {

            Console.WriteLine("Bytes Read: {0}", TotalBytesRead += BytesRead);                                

            //Call Decryption from local Class
            ChunkData = Crypt.Decrypt(ChunkData, KeyBytes, RM.IV);

            //Write byte Array to file
            StreamOUT.Write(ChunkData, 0, BytesRead);
        }
    }
}

I think that’s all of it? Also, the “Console” and other useless calls are only made to help me check and figure out the problem thusfar.
I’m using Visual Studios 2010, and trying to implement the RijndaelManaged class.

The Encryption/Decryption Methods are passed the FileChunks(byte array) with my Key and the IV and these return a byte array(Crypted) which is saved to the stream.

I Hope my question is clear? If not Ill try to explain better,
I’ve read everywhere looking for help on this particular problem but most other questions posted here are outside of my scope.

I really would appreciate any help, I’m sure I’m making a clown mistake somewhere..

Thank you!!

  • 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-13T06:48:30+00:00Added an answer on June 13, 2026 at 6:48 am

    As per long-standing Framework guidelines, it looks like SymmetricAlgorithm.IV has been written not to return an internal array. This means that it’s handing you back a copy, so subsequent changes to it aren’t reflected in the RijndaelManaged instance.

    // Effectively reading the persisted value into a temporary array
    // and throwing away the temporary.
    int Read = StreamIN.Read(RM.IV, 0, 16);
    

    In order to make the change stick and set the IV to the persisted value, you need to actually set the IV property explicitly, rather than just modifying the value it returned previously.

    byte[] persistedIv = new byte[16];
    StreamIN.Read(persistedIv, 0, persistedIv.Length);
    RM.IV = persistedIv;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Afternoon all. I'm building a web app and I'm attempting to pull through some
Good afternoon all, I am building a function that takes a string as input,
Afternoon all, I have a dataset with a variable called list_name. Some of these
Afternoon All, I have a web form that i wish my user to fill
Afternoon all. I have a drop down list in my view <div class=editor-label> @Html.LabelFor(model
Afternoon All, I have a web site where i need to know who is
Afternoon all, I tried to add a second data entity to the persistent store
Afternoon all. I have the following scenario: I have a search page where by
Afternoon all, I really need your help as this is a Nightmare! I was
Afternoon all - it's Friday th 13th so of course I am having an

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.