C
chau_fai
i got two buttons in the form. One is for encrypting the message read
from a xml file and then encrypt the xml and save back to another
file. The other button is for decryption. Again, this button read the
encrypted file back, and then perform decryption.
But i don't know why an exception always thrown when processing this
line of code "int actualbytesread = encStream.Read(buf, 0, 100);" ...
Can anyone give me some instructions what is happening ?
private void Encrypt_Click(object sender, System.EventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(Application.StartupPath + "\\Data\\userInfo.xml");
Byte[] data = new UnicodeEncoding().GetBytes(xml.OuterXml);
MemoryStream ms = new MemoryStream(data.Length* 2);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,des.CreateEncryptor(),
CryptoStreamMode.Write);
encStream.Write(data, 0, data.Length);
encStream.FlushFinalBlock();
//calculate the length of the encrypted data
byte[] bResult = new byte[ms.Position];
ms.Position = 0;
ms.Read(bResult, 0, bResult.Length) ;
FileStream fs = new FileStream(Application.StartupPath +
"\\Data\\file.try", FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.None);
fs.Write(bResult, 0, bResult.Length);
fs.Close();
}
private void Decrypt_Click(object sender, System.EventArgs e)
{
FileStream fs= new FileStream(Application.StartupPath +
"\\Data\\file.try", FileMode.Open, FileAccess.Read, FileShare.None);
byte[] ByteArray=new byte[fs.Length];
int nBytesRead = fs.Read(ByteArray, 0, ByteArray.Length);
fs.Close();
MemoryStream ms = new MemoryStream(ByteArray);
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(), CryptoStreamMode.Read);
MemoryStream plaintextmem = new MemoryStream();
do
{
Byte[] buf = new Byte[100];
try
{
//read the plaintext from CryptoStream
int actualbytesread = encStream.Read(buf, 0, 100);
//if we have reached the end of stream quit the loop
if (0 == actualbytesread)
break;
//copy the plaintext byte array to MemoryStream
plaintextmem.Write(buf,0,actualbytesread);
}
catch(Exception ab)
{
MessageBox.Show(ab.Message, "Asdf");
}
}while(true);
encStream.Close();
ms.Close();
//Extract the plaintext byte stream and close the MemoryStream
Byte[] plaintextbyte = plaintextmem.ToArray();
plaintextmem.Close();
//Encode the plaintext byte into Unicode string
string plaintext = new UnicodeEncoding().GetString(plaintextbyte);
encStream.Close();
}
I would appreciate any response and help from yours ~
from a xml file and then encrypt the xml and save back to another
file. The other button is for decryption. Again, this button read the
encrypted file back, and then perform decryption.
But i don't know why an exception always thrown when processing this
line of code "int actualbytesread = encStream.Read(buf, 0, 100);" ...
Can anyone give me some instructions what is happening ?
private void Encrypt_Click(object sender, System.EventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(Application.StartupPath + "\\Data\\userInfo.xml");
Byte[] data = new UnicodeEncoding().GetBytes(xml.OuterXml);
MemoryStream ms = new MemoryStream(data.Length* 2);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,des.CreateEncryptor(),
CryptoStreamMode.Write);
encStream.Write(data, 0, data.Length);
encStream.FlushFinalBlock();
//calculate the length of the encrypted data
byte[] bResult = new byte[ms.Position];
ms.Position = 0;
ms.Read(bResult, 0, bResult.Length) ;
FileStream fs = new FileStream(Application.StartupPath +
"\\Data\\file.try", FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.None);
fs.Write(bResult, 0, bResult.Length);
fs.Close();
}
private void Decrypt_Click(object sender, System.EventArgs e)
{
FileStream fs= new FileStream(Application.StartupPath +
"\\Data\\file.try", FileMode.Open, FileAccess.Read, FileShare.None);
byte[] ByteArray=new byte[fs.Length];
int nBytesRead = fs.Read(ByteArray, 0, ByteArray.Length);
fs.Close();
MemoryStream ms = new MemoryStream(ByteArray);
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(), CryptoStreamMode.Read);
MemoryStream plaintextmem = new MemoryStream();
do
{
Byte[] buf = new Byte[100];
try
{
//read the plaintext from CryptoStream
int actualbytesread = encStream.Read(buf, 0, 100);
//if we have reached the end of stream quit the loop
if (0 == actualbytesread)
break;
//copy the plaintext byte array to MemoryStream
plaintextmem.Write(buf,0,actualbytesread);
}
catch(Exception ab)
{
MessageBox.Show(ab.Message, "Asdf");
}
}while(true);
encStream.Close();
ms.Close();
//Extract the plaintext byte stream and close the MemoryStream
Byte[] plaintextbyte = plaintextmem.ToArray();
plaintextmem.Close();
//Encode the plaintext byte into Unicode string
string plaintext = new UnicodeEncoding().GetString(plaintextbyte);
encStream.Close();
}
I would appreciate any response and help from yours ~