0

my program runs into a OutOfMemoryException when I use large Strings with the following method.

        Public Function EncryptData(ByVal plaintext As String) As String
            Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)
            Dim ms As New System.IO.MemoryStream
            Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
            encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
            encStream.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray)
        End Function

The following Line is causing the exception:

encStream.Write(plaintextBytes, 0, plaintextBytes.Length)

How can I fix this problem? My method works fine with short strings.

NiklasV
  • 1
  • 1
  • 1
    Take a look at this replacement for MemoryStream which allows for larger allocations of memory: https://www.codeproject.com/Articles/348590/A-replacement-for-MemoryStream – David Feb 01 '23 at 14:16

0 Answers0