Solved

When I dcrypt a file, I have an exception that says "Length of the data to decrypt is invalid"

Posted on 2006-07-17
1
306 Views
Last Modified: 2012-06-27
I am using the .NET encryption classes. I am fine with encryption the files. However, when I try to decrypt, I am havving an exception that says "Length of the data to decrypt is invalid"

Here is my code:
1. Decryption:

private void decrypt_Click(object sender, System.EventArgs e)
{
      FileStream stream = new FileStream("C:\\spAddDel.txt",  FileMode.Open,FileAccess.Read);

      DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
      cryptic.Padding = PaddingMode.Zeros;
      cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
      cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
      CryptoStream crStream = new CryptoStream(stream,
      cryptic.CreateDecryptor(),CryptoStreamMode.Read);
      StreamReader reader = new StreamReader(crStream);
      string data = reader.ReadToEnd();
      reader.Close();
      stream.Close();
}

What is wrong with the above code? The following encryption method works fine.


2. Encryption:

private void Button2_Click(object sender, System.EventArgs e)
{
      FileStream stream = new FileStream("C:\\root.txt", FileMode.OpenOrCreate,FileAccess.Write);
      DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
      cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
      cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
      CryptoStream crStream = new CryptoStream(stream,
      cryptic.CreateEncryptor(),CryptoStreamMode.Write);
      byte[] data = ASCIIEncoding.ASCII.GetBytes("charlie");
      crStream.Write(data,0,data.Length);
      crStream.Close();
      stream.Close();
}

What am I missing there in the decryption method?

Thanks in advance.
0
Comment
Question by:chaleastale
1 Comment
 
LVL 41

Accepted Solution

by:
graye earned 125 total points
ID: 17216427
You need to set the Padding property for the encryption routine as well...  DES is a block cipher and is expecting it's cipher text be be in mulitples of 8 bytes.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now