Hi all,
I am working on a texteditor, it is really fun to learn vb.net! I am not trying to create the crypt and decrypt function, and I found a nice code on this forum made by CVMichael (http://www.vbforums.com/showthread.php?t=231798).

The code is nice, but I have a problem...
In my text editor i show the text in a textbox, and when I hit the "Crypt" button the text is crypted, a text like "The file is crypted" and the crypted text is shown in the textbox1.

Here is the problem, when I decrypt the file, text is lost. It can be read in the thread where I got the crypt function.

Is there anyway to fix this?

Yea, I could store the crypted text directly into the .txt file, but I whant it to be useble in more ways...

I really need a hand, I tryed to show the crypted text in a label instead of a textbox, but it is the same problem...

Help!


Crypt function:
Code:
Public Function RndCrypt(ByVal Str As String, ByVal Password As String) As String
        '
        '  Made by Michael Ciurescu (CVMichael from vbforums.com)
        '  Original thread: http://www.vbforums.com/showthread.php?t=231798
        '
        Dim SK As Long, K As Long

        ' init randomizer for password
        Rnd(-1)
        Randomize(Len(Password))
        ' (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd)) -> makes sure that a
        ' password like "pass12" does NOT give the same result as the password "sspa12" or "12pass"
        ' or "1pass2" etc. (or any combination of the same letters)

        For K = 1 To Len(Password)
            SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))
        Next K

        ' init randomizer for encryption/decryption
        Rnd(-1)
        Randomize(SK)

        ' encrypt/decrypt every character using the randomizer
        For K = 1 To Len(Str)
            Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))
        Next K

        RndCrypt = Str
    End Function

The event for crypt/decrypt button:
Code:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Check that password is correct        
Dim pw As String = InputBox("Please enter the password to crypt or decrypt file. Do not forget the password, wrong password will make the file corrupt.", "Crypt or Decrypt file")
        Dim pw2 As String = InputBox("Please enter the password again to make sure it is the same.", "Crypt or Decrypt file")
        If pw = pw2 Then


            'if the file is crypted
            Dim r As String = Label1.Text
            r = r.IndexOf("#37#37#2007#x3#39R90#")
            If r > 0 Then

                Dim textlabel As String = Label1.Text
                Dim krypteradtext, krypteradlasbar As String
                krypteradtext = textlabel.Substring(r)

                krypteradtext = krypteradtext.Substring(21)

                krypteradlasbar = RndCrypt(krypteradtext, "hej")
                Label1.Text = ""
                Label1.Visible = False
                TextBox1.Visible = True
                TextBox1.Text = krypteradlasbar
            Else

                Dim krypterad, text, txtbox As String
                txtbox = TextBox1.Text
                krypterad = RndCrypt(txtbox, "hej")

                text = "This file is crypted. DO NOT CHANGE THIS FILE. If you do the file will be corrupt. Please open the file with EasyNote and decrypt it in order to view or change it. #37#37#2007#x3#39R90#" & krypterad
                Label1.Text = text
                Label1.Visible = True
                TextBox1.Visible = False

            End If
        Else
            MsgBox("The password was not equal, please try again.")

        End If
    End Sub
Sorry but I dont have time right now to comment it.. Gah... I´ll maybe do it tomorrow... Have to go, thanks for a little hand!
Best Regards,
Johan
Sweden