I got stuck with streamwriter and cryptostream classes
-
My aim is to use the above specified classes for encryption and decryption on strings send across the network. I am unable to find how to use these two classes put together. Can anyone help me in this regard. Early response is encouraged.
-
My aim is to use the above specified classes for encryption and decryption on strings send across the network. I am unable to find how to use these two classes put together. Can anyone help me in this regard. Early response is encouraged.
Hi, 1) Create the key and IV 2) Create a crypto service provider 3) Create a crypto stream 4) Create new stream writer on the crypto stream byte [] key ={171, 196, 154, 92, 35, 195, 212, 50}; byte [] IV ={65, 86, 22, 91, 214, 4, 177, 199}; try { DESCryptoServiceProvider dsp = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms,dsp.CreateEncryptor(key,IV),CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cs); sw.Write(textBox1.Text); sw.Flush(); cs.FlushFinalBlock(); ms.Flush(); textBox2.Text = Convert.ToBase64String(ms.GetBuffer(),0, (int)(ms.Length)); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } Hope this will help you. Thanks and Regards SGS
-
Hi, 1) Create the key and IV 2) Create a crypto service provider 3) Create a crypto stream 4) Create new stream writer on the crypto stream byte [] key ={171, 196, 154, 92, 35, 195, 212, 50}; byte [] IV ={65, 86, 22, 91, 214, 4, 177, 199}; try { DESCryptoServiceProvider dsp = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms,dsp.CreateEncryptor(key,IV),CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cs); sw.Write(textBox1.Text); sw.Flush(); cs.FlushFinalBlock(); ms.Flush(); textBox2.Text = Convert.ToBase64String(ms.GetBuffer(),0, (int)(ms.Length)); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } Hope this will help you. Thanks and Regards SGS
I am intereseted in developing network application where a server and client will just pass the text messages. I am using Tcp sockets. I could not incorporate networkstream classes in the context. I mean to say that I have to work with networkstream, streamwriter or streamreader and cryptostream classes put together. Hope you understand the situation.
-
I am intereseted in developing network application where a server and client will just pass the text messages. I am using Tcp sockets. I could not incorporate networkstream classes in the context. I mean to say that I have to work with networkstream, streamwriter or streamreader and cryptostream classes put together. Hope you understand the situation.
Hi there.. There is a lot of examples on how to do the server client stuff in the .Net framework documentation. Search for "TCP" and you'll get something usefull! About the cryptographic stuff:
public string Encrypt(string ToCrypt) { string encoded = ""; byte [] EncKey = {0x16, 0x1F, 0x01, 0x03, 0x1A, 0x0B, 0x10, 0x7F, 0x65, 0x5E, 0x45, 0x3F, 0x11,0x1F, 0xFF, 0xEF, 0x32, 0xEE, 0x5F, 0x38, 0xA7, 0xB2, 0xCC, 0x77, 0x51, 0x14, 0x40, 0x8A, 0x95, 0xE5, 0x00, 0x84}; byte [] EncIV = {0x05, 0x14, 0x06, 0x15, 0x02, 0x13, 0x00, 0x0F, 0xC0, 0xD8, 0xB6, 0x58, 0xE4, 0xA9, 0x21, 0x5E}; RijndaelManaged rmEnc = new RijndaelManaged(); rmEnc.Key = EncKey; rmEnc.IV = EncIV; StreamWriter sw = new StreamWriter(encoded); CryptoStream cs = new CryptoStream(sw, rmEnc.CreateEncryptor(rmEnc.Key, rmEnc.IV), CryptoStreamMode.Write); cs.Write(Encoding.ASCII.GetBytes(ToCrypt), 0, ToCrypt.Length); cs.Close(); sw.Close(); return encoded; }
If this wasen't what you were looking for, you must express yourself more clearly. What is the exact problem? -
I am intereseted in developing network application where a server and client will just pass the text messages. I am using Tcp sockets. I could not incorporate networkstream classes in the context. I mean to say that I have to work with networkstream, streamwriter or streamreader and cryptostream classes put together. Hope you understand the situation.
Hi Aijaz, You can initialize the crypto stream class using any class that's derieved from Stream class. (Ex File Stream , Memory Stream, NetworkStream). Using NetworkStream: TcpClient client = new TcpClient(); NetworkStream ns = client.GetStream(); CryptoStream cs = new CryptoStream(ns,dsp.CreateEncryptor(key,IV),CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cs); Guess this will help you. Thanks and Regards SGS