Click here to Skip to main content
15,903,362 members
Home / Discussions / C#
   

C#

 
GeneralRe: Application.Run Pin
scadaguy22-Mar-04 5:11
scadaguy22-Mar-04 5:11 
GeneralRe: Application.Run Pin
Heath Stewart22-Mar-04 5:19
protectorHeath Stewart22-Mar-04 5:19 
GeneralRe: Application.Run Pin
scadaguy22-Mar-04 5:41
scadaguy22-Mar-04 5:41 
GeneralRe: Application.Run Pin
HAHAHA_NEXT22-Mar-04 8:38
HAHAHA_NEXT22-Mar-04 8:38 
Generalactive directory users listing Pin
agakyurek21-Mar-04 23:42
agakyurek21-Mar-04 23:42 
GeneralRe: active directory users listing Pin
Heath Stewart22-Mar-04 3:54
protectorHeath Stewart22-Mar-04 3:54 
QuestionHow to: Registry Pin
bouli21-Mar-04 22:55
bouli21-Mar-04 22:55 
Generalunknown exception thrown when decrypting read-in message Pin
chau_fai_rubbish21-Mar-04 21:36
chau_fai_rubbish21-Mar-04 21:36 
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 ?Confused | :confused:

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) ;
rich.Text += new UnicodeEncoding().GetString(bResult) + "\n\n"; encStream.Close();
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();
ms.Position = 0;

do
{
//Create a byte array into which we will read the plaintext
//from CryptoStream
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);

//don't forget to close the streams
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 ~
GeneralRe: unknown exception thrown when decrypting read-in message Pin
chau_fai_rubbish22-Mar-04 2:34
chau_fai_rubbish22-Mar-04 2:34 
GeneralRe: unknown exception thrown when decrypting read-in message Pin
Heath Stewart22-Mar-04 3:17
protectorHeath Stewart22-Mar-04 3:17 
GeneralRe: unknown exception thrown when decrypting read-in message Pin
Heath Stewart22-Mar-04 3:23
protectorHeath Stewart22-Mar-04 3:23 
GeneralRe: unknown exception thrown when decrypting read-in message Pin
chau_fai_rubbish22-Mar-04 15:00
chau_fai_rubbish22-Mar-04 15:00 
GeneralRe: unknown exception thrown when decrypting read-in message Pin
Heath Stewart23-Mar-04 2:56
protectorHeath Stewart23-Mar-04 2:56 
GeneralRe: unknown exception thrown when decrypting read-in message Pin
terry_cyf23-Mar-04 20:40
terry_cyf23-Mar-04 20:40 
GeneralRe: unknown exception thrown when decrypting read-in message Pin
Heath Stewart24-Mar-04 2:46
protectorHeath Stewart24-Mar-04 2:46 
GeneralFormatting Strings Pin
Maharishi Bhatia21-Mar-04 18:35
Maharishi Bhatia21-Mar-04 18:35 
GeneralRe: Formatting Strings Pin
Heath Stewart21-Mar-04 18:56
protectorHeath Stewart21-Mar-04 18:56 
GeneralRe: Formatting Strings Pin
Spanky321-Mar-04 21:12
Spanky321-Mar-04 21:12 
GeneralRe: Formatting Strings Pin
Heath Stewart22-Mar-04 3:09
protectorHeath Stewart22-Mar-04 3:09 
GeneralRe: Formatting Strings Pin
Judah Gabriel Himango22-Mar-04 4:01
sponsorJudah Gabriel Himango22-Mar-04 4:01 
GeneralRe: Formatting Strings Pin
Heath Stewart22-Mar-04 4:11
protectorHeath Stewart22-Mar-04 4:11 
GeneralRe: Formatting Strings Pin
Alvaro Mendez22-Mar-04 10:08
Alvaro Mendez22-Mar-04 10:08 
GeneralRe: Formatting Strings Pin
Heath Stewart22-Mar-04 11:45
protectorHeath Stewart22-Mar-04 11:45 
GeneralRe: Formatting Strings Pin
Alvaro Mendez22-Mar-04 10:34
Alvaro Mendez22-Mar-04 10:34 
Generalclipboard Pin
cmarmr21-Mar-04 16:49
cmarmr21-Mar-04 16:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.