Sunday, March 3, 2013

cryptography encryption and decryption program


Buatlah form seperti gambar di atas dan isikan perintah di bawah ini ke dalam formnya :


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim encrypt As String = "" 'mendeklarasi variable
        Dim isi As Char 'mendeklarasi variable
        Dim i, charsInFile As Short 'mendeklarasi variable bisa menggunakan apa saja yang anda inginkan

        SaveFileDialog1.Filter = "Text file |*.txt" 'menyimpan file kepada notepad
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then 'akan menampilkan pesan dialog ketika kita menyimpan
            Try
                charsInFile = TextBox1.Text.Length '

                For i = 0 To charsInFile - 1 '
                    isi = TextBox1.Text.Substring(i, 1)
                    encrypt = encrypt & Chr(Asc(isi) + 13) 'melakukan encrypsi dan decrypsi Rot 13

                Next
                My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, encrypt, False) 'melakukan penulisan pesan yang akan di enkripsi dan disimpan pada notepad
                TextBox1.Text = encrypt '
                TextBox1.Select(0, 0)

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Information, "error") 'menampilkan pesan error ketika ada kesalahan
            End Try
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim tulisan As String
        Dim i, charsInFile As Short
        Dim isi As Char
        Dim decrypt As String = ""

        OpenFileDialog1.Filter = "Text file |*.txt" 'membuka file yang telah di simpan
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 'pesan dialog ketika kita hendak membuka file yang di simpan tadi
            If My.Computer.FileSystem.FileExists(OpenFileDialog1.FileName) Then
                Try
                    tulisan = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
                    charsInFile = tulisan.Length

                    For i = 0 To charsInFile - 1
                        isi = tulisan.Substring(i, 1)
                        decrypt = decrypt & Chr(Asc(isi) - 13)
                    Next i

                    TextBox1.Text = decrypt
                    TextBox1.Select(0, 0)

                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Information)
                End Try
            End If
        End If
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Text = ""
    End Sub
End Class

0 comments:

Post a Comment

 

Copyright @ 2013 Dunia Teknologi.