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. ManagedRijndael Out of Memory Exception

ManagedRijndael Out of Memory Exception

Scheduled Pinned Locked Moved C#
data-structuressecurityperformancehelpquestion
5 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

    byte[] CryptographicKey = Convert.FromBase64String(txtCK.Text); byte[] IVector = new byte[16]; //16 bytes of IV Array.Copy(CryptographicKey, IVector, 16); //Get first 128-bits of CK as IV. //Set-up Rijndael as AES256 RijndaelManaged AES256 = new RijndaelManaged(); AES256.KeySize = 256; AES256.Mode = CipherMode.CBC; ICryptoTransform Encryptor = AES256.CreateEncryptor(CryptographicKey, IVector); //Prepare Writer for CipherText Ciphertext = new FileStream(txtOutputPath.Text, FileMode.Create, FileAccess.Write, FileShare.None); CryptoStream CStreamWriter = new CryptoStream(Ciphertext, Encryptor, CryptoStreamMode.Write); //Start the Encryption process CStreamWriter.Write(Plaintext, 0, Plaintext.Length); //OUT OF MEMORY EXCEPTION HERE CStreamWriter.FlushFinalBlock(); Ciphertext.Close(); CStreamWriter.Close(); CStreamWriter.Write(Plaintext, 0, Plaintext.Length); //OUT OF MEMORY EXCEPTION HERE The code works fine for files smaller than 150mb, but when I tried to encrypt a 300mb file. I got that exception. I have 3gb of ram and when I started the encryption, my usage is only at 30%. What seems to be the problem? :^)

    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.

    N 1 Reply Last reply
    0
    • I Ian Uy

      byte[] CryptographicKey = Convert.FromBase64String(txtCK.Text); byte[] IVector = new byte[16]; //16 bytes of IV Array.Copy(CryptographicKey, IVector, 16); //Get first 128-bits of CK as IV. //Set-up Rijndael as AES256 RijndaelManaged AES256 = new RijndaelManaged(); AES256.KeySize = 256; AES256.Mode = CipherMode.CBC; ICryptoTransform Encryptor = AES256.CreateEncryptor(CryptographicKey, IVector); //Prepare Writer for CipherText Ciphertext = new FileStream(txtOutputPath.Text, FileMode.Create, FileAccess.Write, FileShare.None); CryptoStream CStreamWriter = new CryptoStream(Ciphertext, Encryptor, CryptoStreamMode.Write); //Start the Encryption process CStreamWriter.Write(Plaintext, 0, Plaintext.Length); //OUT OF MEMORY EXCEPTION HERE CStreamWriter.FlushFinalBlock(); Ciphertext.Close(); CStreamWriter.Close(); CStreamWriter.Write(Plaintext, 0, Plaintext.Length); //OUT OF MEMORY EXCEPTION HERE The code works fine for files smaller than 150mb, but when I tried to encrypt a 300mb file. I got that exception. I have 3gb of ram and when I started the encryption, my usage is only at 30%. What seems to be the problem? :^)

      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.

      N Offline
      N Offline
      natsuyaki
      wrote on last edited by
      #2

      Allocating a memory block of 300M at a time is not a good idea... Try to split the file into smaller pieces and then encrypt.

      I 1 Reply Last reply
      0
      • N natsuyaki

        Allocating a memory block of 300M at a time is not a good idea... Try to split the file into smaller pieces and then encrypt.

        I Offline
        I Offline
        Ian Uy
        wrote on last edited by
        #3

        Will the output still be the same?

        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.

        N 1 Reply Last reply
        0
        • I Ian Uy

          Will the output still be the same?

          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.

          N Offline
          N Offline
          natsuyaki
          wrote on last edited by
          #4

          Yes, if each piece is encrypted with the same key and IV. And remember to use the same size to decrypt. Sample: //Start the Encryption process FileStream fs = new FileStream(f, FileMode.Open); byte[] data=new byte[1024]; while (true) { int i = fs.Read(data, 0, 1024); if (i <= 0) break; else { CStreamWriter.Write(data, 0, 1024); //OUT OF MEMORY EXCEPTION HERE CStreamWriter.FlushFinalBlock(); } }

          modified on Sunday, May 11, 2008 7:19 AM

          I 1 Reply Last reply
          0
          • N natsuyaki

            Yes, if each piece is encrypted with the same key and IV. And remember to use the same size to decrypt. Sample: //Start the Encryption process FileStream fs = new FileStream(f, FileMode.Open); byte[] data=new byte[1024]; while (true) { int i = fs.Read(data, 0, 1024); if (i <= 0) break; else { CStreamWriter.Write(data, 0, 1024); //OUT OF MEMORY EXCEPTION HERE CStreamWriter.FlushFinalBlock(); } }

            modified on Sunday, May 11, 2008 7:19 AM

            I Offline
            I Offline
            Ian Uy
            wrote on last edited by
            #5

            Thanks! I'll give it a try. :)

            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.

            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