Printable Version of Topic

Click here to view this topic in its original format

Dream.In.Code _ VB.NET _ encryption of txt files

Posted by: some_kid 2 Jun, 2009 - 06:07 AM

my program takes a txt file and encrypts it successfully but it returns the wrong thing when decryting

vb

Public Class Form1
Dim letters() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "/", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", ",", ".", "<", ">", "?", "`", "~", "'", "\", "|"}
Dim key() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86}


Private Sub Browse_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"
OpenFileDialog1.Title = "select a file"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
End If
End Sub

Private Sub Encrypt_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RandomizeArray(key)
RandomizeArray(key)
RandomizeArray(key)
Dialog1.ShowDialog()
If Dialog1.DialogResult = Windows.Forms.DialogResult.OK Then
encryption(TextBox1.Text, TextBox2.Text)
End If
End Sub

Private Sub RandomizeArray(ByVal items() As Integer)
Dim max_index As Integer = items.Length - 1
Dim rnd As New Random
For i As Integer = 0 To max_index - 1
' Pick an item for position i.
Dim j As Integer = rnd.Next(i, max_index + 1)

' Swap them.
Dim temp As Integer = items(i)
items(i) = items(j)
items(j) = temp
Next i
End Sub

Private Sub encryption(ByVal source As String, ByVal destination As String)
If System.IO.File.Exists(destination) = True Then
System.IO.File.Delete(destination)
End If

If System.IO.File.Exists(source) = True Then

Dim objReader As New System.IO.StreamReader(source)
Dim objwriter As New System.IO.StreamWriter(destination, True)
Dim contents As String
Dim strchar As String = ""

contents = objReader.ReadLine + vbNewLine
While objReader.Peek() <> -1
contents = contents + objReader.ReadLine + vbNewLine
End While

For a = 0 To key.Length - 1
objwriter.Write(key(a))
objwriter.Write(",")
Next
objwriter.WriteLine()

For i = 1 To Len(contents)
strchar = Mid(contents, i, 1)
'Do whatever you want with the character
Dim location As Integer
If strchar.Trim <> "" Then
location = Array.IndexOf(letters, strchar)
location = Array.IndexOf(key, location)
If location = -1 Then
objwriter.Write(strchar)
Else
objwriter.Write(letters(location))
End If

Else
objwriter.Write(strchar)
End If
Next i
objwriter.Close()
MsgBox("file encrypted")
Else
MsgBox("File Does Not Exist")
End If
End Sub

Private Sub get_key(ByVal source As String)
Dim objreader As New System.IO.StreamReader(source)
Dim contents As String
Dim key_2() As String
Dim a As Integer = 0

contents = objreader.ReadLine()
key_2 = Split(contents, ",")

While a < key.Length
key(a) = Convert.ToInt32(key_2(a))
a += 1
End While
End Sub

Private Sub decrypt_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dialog1.ShowDialog()
If DialogResult.OK Then
get_key(TextBox1.Text)
decryption(TextBox1.Text, TextBox2.Text)
End If
End Sub

Private Sub decryption(ByVal source As String, ByVal destination As String)
If System.IO.File.Exists(destination) = True Then
System.IO.File.Delete(destination)
End If

If System.IO.File.Exists(source) = True Then

Dim objReader As New System.IO.StreamReader(source)
Dim objwriter As New System.IO.StreamWriter(destination, True)
Dim contents As String
Dim strchar As String = ""

contents = objReader.ReadLine
contents = ""
While objReader.Peek() <> -1
contents = contents + objReader.ReadLine + vbNewLine
End While


For i = 1 To Len(contents)
strchar = Mid(contents, i, 1)
'Do whatever you want with the character
Dim location As Integer
If strchar.Trim <> "" Then
location = Array.IndexOf(letters, strchar)
location = Array.IndexOf(key, location)
If location = -1 Then
objwriter.Write(strchar)
Else
objwriter.Write(letters(location))
End If

Else
objwriter.Write(strchar)
End If
Next i
objwriter.Close()
MsgBox("File Decrypted")
Else
MsgBox("File Does Not Exist")
End If
End Sub
End Class


if i take test.txt which contains

CODE

hi
hello
what up people
message
cant read this can you


it comes out in test_file.txt as

CODE

85,76,25,66,42,46,55,74,56,69,50,49,8,9,22,23,14,45,83,3,35,39,11,64,34,24,15,40
,27,17,65,41,19,78,37,70,52,30,48,62,51,29,33,54,59,84,86,81,43,28,12,31,82,63,5
8,21,73,47,53,38,2,6,0,72,10,20,32,16,61,7,5,18,68,75,1,71,79,44,67,26,13,4,77,8
0,57,36,60,
&m
&?wwq
o&/7 #1 1?q1w?
O?((/Z?
Y/>7 4?/t 7&m( Y/> zq#


after decryting it comes out in decrypt_test.txt as

CODE

jO
jLoo%
qjD$ 5< <L%<oL
kL++D^L
|D~$ FLD7 $jO+ |D~ c%5


can someone help me fix the decrytion

Posted by: delicowa 2 Jun, 2009 - 10:45 AM

Make sure your code decrypts the file the same way as encryption only backwards

Posted by: MajorWalrus 2 Jun, 2009 - 01:57 PM

I have two thoughts for you. Firstly, your RandomizeArray sub could be the source of your troubles. It randomizes the key array which is good for enciphering, but bad for deciphering. This is because there isn't any way for you to undo the randomness you introduced.

A cryptosystem uses randomness, yes, but there's always a way to remove it upon deciphering. Usually this is done with a key to seed the random numbers.

Secondly, and this goes to my above point, as well as what delicowa said, it can sometimes be good to write out (by hand, on paper) the steps in a cryptosystem. Don't try to write code, just broad steps. Doing this will allow you to catch elusive mistakes.

Also, write out any equations you use to encipher a character. Then, write out the equation that does the exact opposite. If you can't, then you'll need to revisit your premise.

Posted by: some_kid 3 Jun, 2009 - 05:35 AM

To get the key back it writes the key into the encrypted, that was all the numbers, and when decryting it reads them back.

my problem is i am not sure how to reverse the proccess to get the original file back.

Posted by: some_kid 3 Jun, 2009 - 06:08 AM

ok i fixed it. biggrin.gif

my key needed to be letters also or else it was impossible to reverse the process. icon_up.gif

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)