|
-
Jan 15th, 2007, 10:18 AM
#1
Thread Starter
New Member
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
-
Jan 15th, 2007, 12:18 PM
#2
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.
-
Jan 15th, 2007, 12:20 PM
#3
Thread Starter
New Member
Re: How to build a texteditor with crypt/decrypt?
Thank you, I will have a look at it!
Best Regards,
Johan
Sweden
-
Jan 16th, 2007, 03:53 AM
#4
Thread Starter
New Member
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
-
Jan 16th, 2007, 09:07 AM
#5
Re: How to build a texteditor with crypt/decrypt?
I will see if I can create you a small sample
-
Jan 16th, 2007, 09:10 AM
#6
Re: How to build a texteditor with crypt/decrypt?
what version of .NET are you using?
-
Jan 16th, 2007, 09:11 AM
#7
Re: How to build a texteditor with crypt/decrypt?
Check this example out. I found it very useful:
Encrypt / Decrypt
-
Jan 17th, 2007, 01:27 AM
#8
Thread Starter
New Member
Re: How to build a texteditor with crypt/decrypt?
 Originally Posted by stimbo
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.
-
Jan 17th, 2007, 09:25 AM
#9
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
-
Jan 17th, 2007, 09:29 AM
#10
Thread Starter
New Member
Re: How to build a texteditor with crypt/decrypt?
 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
-
Jan 18th, 2007, 07:33 AM
#11
Thread Starter
New Member
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
-
Jan 18th, 2007, 09:24 AM
#12
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.
-
Jan 18th, 2007, 09:54 AM
#13
Thread Starter
New Member
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.
-
Jan 18th, 2007, 10:14 AM
#14
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:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class TripleDES
' define the triple des provider
Private m_des As New TripleDESCryptoServiceProvider
' define the string handler
Private m_utf8 As New UTF8Encoding
' define the local property arrays
Private m_key() As Byte
Private m_iv() As Byte
Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
Me.m_key = key
Me.m_iv = iv
End Sub
Public Function Encrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
End Function
Public Function Decrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
End Function
Public Function Encrypt(ByVal text As String) As String
Dim input() As Byte = m_utf8.GetBytes(text)
Dim output() As Byte = Transform(input, _
m_des.CreateEncryptor(m_key, m_iv))
Return Convert.ToBase64String(output)
End Function
Public Function Decrypt(ByVal text As String) As String
Dim input() As Byte = Convert.FromBase64String(text)
Dim output() As Byte = Transform(input, _
m_des.CreateDecryptor(m_key, m_iv))
Return m_utf8.GetString(output)
End Function
Private Function Transform(ByVal input() As Byte, _
ByVal CryptoTransform As ICryptoTransform) As Byte()
' create the necessary streams
Dim memStream As MemoryStream = New MemoryStream
Dim cryptStream As CryptoStream = New _
CryptoStream(memStream, CryptoTransform, _
CryptoStreamMode.Write)
' transform the bytes as requested
cryptStream.Write(input, 0, input.Length)
cryptStream.FlushFinalBlock()
' Read the memory stream and convert it back into byte array
memStream.Position = 0
Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
memStream.Read(result, 0, CType(result.Length, System.Int32))
' close and release the streams
memStream.Close()
cryptStream.Close()
' hand back the encrypted buffer
Return result
End Function
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:
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}
Dim MyIV() As Byte = {1, 2, 3, 4, 5, 6, 7, 8}
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.
-
Jan 18th, 2007, 10:20 AM
#15
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|