BadData error while during Decryption Plz how to solve this problem | Newbie | | Join Date: Oct 2009
Posts: 2
| | - using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.IO;
-
using System.Security.Cryptography;
-
using System.Security;
-
using System.Runtime.InteropServices;
-
using System.Windows.Forms;
-
-
namespace cryptography
-
{
-
public partial class CRYPTROGRAPHY : Form
-
{
-
public CRYPTROGRAPHY()
-
{
-
InitializeComponent();
-
}
-
[System.Runtime.InteropServices.DllImport("Kernal32.DLL", EntryPoint = "RTLZeromemory")]
-
public static extern bool Zeromemory(IntPtr destination, int length);
-
-
public static string Generatekey()
-
{
-
DESCryptoServiceProvider descrpto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
-
return (ASCIIEncoding.ASCII.GetString(descrpto.Key));
-
}
-
-
public static void Encrypt(string inputfile, string outputfile, string skey)
-
{
-
FileStream fileinput = new FileStream(inputfile, FileMode.Open, FileAccess.Read);
-
FileStream fileoutput = new FileStream(outputfile, FileMode.Create , FileAccess.Write);
-
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
-
des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
-
des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
-
ICryptoTransform descrypt = des.CreateEncryptor();
-
CryptoStream cryptostream = new CryptoStream(fileoutput, descrypt, CryptoStreamMode.Write);
-
byte[] bytearray = new byte[fileinput.Length];
-
fileinput.Read(bytearray, 0, bytearray.Length);
-
cryptostream.Write(bytearray, 0, bytearray.Length);
-
fileoutput.Close();
-
}
-
public static void Decrypt(string inputfile, string outputfile, string skey)
-
{
-
try
-
{
-
-
FileStream fileoutput = new FileStream(outputfile, FileMode.Open, FileAccess.Write);
-
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
-
des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
-
des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
-
-
FileStream fileinput = new FileStream(inputfile, FileMode.Open, FileAccess.Read);
-
ICryptoTransform descrypt = des.CreateDecryptor();
-
CryptoStream cryptostreamdec = new CryptoStream(fileinput , descrypt, CryptoStreamMode.Read);
-
StreamWriter fsdec = new StreamWriter(outputfile);
-
fsdec.Write(new StreamReader(cryptostreamdec).ReadToEnd());//Here BADData error
-
fsdec.Flush();
-
fsdec.Close();
-
MessageBox.Show("Data Decrypted");
-
}
-
catch (IOException ex)
-
{
-
MessageBox.Show(ex.ToString());
-
}
-
-
catch (CryptographicException ex)
-
{
-
MessageBox.Show(ex.StackTrace);
-
}
-
}
-
//Browse button coding
-
private void btn_Browse_Click(object sender, EventArgs e)
-
{
-
openFileDialog1.Filter = "(.*txt)|*.txt";
-
if (openFileDialog1.ShowDialog() == DialogResult.OK)
-
{
-
txt_Filename.Text = openFileDialog1.FileName;
-
}
-
}
-
//Form Loading
-
private void CRYPTROGRAPHY_Load(object sender, EventArgs e)
-
{
-
toolStripStatusLabel1.Text = "Select File";
-
}
-
//Encrypt
-
private void btn_Enncrypt_Click(object sender, EventArgs e)
-
{
-
string mysecretkey = Generatekey();
-
txt_Secretkey.Text = mysecretkey;
-
Encrypt(txt_Filename.Text , txt_Save.Text , mysecretkey);//call encrypt method
-
toolStripStatusLabel1.Text = "Encrypting data";
-
-
}
-
-
//call decrypt method
-
private void btn_Decrypt_Click(object sender, EventArgs e)
-
{
-
string keyvalue = txt_Secretkey.Text;
-
Decrypt(txt_Filename.Text, txt_Save.Text, keyvalue);
-
}
-
}
-
}
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,739
| | | 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. |  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,739
| | | 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
| | | re: BadData error while during Decryption Plz how to solve this problem
how to rectify it plz modify this code
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,739
| | | re: BadData error while during Decryption Plz how to solve this problem Quote:
Originally Posted by jayabalan07 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.
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|