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
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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