473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

BadData error while during Decryption Plz how to solve this problem

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. }
Oct 9 '09 #1
4 2527
tlhintoq
3,525 Expert 2GB
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.
Oct 9 '09 #2
tlhintoq
3,525 Expert 2GB
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?
Oct 9 '09 #3
how to rectify it plz modify this code
Oct 9 '09 #4
tlhintoq
3,525 Expert 2GB
@jayabalan07

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.
Oct 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike Chirico | last post by:
With mysql-4.1.1-alpha, using any User Defined Function ( UDF) at the query execution stage, on Linux 2.6, gave me the following error: "ERROR 2013 (HY000): Lost connection to MySQL server...
1
by: Martin | last post by:
Hi Im trying to make a Client/Server where im going to encrypt the stream but i cant get it to work. I can recieve and send as long as im not trying to send/recieve encrypted. but when i am i cant...
2
by: Dave Bailey | last post by:
I have developed a web app using DPAPI to encrypt a connection string in the web.config file. The application works perfectly on the development machine but when deployed to the server when...
2
by: Microsoft news group | last post by:
Every time when I try to create a new asp.net porject, I always get this message: The Web server reported the following error when attempting to create or open the Web project located at the...
0
by: pebelund | last post by:
Im having a problem I cant solve atm. I got a webbserver, where Im doing a search, that will list some information. The search goes to a seperate SQL 2000 server, to a stored procedure that runs...
1
by: BigLuzer | last post by:
hi i am using the following setup: - .net 1.1 - 2 load-balanced iis servers - DPAPI machine store. - C# - i encrypted the connection string separately, one on each machine. the error i get...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
1
by: Elliot | last post by:
When decrypt the xml, output "Unable to retrieve the decryption key." Can anyone help me solve the problem? I got the code from http://msdn.microsoft.com/en-us/library/ms229746.aspx using...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.