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

  • Home
  • SEARCH
  • 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 7698755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:18:59+00:00 2026-05-31T22:18:59+00:00

so my problem is simple. I want to Encrypt some text and write it

  • 0

so my problem is simple. I want to Encrypt some text and write it to File like byte of array and than i need read the content of the File and get like parametr to another method which Decrypt that byte of array to string.
Method Encrypt and Decrypt work fine i try it but when i use byte array from FILE it thrown Exception.

private static byte[] EncryptString(string text,byte[] key,byte[] vektor)
    {
        byte[] array=null;
        // Check arguments.
        if (text == null || text.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (key == null || key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (vektor == null || vektor.Length <= 0)
            throw new ArgumentNullException("Vektor");
        try
        {
            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                provider.Key = key;
                provider.IV = vektor;
                using (MemoryStream memory = new MemoryStream())
                {
                    using (CryptoStream crypto = new CryptoStream(memory, provider.CreateEncryptor(provider.Key, provider.IV), CryptoStreamMode.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(crypto))
                        {
                            writer.WriteLine(text);
                        }
                    }
                    array = memory.ToArray();
                }
            }
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("Error in EncryptString  {0}", e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error in EncryptString  {0}", e.Message);
        }
        return array;
    }

This method Ecnrypt byte array to string

private static string DecryptByte(byte[] text, byte[] key, byte[] vektor)
    {
        string result = null;
        // Check arguments.
        if (text == null || text.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (key == null || key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (vektor == null || vektor.Length <= 0)
            throw new ArgumentNullException("Key");
        try
        {
            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                provider.Key=key;
                provider.IV=vektor;
                using (MemoryStream memory = new MemoryStream(text))
                {
                    using (CryptoStream crypto = new CryptoStream(memory, provider.CreateDecryptor(provider.Key, provider.IV), CryptoStreamMode.Read))
                    {
                        using (StreamReader read = new StreamReader(crypto))
                        { 
                            result=read.ReadToEnd();
                        }
                    }
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Error in DecryptByte:{0}"+e.Message);
        }
        return result;
    }

Well i save the encrypt text and then when button is pushed

private void buttonDecrypt_Click(object sender, RoutedEventArgs e)
    {
        byte[] text=null,helper=null;
        string result = null;
        try
        {
            using (FileStream filestream = File.OpenRead(path))
            {
                helper = new byte[filestream.Length];
                filestream.Read(helper, 0, (int)filestream.Length);
            }
                    using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
                    {
                        result = DecryptByte(helper, provider.Key, provider.IV);
                    }
        }
        catch(Exception ex)
        {
            Console.WriteLine("Error v ButtonDecrypt.{0}_____{1}",ex.Message,ex.Data);
        }
    }

So my problem is that data which i read from File is not the same and in DecryptByte thrown Exception. Could you help me?
EDIT yeah Exception message is Invalid Data

  • 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-31T22:19:00+00:00Added an answer on May 31, 2026 at 10:19 pm

    The code in your buttonDecrypt_Click you don’t initialize the Key and the IV properties of your Decryptor.
    I suspect that this is the cause of your exception.

    Moreover, I think you have another problem in the crypting and decrypting methods.
    DESCryptoServiceProvider is a wrapper around a SymmetricAlgorithm. This means that it encrypts bytes in blocks of equal size. So the input data needs to be an exact multiple of the block size. This isn’t going to happen all the time. Then padding needs to be used.

    provider.Padding = PaddingMode.PKCS7;
    

    will ensure a correct block size in encrypted bytes (also needed in decrypting of course)

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

Sidebar

Related Questions

I want to write a simple chat-client in Ruby for the terminal. The Problem
Really simple problem: I want to split a connection string into its keyword /
Simple problem (I think): I want to be able to invoke a click method
My problem is simple: I have a long list of elements that I want
The problem is simple: Using bash, I want to add a directory to my
Problem 1: I have a simple winforms app and I want to DataBind my
I have a simple, real life problem I want to solve using an OO
my problem is very simple: I want to know how many characters are inside
I have a simple problem: I want to construct a regex that matches a
I have a simple problem: I want to configure an object differently based on

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.