473,320 Members | 1,821 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,320 software developers and data experts.

An Encryption Question

The following code produces an error on the very last line, namely: An
unhandled exception of type
'System.Security.Cryptography.CryptographicExcepti on' occurred in
mscorlib.dll Additional information: Bad Data.

Please help; if you can with exactly which code I should change and how.

Patrick

************************************************** **************

static public void putID(String strinput)
{
FileStream fs = new
FileStream("Param.txt",FileMode.Create,FileAccess. Write);
Console.WriteLine("Enter Some Text to be stored in encrypted file:");
Byte[] bytearrayinput=ConvertStringToByteArray(strinput);
DESCryptoServiceProvider des1 = new DESCryptoServiceProvider();
ICryptoTransform desencrypt = des1.CreateEncryptor();
CryptoStream cryptostream = new
CryptoStream(fs,desencrypt,CryptoStreamMode.Write) ;
cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);
cryptostream.Close();
fs.Close();
}

static public String getID()
{
FileStream fsread = new
FileStream("Param.txt",FileMode.Open,FileAccess.Re ad);
DESCryptoServiceProvider des2 = new DESCryptoServiceProvider();
ICryptoTransform desdecrypt = des2.CreateDecryptor();
CryptoStream cryptostreamDecr = new
CryptoStream(fsread,desdecrypt,CryptoStreamMode.Re ad);
return( (new StreamReader(cryptostreamDecr, new
UnicodeEncoding())).ReadToEnd() );
}

************************************************** **************


Nov 15 '05 #1
5 2131
Patrick,

I'm not sure, but I think that before you close the stream you should
call FlushFinalBlock on the CryptoStream instance. I think you need to do
this in order to get anything left in the buffer written to the underlying
stream.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Patrick De Ridder" <00*@000.00> wrote in message
news:10***************@evisp-news-01.ops.asmr-01.energis-idc.net...
The following code produces an error on the very last line, namely: An
unhandled exception of type
'System.Security.Cryptography.CryptographicExcepti on' occurred in
mscorlib.dll Additional information: Bad Data.

Please help; if you can with exactly which code I should change and how.

Patrick

************************************************** **************

static public void putID(String strinput)
{
FileStream fs = new
FileStream("Param.txt",FileMode.Create,FileAccess. Write);
Console.WriteLine("Enter Some Text to be stored in encrypted file:");
Byte[] bytearrayinput=ConvertStringToByteArray(strinput);
DESCryptoServiceProvider des1 = new DESCryptoServiceProvider();
ICryptoTransform desencrypt = des1.CreateEncryptor();
CryptoStream cryptostream = new
CryptoStream(fs,desencrypt,CryptoStreamMode.Write) ;
cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);
cryptostream.Close();
fs.Close();
}

static public String getID()
{
FileStream fsread = new
FileStream("Param.txt",FileMode.Open,FileAccess.Re ad);
DESCryptoServiceProvider des2 = new DESCryptoServiceProvider();
ICryptoTransform desdecrypt = des2.CreateDecryptor();
CryptoStream cryptostreamDecr = new
CryptoStream(fsread,desdecrypt,CryptoStreamMode.Re ad);
return( (new StreamReader(cryptostreamDecr, new
UnicodeEncoding())).ReadToEnd() );
}

************************************************** **************

Nov 15 '05 #2
On Wed, 10 Sep 2003 09:48:36 -0400, "Nicholas Paldino [.NET/C# MVP]"
<ni**************@exisconsulting.com> wrote:
Patrick,

I'm not sure, but I think that before you close the stream you should
call FlushFinalBlock on the CryptoStream instance. I think you need to do
this in order to get anything left in the buffer written to the underlying
stream.

Hope this helps.


No the function still freaks out on the last line.
--
Regards,
Patrick.
Nov 15 '05 #3
On Wed, 10 Sep 2003 17:32:51 +0200, Patrick De Ridder
<pa***************@hetnet.nl> wrote:
On Wed, 10 Sep 2003 09:48:36 -0400, "Nicholas Paldino [.NET/C# MVP]"
<ni**************@exisconsulting.com> wrote:
Patrick,

I'm not sure, but I think that before you close the stream you should
call FlushFinalBlock on the CryptoStream instance. I think you need to do
this in order to get anything left in the buffer written to the underlying
stream.

Hope this helps.


No the function still freaks out on the last line.


This is an encrypting / decrypting program.
The program works fine. It is a download from MSDN.
I cannot get this code split up into two separate functions
one function to encrypt, one function to decrypt.
Can anyone please help?

Patrick.
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

class FileEncrypt {

public static Byte[] ConvertStringToByteArray(String s)
{
return (new UnicodeEncoding()).GetBytes(s);
}

public static void Main()
{
//Creating a file stream
FileStream fs = new
FileStream("EncryptedFile.txt",FileMode.Create,Fil eAccess.Write);

Console.WriteLine("Enter Some Text to be stored in encrypted
file:");
String strinput = Console.ReadLine();

Byte[] bytearrayinput=ConvertStringToByteArray(strinput);

//DES instance with random key
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//create DES Encryptor from this instance
ICryptoTransform desencrypt = des.CreateEncryptor();

//Create Crypto Stream that transforms file stream using des
encryption
CryptoStream cryptostream = new
CryptoStream(fs,desencrypt,CryptoStreamMode.Write) ;

//write out DES encrypted file
cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);

cryptostream.Close();

//create file stream to read encrypted file back
FileStream fsread = new
FileStream("EncryptedFile.txt",FileMode.Open,FileA ccess.Read);

//create DES Decryptor from our des instance
ICryptoTransform desdecrypt = des.CreateDecryptor();

//create crypto stream set to read and do a des decryption transform
on incoming bytes
CryptoStream cryptostreamDecr = new
CryptoStream(fsread,desdecrypt,CryptoStreamMode.Re ad);
//print out the contents of the decrypted file
Console.WriteLine( (new StreamReader(cryptostreamDecr, new
UnicodeEncoding())).ReadToEnd() );

Console.WriteLine ();
Console.WriteLine ("Press Enter to continue...");
Console.ReadLine();
}
}


--
Regards,
Patrick.
Nov 15 '05 #4
I think a small change to the original code will make it easy to be
seperated. Just to store IV and key some where and use it later when call
CreateDecryptor()

like

public static void Main()
{
//Creating a file stream
FileStream fs = new
FileStream("EncryptedFile.txt",FileMode.Create,Fil eAccess.Write);

Console.WriteLine("Enter Some Text to be stored in encrypted
file:");
String strinput = Console.ReadLine();

Byte[] bytearrayinput=ConvertStringToByteArray(strinput);

//DES instance with random key
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//create DES Encryptor from this instance
ICryptoTransform desencrypt = des.CreateEncryptor();
//***************************
byte[] IV = des.IV;
byte[] key = des.Key;
//************************
//Create Crypto Stream that transforms file stream using des
encryption
CryptoStream cryptostream = new
CryptoStream(fs,desencrypt,CryptoStreamMode.Write) ;

//write out DES encrypted file
cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);

cryptostream.Close();

//create file stream to read encrypted file back
FileStream fsread = new
FileStream("EncryptedFile.txt",FileMode.Open,FileA ccess.Read);

//create DES Decryptor from our des instance
//ICryptoTransform desdecrypt = des.CreateDecryptor();
//*********************************
ICryptoTransform desdecrypt = des.CreateDecryptor(key, IV);
//*************************************
//create crypto stream set to read and do a des decryption transform
on incoming bytes
CryptoStream cryptostreamDecr = new
CryptoStream(fsread,desdecrypt,CryptoStreamMode.Re ad);
//print out the contents of the decrypted file
Console.WriteLine( (new StreamReader(cryptostreamDecr, new
UnicodeEncoding())).ReadToEnd() );

Console.WriteLine ();
Console.WriteLine ("Press Enter to continue...");
Console.ReadLine();
}
}

"Patrick De Ridder" <pa***************@hetnet.nl> wrote in message
news:n7********************************@4ax.com...
On Wed, 10 Sep 2003 17:32:51 +0200, Patrick De Ridder
<pa***************@hetnet.nl> wrote:
On Wed, 10 Sep 2003 09:48:36 -0400, "Nicholas Paldino [.NET/C# MVP]"
<ni**************@exisconsulting.com> wrote:
Patrick,

I'm not sure, but I think that before you close the stream you shouldcall FlushFinalBlock on the CryptoStream instance. I think you need to dothis in order to get anything left in the buffer written to the underlyingstream.

Hope this helps.


No the function still freaks out on the last line.


This is an encrypting / decrypting program.
The program works fine. It is a download from MSDN.
I cannot get this code split up into two separate functions
one function to encrypt, one function to decrypt.
Can anyone please help?

Patrick.
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

class FileEncrypt {

public static Byte[] ConvertStringToByteArray(String s)
{
return (new UnicodeEncoding()).GetBytes(s);
}

public static void Main()
{
//Creating a file stream
FileStream fs = new
FileStream("EncryptedFile.txt",FileMode.Create,Fil eAccess.Write);

Console.WriteLine("Enter Some Text to be stored in encrypted
file:");
String strinput = Console.ReadLine();

Byte[] bytearrayinput=ConvertStringToByteArray(strinput);

//DES instance with random key
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//create DES Encryptor from this instance
ICryptoTransform desencrypt = des.CreateEncryptor();

//Create Crypto Stream that transforms file stream using des
encryption
CryptoStream cryptostream = new
CryptoStream(fs,desencrypt,CryptoStreamMode.Write) ;

//write out DES encrypted file
cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);

cryptostream.Close();

//create file stream to read encrypted file back
FileStream fsread = new
FileStream("EncryptedFile.txt",FileMode.Open,FileA ccess.Read);

//create DES Decryptor from our des instance
ICryptoTransform desdecrypt = des.CreateDecryptor();

//create crypto stream set to read and do a des decryption transform
on incoming bytes
CryptoStream cryptostreamDecr = new
CryptoStream(fsread,desdecrypt,CryptoStreamMode.Re ad);
//print out the contents of the decrypted file
Console.WriteLine( (new StreamReader(cryptostreamDecr, new
UnicodeEncoding())).ReadToEnd() );

Console.WriteLine ();
Console.WriteLine ("Press Enter to continue...");
Console.ReadLine();
}
}


--
Regards,
Patrick.

Nov 15 '05 #5
On Wed, 10 Sep 2003 17:56:12 -0400, "Leo Lin" <le*****@hotmail.com>
wrote:
I think a small change to the original code will make it easy to be
seperated. Just to store IV and key some where and use it later when call
CreateDecryptor()

Thank you Leo, I am sending an email to your email address.
--
Regards,
Patrick.
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Harold Crump | last post by:
Greetings, I have a requirement of storing some .xml files on a web server. The files will contain financial information like credit card numbers, so I would like to encrypt them. The files...
34
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? ...
14
by: Ray Cassick \(Home\) | last post by:
Ok, time to ask the question here.. I have been battling over this one for sometime now and just have to ask it. I have created a few classes that I use to act a security keys. These classes get...
4
by: panik | last post by:
Hi, I'm looking for something similar to Encryption. I'd like to generate URL's with a format that avoids visible ID's (e.g. http://thesite/viewlink.aspx?ID=105) Instead, I'd like to generate a...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
1
by: David | last post by:
One thing that's always puzzled me about implementing encryption on remote asp.net apps is where to store the keys. The demo code indicate that you include them in a configuration file, but this...
3
by: dmalhotr2001 | last post by:
I was wondering whether anyone ever dealt with encryption that are visa compliant with credit card numbers: On 3.4 of this document...
1
by: =?Utf-8?B?bWljcm9ob2Y=?= | last post by:
Short version: Is there a way to configure (preferably programmatically) the max encryption strength that will be used by the framework when connecting to a particular SSL-protected web service? ...
25
by: eggie5 | last post by:
I have a form where a user can change his password, but I'm confused on how to prevent this from being transmitted in plain text. Well, I know how not to transmit it in plain text - use any type...
19
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.