Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I got stuck with streamwriter and cryptostream classes

I got stuck with streamwriter and cryptostream classes

Scheduled Pinned Locked Moved C#
helpsysadminsecuritytutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohammed Aijaz Mohiuddin
    wrote on last edited by
    #1

    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.

    K 1 Reply Last reply
    0
    • M Mohammed Aijaz Mohiuddin

      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.

      K Offline
      K Offline
      Kodanda Pani
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • K Kodanda Pani

        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

        M Offline
        M Offline
        Mohammed Aijaz Mohiuddin
        wrote on last edited by
        #3

        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.

        Z K 2 Replies Last reply
        0
        • M Mohammed Aijaz Mohiuddin

          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.

          Z Offline
          Z Offline
          zagzagzag
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • M Mohammed Aijaz Mohiuddin

            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.

            K Offline
            K Offline
            Kodanda Pani
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups