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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Class object 2 26
Filename to be appended with DateTimeof Download 3 44
What .NET URL re-routing tool did I use? 2 36
C# Single Form 8 27
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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

25 Experts available now in Live!

Get 1:1 Help Now