my program takes a txt file and encrypts it successfully but it returns the wrong thing when decryting
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
Make sure your code decrypts the file the same way as encryption only backwards
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.
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.
ok i fixed it. ![]()
my key needed to be letters also or else it was impossible to reverse the process.
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)