Using the Rijndael Object in VB.NET

Posted by broke on Stack Overflow See other posts from Stack Overflow or by broke
Published on 2010-04-20T13:35:15Z Indexed on 2010/04/20 14:33 UTC
Read the original article Hit count: 811

Filed under:
|
|

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:

  1. Getting the users computer name.

  2. Adding a random number between 100 and 1000000000

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).

Here is some sample codez:

 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

That's my load event, but here is where the magic happens:

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

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.

Thanks in advance

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net