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

Posted on 2006-07-17
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,
      StreamReader reader = new StreamReader(crStream);
      string data = reader.ReadToEnd();

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,
      byte[] data = ASCIIEncoding.ASCII.GetBytes("charlie");

What am I missing there in the decryption method?

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

Accepted Solution

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.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
scanning dentists xray (the small ones) 3 53
Wav problem 4 25
SSRS subreport with parameter 3 32
Need help with a Crystal Report in my C#.Net application 2 15
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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