encryption of txt files

encryption works - decrytion doesnt

Page 1 of 1

4 Replies - 3706 Views - Last Post: 03 June 2009 - 07:08 AM Rate Topic: -----

#1 some_kid  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 43
  • Joined: 26-May 09

encryption of txt files

Posted 02 June 2009 - 07:07 AM

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



if i take test.txt which contains

hi
hello
what up people
message
cant read this can you



it comes out in test_file.txt as

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,58,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,80,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

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



can someone help me fix the decrytion

Is This A Good Question/Topic? 1
  • +

Replies To: encryption of txt files

#2 delicowa  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 25-September 08

Re: encryption of txt files

Posted 02 June 2009 - 11:45 AM

Make sure your code decrypts the file the same way as encryption only backwards
Was This Post Helpful? 1

#3 MajorWalrus  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 115
  • Joined: 22-April 09

Re: encryption of txt files

Posted 02 June 2009 - 02: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.
Was This Post Helpful? 1

#4 some_kid  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 43
  • Joined: 26-May 09

Re: encryption of txt files

Posted 03 June 2009 - 06: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.
Was This Post Helpful? 0
  • +
  • -

#5 some_kid  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 43
  • Joined: 26-May 09

Re: encryption of txt files

Posted 03 June 2009 - 07:08 AM

ok i fixed it. :D

my key needed to be letters also or else it was impossible to reverse the process. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1