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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:53:53+00:00 2026-05-23T08:53:53+00:00

I’m attempting to use symmetric encryption to encrypt some data, and store the key

  • 0

I’m attempting to use symmetric encryption to encrypt some data, and store the key necessary to decrypt with the data in a MemoryStream. (While I know that this alone is really dumb security-wise, I’m going to use RSA to encrypt the symmetric key. Right now, though, I’m trying to just get this part to work.)

I’m using the FileHelpers library to parse the data for my delimiter (the semicolon, as I don’t believe I’ll ever have a semicolon in the data). Unfortunately, in my decryption function, when it’s parsing, it only returns back one record. And, if I show the entire string of encrypted data created at the end of this function, it doesn’t appear to be using multiple records.

I’m wondering if when I create the new cryptostream, it’s defaulting to the beginning of the memory stream, so when I write my encrypted data, it overwrites the data I had just written to the memory stream. Do you think that’s right?

Thanks for your help!

Private Function Encrypt(ByVal data As String) As String
    Dim utf8 As New UTF8Encoding
    ' Convert string data to byte array
    Dim inBytes() As Byte = utf8.GetBytes(data)
    ' Create memory stream for storing the data we've manipulated
    Dim ms As New MemoryStream()

    Dim aes As New RijndaelManaged()
    aes.KeySize = 256

    Dim cs As New CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)

    ' Write key to beginning of memory stream
    ms.Write(aes.Key, 0, aes.Key.Length)
    ' Add semicolon delimiter to memory stream
    ms.Write(utf8.GetBytes(";"), 0, utf8.GetBytes(";").Length)
    ' Write IV to memory stream
    ms.Write(aes.IV, 0, aes.IV.Length)
    ' Write semicolon delimiter to memory stream
    ms.Write(utf8.GetBytes(";"), 0, utf8.GetBytes(";").Length)
    ' Ensure that the data we've just written is in the memory stream, before
    ' we add the encrypted data
    ms.Flush()

    ' Write the encrypted data
    cs.Write(inBytes, 0, inBytes.Length) ' encrypt
    cs.FlushFinalBlock()

    ' Return encrypted data as string
    Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
End Function
  • 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-23T08:53:53+00:00Added an answer on May 23, 2026 at 8:53 am

    Here is a quick sample of how you can achieve this. I have included both the Encrypt and Decrypt because it is obviously important that you follow the same pattern on both sides. I will leave the relevant error checking etc. to you 🙂

    Imports System.Security.Cryptography
    Imports System.IO
    Imports System.Text
    
    Module Module1
    
      Sub Main()
        Dim encrypted As String = Encrypt("Here is the data")
        Dim decrypted As String = Decrypt(encrypted)
        Console.WriteLine(decrypted)
        Console.ReadKey()
      End Sub
    
      Private Function Encrypt(clearText As String) As String
        Dim aes As New RijndaelManaged
        aes.KeySize = 256
    
        Using ms As New MemoryStream
          ms.WriteByte(aes.Key.Length)
          ms.Write(aes.Key, 0, aes.Key.Length)
          ms.WriteByte(aes.IV.Length)
          ms.Write(aes.IV, 0, aes.IV.Length)
    
          Using cs As New CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)
            Dim bytes() As Byte = Encoding.UTF8.GetBytes(clearText)
            cs.Write(bytes, 0, bytes.Length)
          End Using
    
          Return Convert.ToBase64String(ms.ToArray())
        End Using
      End Function
    
      Private Function Decrypt(cipherText As String) As String
        Dim ms As New MemoryStream(Convert.FromBase64String(cipherText))
    
        Dim keyLength As Byte = ms.ReadByte()
        Dim key(keyLength - 1) As Byte
        ms.Read(key, 0, keyLength)
    
        Dim ivLength As Byte = ms.ReadByte()
        Dim iv(ivLength - 1) As Byte
        ms.Read(iv, 0, ivLength)
    
        Dim dataOffset As Integer = ms.Position
    
        Dim aes As New RijndaelManaged()
        aes.Key = key
        aes.IV = iv
    
        Using cs As New CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)
          Using sr As New StreamReader(cs, Encoding.UTF8)
            Return sr.ReadToEnd()
          End Using
        End Using
      End Function
    End Module
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.