Connecting Tech Pros Worldwide Help | Site Map

BadData error while during Decryption Plz how to solve this problem

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: Oct 9 '09
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.IO;
  8. using System.Security.Cryptography;
  9. using System.Security;
  10. using System.Runtime.InteropServices;
  11. using System.Windows.Forms;
  12.  
  13. namespace cryptography
  14. {
  15.     public partial class CRYPTROGRAPHY : Form
  16.     {
  17.         public CRYPTROGRAPHY()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         [System.Runtime.InteropServices.DllImport("Kernal32.DLL", EntryPoint = "RTLZeromemory")]
  22.         public static extern bool Zeromemory(IntPtr destination, int length);
  23.  
  24.         public static string Generatekey()
  25.         {
  26.             DESCryptoServiceProvider descrpto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  27.             return (ASCIIEncoding.ASCII.GetString(descrpto.Key));
  28.         }
  29.  
  30.         public static void  Encrypt(string inputfile, string outputfile, string skey)
  31.         {
  32.             FileStream fileinput = new FileStream(inputfile, FileMode.Open, FileAccess.Read);
  33.             FileStream fileoutput = new FileStream(outputfile, FileMode.Create , FileAccess.Write);
  34.             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  35.             des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
  36.             des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
  37.             ICryptoTransform descrypt = des.CreateEncryptor();
  38.             CryptoStream cryptostream = new CryptoStream(fileoutput, descrypt, CryptoStreamMode.Write);
  39.             byte[] bytearray = new byte[fileinput.Length];
  40.             fileinput.Read(bytearray, 0, bytearray.Length);
  41.             cryptostream.Write(bytearray, 0, bytearray.Length);
  42.              fileoutput.Close();
  43.          }
  44.         public static void Decrypt(string inputfile, string outputfile, string skey)
  45.         {
  46.             try
  47.             {
  48.  
  49.                 FileStream fileoutput = new FileStream(outputfile, FileMode.Open, FileAccess.Write);
  50.                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  51.                 des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
  52.                 des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
  53.  
  54.                 FileStream fileinput = new FileStream(inputfile, FileMode.Open, FileAccess.Read);
  55.                 ICryptoTransform descrypt = des.CreateDecryptor();
  56.                 CryptoStream cryptostreamdec = new CryptoStream(fileinput , descrypt, CryptoStreamMode.Read);
  57.                 StreamWriter fsdec = new StreamWriter(outputfile);
  58.                 fsdec.Write(new StreamReader(cryptostreamdec).ReadToEnd());//Here BADData error 
  59.                 fsdec.Flush();
  60.                 fsdec.Close();
  61.                 MessageBox.Show("Data Decrypted");
  62.             }
  63.             catch (IOException ex)
  64.             {
  65.                 MessageBox.Show(ex.ToString());
  66.             }
  67.  
  68.             catch (CryptographicException ex)
  69.             {
  70.                 MessageBox.Show(ex.StackTrace);
  71.             }
  72.         }
  73.         //Browse button coding
  74.         private void btn_Browse_Click(object sender, EventArgs e)
  75.         {
  76.             openFileDialog1.Filter = "(.*txt)|*.txt";
  77.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  78.             {
  79.                 txt_Filename.Text = openFileDialog1.FileName;
  80.             }
  81.         }
  82.         //Form Loading 
  83.         private void CRYPTROGRAPHY_Load(object sender, EventArgs e)
  84.         {
  85.             toolStripStatusLabel1.Text = "Select File";
  86.         }
  87.         //Encrypt
  88.         private void btn_Enncrypt_Click(object sender, EventArgs e)
  89.         {
  90.             string mysecretkey = Generatekey();
  91.             txt_Secretkey.Text = mysecretkey;
  92.             Encrypt(txt_Filename.Text , txt_Save.Text , mysecretkey);//call encrypt method
  93.             toolStripStatusLabel1.Text = "Encrypting data";
  94.  
  95.         }
  96.  
  97.         //call decrypt method
  98.         private void btn_Decrypt_Click(object sender, EventArgs e)
  99.         {
  100.             string keyvalue = txt_Secretkey.Text;
  101.             Decrypt(txt_Filename.Text, txt_Save.Text, keyvalue);
  102.         }
  103.     }
  104. }
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,739
#2: Oct 9 '09

re: BadData error while during Decryption Plz how to solve this problem


TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,739
#3: Oct 9 '09

re: BadData error while during Decryption Plz how to solve this problem


You're going to have to give the volunteers here a bit more to go on that the entire code for your form.

Like *where* does the program break? On button_encrypt, or decrypt, or when you try to send it an entire file....

On what line does it break, and what is the exact error message you get?
Newbie
 
Join Date: Oct 2009
Posts: 2
#4: Oct 9 '09

re: BadData error while during Decryption Plz how to solve this problem


how to rectify it plz modify this code
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,739
#5: Oct 9 '09

re: BadData error while during Decryption Plz how to solve this problem


Quote:

Originally Posted by jayabalan07 View Post

how to rectify it plz modify this code


Dude, nobody here is going to do your homework for you.

Especially without some more to go on. Please refer to my earlier post that had QUESTIONS FOR YOU in it.
Reply