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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:17:16+00:00 2026-05-14T04:17:16+00:00

OK, my need here is to save whatever typed in the rich text box

  • 0

OK, my need here is to save whatever typed in the rich text box to a file, encrypted, and also retrieve the text from the file again and show it back on the rich textbox. Here is my save code.

private void cmdSave_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);

    AesCryptoServiceProvider aes = new AesCryptoServiceProvider();

    aes.GenerateIV();
    aes.GenerateKey();
    aes.Mode = CipherMode.CBC;

    TextWriter twKey = new StreamWriter("key");
    twKey.Write(ASCIIEncoding.ASCII.GetString(aes.Key));
    twKey.Close();

    TextWriter twIV = new StreamWriter("IV");
    twIV.Write(ASCIIEncoding.ASCII.GetString(aes.IV));
    twIV.Close();

    ICryptoTransform aesEncrypt = aes.CreateEncryptor();

    CryptoStream cryptoStream = new CryptoStream(fs, aesEncrypt, CryptoStreamMode.Write);

    richTextBox1.SaveFile(cryptoStream, RichTextBoxStreamType.RichText);
}

I know the security consequences of saving the key and iv in a file but this just for testing 🙂

Well, the saving part works fine which means no exceptions… The file is created in filePath and the key and IV files are created fine too…

OK now for retrieving part where I am stuck :S

private void cmdOpen_Click(object sender, EventArgs e)
{
    OpenFileDialog openFile = new OpenFileDialog();

    openFile.ShowDialog();

    FileStream openRTF = new FileStream(openFile.FileName, FileMode.Open, FileAccess.Read);

    AesCryptoServiceProvider aes = new AesCryptoServiceProvider();

    TextReader trKey = new StreamReader("key");
    byte[] AesKey = ASCIIEncoding.ASCII.GetBytes(trKey.ReadLine());

    TextReader trIV = new StreamReader("IV");
    byte[] AesIV = ASCIIEncoding.ASCII.GetBytes(trIV.ReadLine());

    aes.Key = AesKey;
    aes.IV = AesIV;

    ICryptoTransform aesDecrypt = aes.CreateDecryptor();

    CryptoStream cryptoStream = new CryptoStream(openRTF, aesDecrypt, CryptoStreamMode.Read);

    StreamReader fx = new StreamReader(cryptoStream);

    richTextBox1.Rtf = fx.ReadToEnd();

    //richTextBox1.LoadFile(fx.BaseStream, RichTextBoxStreamType.RichText);        
} 

But the richTextBox1.Rtf = fx.ReadToEnd(); throws an cryptographic exception “Padding is invalid and cannot be removed.”

while richTextBox1.LoadFile(fx.BaseStream, RichTextBoxStreamType.RichText); throws an NotSupportedException “Stream does not support seeking.”

Any suggestions on what i can do to load the data from the encrypted file and show it in the rich text box?

  • 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-14T04:17:16+00:00Added an answer on May 14, 2026 at 4:17 am

    Your IV and Key are never written in the file to begin with (judging from your save_cmd)

    And same goes for your opening. There’s no link at ALL between between your (“Key” stream and your file anywhere…)

    Updated :

    Here is a better version of your code :

            private void button1_Click(object sender, EventArgs e)
        {
    
    
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
    
            aes.GenerateIV();
            aes.GenerateKey();
            aes.Mode = CipherMode.CBC;
    
    
            File.WriteAllBytes("Key",aes.Key);
            File.WriteAllBytes("IV",aes.IV);
    
    
            ICryptoTransform aesEncrypt = aes.CreateEncryptor();
            using (FileStream fs = new FileStream("file.crypt", FileMode.Create, FileAccess.Write))
            {
                using (CryptoStream cryptoStream = new CryptoStream(fs, aesEncrypt, CryptoStreamMode.Write))
                {
    
                    richTextBox1.SaveFile(cryptoStream, RichTextBoxStreamType.RichText);
                }
            }
    
        }
    
           private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
    
            openFile.ShowDialog();
    
    
    
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
    
    
            byte[] AesKey = File.ReadAllBytes("Key");
            byte[] AesIV = File.ReadAllBytes("IV");
    
            aes.Key = AesKey;
            aes.IV = AesIV;
    
            ICryptoTransform aesDecrypt = aes.CreateDecryptor();
            using (FileStream openRTF = new FileStream(openFile.FileName, FileMode.Open, FileAccess.Read))
            {
                using (CryptoStream cryptoStream = new CryptoStream(openRTF, aesDecrypt, CryptoStreamMode.Read))
                {
    
                    using (StreamReader fx = new StreamReader(cryptoStream))
                    {
                        richTextBox1.Rtf = fx.ReadToEnd();
                    }
                }
            }
    
        }
    

    It works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

BeautifulSoup newbe... Need help Here is the code sample... from mechanize import Browser from
here i need a batch file which can apply and create label or base
I would need little help here. I'm trying to get the git respository from
I have a text file ~6GB which I need to parse and later persist.
I really need help here. I'm desperate at this point. I have NSOperation that
I'm converting an existing program to C++ and here need to manipulate Sybase timestamps.
i need some help here. I will like to transfer an Jpg or PNG
Here I need to call a javascript function first and after some time I
Here i need to check a button click using javascript i.e)if button A is
I need some opinions here. I'm working on a Django project using buildout to

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.