title: Using the Rijndael Object in VB.NET
body: <p>I'm trying out the Rijndael to generate an encrypted license string to use for our new software, so we know that our customers are using the same amount of apps that they paid for.  I'm doing two things:</p>
  
  <ol>
  <li><p>Getting the users computer name.</p></li>
  <li><p>Adding a random number between 100 and 1000000000</p></li>
  </ol>
  
  <p>I then combine the two, and use that as the license number(This probably will change in the final version, but I'm just doing something simple for demonstration purposes).</p>
  
  <p>Here is some sample codez:</p>
  
  <pre><code> Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim generator As New Random
      Dim randomValue As Integer
      randomValue = generator.Next(100, 1000000000)
  
  
      ' Create a new Rijndael object to generate a key
      ' and initialization vector (IV).
      Dim RijndaelAlg As Rijndael = Rijndael.Create
  
      ' Create a string to encrypt.
      Dim sData As String = My.Computer.Name.ToString + randomValue.ToString
      Dim FileName As String = "C:\key.txt"
  
      ' Encrypt text to a file using the file name, key, and IV.
      EncryptTextToFile(sData, FileName, RijndaelAlg.Key, RijndaelAlg.IV)
  
      ' Decrypt the text from a file using the file name, key, and IV.
      Dim Final As String = DecryptTextFromFile(FileName, RijndaelAlg.Key, RijndaelAlg.IV)
  
      txtDecrypted.Text = Final
  
  
  End Sub
  </code></pre>
  
  <p>That's my load event, but here is where the magic happens:</p>
  
  <pre><code>Sub EncryptTextToFile(ByVal Data As String, ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte)
  
      Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
      Dim RijndaelAlg As Rijndael = Rijndael.Create
      Dim cStream As New CryptoStream(fStream, _
                                     RijndaelAlg.CreateEncryptor(Key, IV), _
                                     CryptoStreamMode.Write)
      Dim sWriter As New StreamWriter(cStream)
  
      sWriter.WriteLine(Data)
      sWriter.Close()
      cStream.Close()
      fStream.Close()
  
  
  
  End Sub
  </code></pre>
  
  <p>There is a couple things I don't understand.  What if someone reads the text file and recognizes that it is Rijndael, and writes a VB or C# app that decrypts it?  I don't really understand all of this code, so if you guys can help me out I will love you all forever.</p>
  
  <p>Thanks in advance</p>