Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Initializing Byte Array, IndexOutOfRange Exception

Initializing Byte Array, IndexOutOfRange Exception

Scheduled Pinned Locked Moved C#
helpdatabasedata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ian Uy
    wrote on last edited by
    #1

    Good Day, I'm working with the AES Decryptor (using RijndaelManaged), but when I try to initialize a byte[] array to store my ciphertext data, it throws an IndexOutOfRange exception.

                using (FileStream Ciphertext = new FileStream(\_InputPath, FileMode.Open, FileAccess.Read, FileShare.None)) //For Plaintext Stream
                {
                    using (FileStream PlainText = new FileStream(\_OutputPath, FileMode.Create, FileAccess.Write, FileShare.None)) //For Ciphertext Stream
                    {
                        using (CryptoStream CStreamWriter = new CryptoStream(PlainText, Decryptor, CryptoStreamMode.Write)) //For CryptoStream Writer
                        {
                            byte\[\] CTextData = new byte\[Ciphertext.Length\]; //EXCEPTION THROWN HERE
    

    The exact error message is: "Index was outside the bounds of the array." The weird thing is, this only happens for file > 200MB. But for a 150mb file, I have no problem. Please advice thanks!

    It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

    G 1 Reply Last reply
    0
    • I Ian Uy

      Good Day, I'm working with the AES Decryptor (using RijndaelManaged), but when I try to initialize a byte[] array to store my ciphertext data, it throws an IndexOutOfRange exception.

                  using (FileStream Ciphertext = new FileStream(\_InputPath, FileMode.Open, FileAccess.Read, FileShare.None)) //For Plaintext Stream
                  {
                      using (FileStream PlainText = new FileStream(\_OutputPath, FileMode.Create, FileAccess.Write, FileShare.None)) //For Ciphertext Stream
                      {
                          using (CryptoStream CStreamWriter = new CryptoStream(PlainText, Decryptor, CryptoStreamMode.Write)) //For CryptoStream Writer
                          {
                              byte\[\] CTextData = new byte\[Ciphertext.Length\]; //EXCEPTION THROWN HERE
      

      The exact error message is: "Index was outside the bounds of the array." The weird thing is, this only happens for file > 200MB. But for a 150mb file, I have no problem. Please advice thanks!

      It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Perhaps because the system is unable to allocate an array that large. Don't read the entire file at once, there is no reason to have all the data in memory at the same time. Create a buffer and read only a small part of the file each time.

      Despite everything, the person most likely to be fooling you next is yourself.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups