An Encryption Question

P

Patrick De Ridder

The following code produces an error on the very last line, namely: An
unhandled exception of type
'System.Security.Cryptography.CryptographicException' 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.Read);
DESCryptoServiceProvider des2 = new DESCryptoServiceProvider();
ICryptoTransform desdecrypt = des2.CreateDecryptor();
CryptoStream cryptostreamDecr = new
CryptoStream(fsread,desdecrypt,CryptoStreamMode.Read);
return( (new StreamReader(cryptostreamDecr, new
UnicodeEncoding())).ReadToEnd() );
}

****************************************************************
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
P

Patrick De Ridder

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.
 
P

Patrick De Ridder

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,FileAccess.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,FileAccess.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.Read);
//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();
}
}
 
L

Leo Lin

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,FileAccess.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,FileAccess.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.Read);
//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 said:
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,FileAccess.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,FileAccess.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.Read);
//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();
}
}
 
P

Patrick De Ridder

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top