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
309 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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