Results 1 to 15 of 15

Thread: How to build a texteditor with crypt/decrypt?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    How to build a texteditor with crypt/decrypt?

    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

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to build a texteditor with crypt/decrypt?

    you should really use some of the cryptography classes that are built into the .NET framework. I am not dissing that code bank submission, but it was written for VB6, which didn't have any built in cryptography.

    Look at tripleDES (to name one) which provides good encryption and is symmetrical which allows encrypt/decrypt using the same key pair.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: How to build a texteditor with crypt/decrypt?

    Thank you, I will have a look at it!

    Best Regards,
    Johan
    Sweden

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: How to build a texteditor with crypt/decrypt?

    Oh.. I am stuck... I really need to use a password.. Its more safe. The code I got works fine, its just that some characters is lost when I show it in a textbox or label.. Is there no way to fix this or maybe cryptate in another way but still with a password?

    B. Regs
    Johan

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to build a texteditor with crypt/decrypt?

    I will see if I can create you a small sample

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to build a texteditor with crypt/decrypt?

    what version of .NET are you using?

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: How to build a texteditor with crypt/decrypt?

    Check this example out. I found it very useful:

    Encrypt / Decrypt
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: How to build a texteditor with crypt/decrypt?

    Quote Originally Posted by stimbo
    Check this example out. I found it very useful:

    Encrypt / Decrypt

    I´ll try that right away! Thanks.
    Iam using Vb.net 7.1.. With service pack 1.

    Thanks guys..

    B. Regs
    Johan

    EDIT: That was not what I was looking for, its one way only as far as I know? But thanks anway.
    Last edited by Johan_S; Jan 17th, 2007 at 01:31 AM.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to build a texteditor with crypt/decrypt?

    I will try to make you a sample in VS 2003 to show you how to use TripleDES

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: How to build a texteditor with crypt/decrypt?

    Quote Originally Posted by kleinma
    I will try to make you a sample in VS 2003 to show you how to use TripleDES
    Thanks!

    B. Regs
    Johan

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: How to build a texteditor with crypt/decrypt?

    I am just stuck.. Eh..

    Is there no good crypt/encrypt functions out there? It dont have do be super secure... Just so normal people cant read it. (With pw!)

    Know anyone?

    B. Regs
    Johan

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to build a texteditor with crypt/decrypt?

    sorry I have just been busy. I will try to complete the sample I started for you.

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: How to build a texteditor with crypt/decrypt?

    YEA! I made it!

    This is how I did it:
    Instead of using the textbox, or a label I store it in a text file (.txt). And when I have stored the crypted text into the file, I read the file and put it into the textbox AFTER I decrypted it. And yea, it works!

    The code is REALLY bad, I have to look at it for a while.. Lot of crap.. But, it works. And that was my goal. Please dont use the code, if u do please look at it due to a lot of "bad" solutions maybe.. I am really new to Vb.

    Code:
    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 the pw is the same
            If pw = pw2 Then
    
                'Does the "tag" exist? I add this when I crypt it, and if it is there, its crypted.
                Dim r As String = TextBox1.Text()
                r = r.IndexOf("#37#37#2007#x3#39R90#")
    
                'Decrypt
                If r > 0 Then
    
    
                    Dim textboxen As String = TextBox1.Text
                    Dim krypteradtext, krypteradlasbar As String
                    'Substring where the "tag" starts. So I just work with the crypted text
                    krypteradtext = textboxen.Substring(r)
    
                    'Another substring, all text after the "tag" is crypted
                    krypteradtext = krypteradtext.Substring(21)
    
                    'Decrypt it with pw
                    krypteradlasbar = RndCrypt(krypteradtext, pw)
                    TextBox1.Text = krypteradlasbar
                Else
    
                    Dim krypterad, text, txtbox As String
                    txtbox = TextBox1.Text
                    krypterad = RndCrypt(txtbox, pw)
    
                    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
    
    
                    'Filtrera bort alla onödiga filformat
                    SaveFileDialog1.Filter = "Both *.txt and *.easynote files|*.txt;*.easynote|Text Files(*.txt)|*.txt|Easynote files(*.easynote)|*.easynote"
    
                    'Om man tryckt på Ok vid sparning
                    If SaveFileDialog1.ShowDialog = DialogResult.OK Then
    
    
                        'Skriv text till fil, FALSE säger att filen ska skapas om den inte finns.
                        Dim objWriter As New System.IO.StreamWriter(SaveFileDialog1.FileName, False)
    
    
                        objWriter.Write(text)
                        objWriter.Close()
    
                    End If
                End If
    
                'Börja läsa in filen
                Dim objReader As New System.IO.StreamReader(SaveFileDialog1.FileName)
                Dim lasintxt As String
    
                lasintxt = objReader.ReadToEnd
                objReader.Close()
    
                Dim u As String = lasintxt
                u = u.IndexOf("#37#37#2007#x3#39R90#")
    
                Dim krypteradtext2, krypteradlasbar2 As String
                krypteradtext2 = lasintxt.Substring(u)
    
                krypteradtext2 = krypteradtext2.Substring(21)
    
                krypteradlasbar2 = RndCrypt(krypteradtext2, pw)
                TextBox1.Text = krypteradlasbar2
    
            Else
                MsgBox("The password was not equal, please try again.")
    
            End If
        End Sub
    Thanks Kleinma, hopa you dont spend hours to help me out! If you do, it would be really intressting to look at your code.

    B Regs
    Johan

    EDIT: Hm... Well.. Kind of worked... Does it sometimes. Eh. Dont use the code, It has to be fixed. But I am in the right direction.
    Last edited by Johan_S; Jan 18th, 2007 at 10:11 AM.

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to build a texteditor with crypt/decrypt?

    well since you already got it, I wont bother to finish the example (since I have about a million things on my plate today)

    however, here is a good class you can use for encryption if you want to make it a bit more secure

    VB Code:
    1. Imports System.IO
    2. Imports System.Text
    3. Imports System.Security.Cryptography
    4.  
    5. Public Class TripleDES
    6.  
    7.     ' define the triple des provider
    8.     Private m_des As New TripleDESCryptoServiceProvider
    9.  
    10.     ' define the string handler
    11.     Private m_utf8 As New UTF8Encoding
    12.  
    13.     ' define the local property arrays
    14.     Private m_key() As Byte
    15.     Private m_iv() As Byte
    16.  
    17.     Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
    18.         Me.m_key = key
    19.         Me.m_iv = iv
    20.     End Sub
    21.  
    22.     Public Function Encrypt(ByVal input() As Byte) As Byte()
    23.         Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
    24.     End Function
    25.  
    26.     Public Function Decrypt(ByVal input() As Byte) As Byte()
    27.         Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
    28.     End Function
    29.  
    30.     Public Function Encrypt(ByVal text As String) As String
    31.         Dim input() As Byte = m_utf8.GetBytes(text)
    32.         Dim output() As Byte = Transform(input, _
    33.                         m_des.CreateEncryptor(m_key, m_iv))
    34.         Return Convert.ToBase64String(output)
    35.     End Function
    36.  
    37.     Public Function Decrypt(ByVal text As String) As String
    38.         Dim input() As Byte = Convert.FromBase64String(text)
    39.         Dim output() As Byte = Transform(input, _
    40.                          m_des.CreateDecryptor(m_key, m_iv))
    41.         Return m_utf8.GetString(output)
    42.     End Function
    43.  
    44.     Private Function Transform(ByVal input() As Byte, _
    45.         ByVal CryptoTransform As ICryptoTransform) As Byte()
    46.         ' create the necessary streams
    47.         Dim memStream As MemoryStream = New MemoryStream
    48.         Dim cryptStream As CryptoStream = New _
    49.             CryptoStream(memStream, CryptoTransform, _
    50.             CryptoStreamMode.Write)
    51.         ' transform the bytes as requested
    52.         cryptStream.Write(input, 0, input.Length)
    53.         cryptStream.FlushFinalBlock()
    54.         ' Read the memory stream and convert it back into byte array
    55.         memStream.Position = 0
    56.         Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
    57.         memStream.Read(result, 0, CType(result.Length, System.Int32))
    58.         ' close and release the streams
    59.         memStream.Close()
    60.         cryptStream.Close()
    61.         ' hand back the encrypted buffer
    62.         Return result
    63.     End Function
    64.  
    65. End Class

    When you create an instance, you pass in 2 byte arrays to create the encryption.. it would look something like this

    VB Code:
    1. Dim MyKey() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    2. Dim MyIV() As Byte = {1, 2, 3, 4, 5, 6, 7, 8}
    3. Dim MyEncryption as New TripleDES(MyKey, MyIV)

    Obviously you would not want to use a byte array that goes 1234567..etc.. like the one above, but its just an example.

    Now since you want to use a password, if I was making this app, I would convert the characters in their password to their byte equivelant, and use that to make the key for the encyrption.

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: How to build a texteditor with crypt/decrypt?

    Ah, great! I´ll try to understand it. And here we go, now it works. Button1 for crypt, button2 for decrypt. Feel free if someone could use it..

    Code:
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            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 the pw is the same
            If pw = pw2 Then
    
                Dim krypterad, text, txtbox As String
                    txtbox = TextBox1.Text
                    krypterad = RndCrypt(txtbox, pw)
    
                    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
    
    
                    'Filtrera bort alla onödiga filformat
                    SaveFileDialog1.Filter = "Both *.txt and *.easynote files|*.txt;*.easynote|Text Files(*.txt)|*.txt|Easynote files(*.easynote)|*.easynote"
    
                    'Om man tryckt på Ok vid sparning
                    If SaveFileDialog1.ShowDialog = DialogResult.OK Then
    
    
                        'Skriv text till fil, FALSE säger att filen ska skapas om den inte finns.
                        Dim objWriter As New System.IO.StreamWriter(SaveFileDialog1.FileName, False)
    
    
                        objWriter.Write(text)
                    objWriter.Close()
    
                    TextBox1.Text = "The file crypted successfully." & vbNewLine & "Please select 'Decrypt a file' and locate the file to decrypt."
    
                    End If
    
            Else
                MsgBox("The password was not equal, please try again.")
    
            End If
        End Sub
    
        Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            'Filtrera bort alla onödiga filformat
            OpenFileDialog1.Filter = "Both *.txt and *.easynote files|*.txt;*.easynote|Text Files(*.txt)|*.txt|Easynote files(*.easynote)|*.easynote"
    
            'Om Ok knappen tryckts efter fil vald
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
    
                'Säkra att filen finns
                If System.IO.File.Exists(OpenFileDialog1.FileName) = True Then
    
                    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 the pw is the same
                    If pw = pw2 Then
                        'Börja läsa in filen
                        Dim objReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
                        Dim lasintxt As String
    
                        lasintxt = objReader.ReadToEnd
                        objReader.Close()
                        Dim u As String = lasintxt
                        u = u.IndexOf("#37#37#2007#x3#39R90#")
    
                        Dim krypteradtext2, krypteradlasbar2 As String
                        krypteradtext2 = lasintxt.Substring(u)
    
                        krypteradtext2 = krypteradtext2.Substring(21)
    
                        krypteradlasbar2 = RndCrypt(krypteradtext2, pw)
                        TextBox1.Text = krypteradlasbar2
    
    
                    Else
                        'Om inte filen finns, visa MsgBox
                        MsgBox("The selected file does not exist.")
    
                    End If
                End If
            Else
                MsgBox("The password was not equal, please try again.")
            End If
    
    
    
    
           
        End Sub
    B. Regs
    Johan


    EDIT: Thank you, very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width