Kamis, 16 Januari 2014

Kriptografi pada VB.Net



Buatlah Pada Form 1,gambarnya seperti dibawah ini :

Lalu copykan code ini :
Public Class Form1

    Private Sub CaesarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CaesarToolStripMenuItem.Click
        Form2.Show()
    End Sub

    Private Sub VernamToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VernamToolStripMenuItem.Click
        Form3.Show()
    End Sub

    Private Sub GrensfeldToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GrensfeldToolStripMenuItem.Click
        Form4.Show()
    End Sub

    Private Sub VenoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VenoToolStripMenuItem.Click
        Form5.Show()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub

End Class

Lalu pada Form 2, Buatlah seperti di bawah ini tampilannya :

Lalu klik double form 2, masukkan kode berikut ini :
Public Class Form2

    Private Sub Btnenkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnenkripsi.Click
        Dim x As String = ""
        Dim xkalimat As String = ""
        For i = 1 To Len(plain.Text)
            x = Mid(plain.Text, i, i)
            x = Chr(Asc(x) + 3)
            xkalimat = xkalimat + x
        Next
        chiper.Text = xkalimat
    End Sub

    Private Sub Btndeskripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btndeskripsi.Click
        Dim x As String = ""
        Dim xenkripsi As String = ""
        For i = 1 To Len(chiper.Text)
            x = Mid(chiper.Text, i, i)
            x = Chr(Asc(x) - 3)
            xenkripsi = xenkripsi + x
        Next
        chiper.Text = xenkripsi
    End Sub
End Class

Lalu pada Form3, buatlah seperti gambar dibawah ini :

Kemudian klik double form 3 tersebut dan masukkan code berikut ini :
Public Class Form3

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        plaintext.Text = ""
        kunci.Text = ""
        chippertext.Text = ""
    End Sub
    Private Sub enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enkripsi.Click
        Dim J As Integer
        Dim jum As Integer
        Dim skey As String
        Dim nkata As Integer
        Dim nkunci As Integer
        Dim skata As String
        Dim splain As String = ""
        Dim nenc As Integer
        J = 0
        skata = plaintext.Text
        jum = Len(skata)
        skey = kunci.Text
        For i = 1 To jum
            If J = Len(skey) Then
                J = 1
            Else
                J = J + 1
            End If
            nkata = Asc(Mid(skata, i, 1)) - 65
            nkunci = Asc(Mid(skey, J, 1)) - 65
            nenc = ((nkata + nkunci) Mod 26)
            splain = splain & Chr((nenc) + 65)
        Next i
        chippertext.Text = splain
    End Sub

    Private Sub plaintext_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles plaintext.KeyPress
        e.KeyChar = UCase(e.KeyChar)
        Dim tombol As Integer = Asc(e.KeyChar)
        If Not (((tombol >= 65) And (tombol <= 90)) Or (tombol = 8)) Then
            e.Handled = True
        End If
    End Sub

    Private Sub kunci_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles kunci.KeyPress
        e.KeyChar = UCase(e.KeyChar)
        Dim tombol As Integer = Asc(e.KeyChar)
        If Not (((tombol >= 65) And (tombol <= 90)) Or (tombol = 8)) Then
            e.Handled = True
        End If
    End Sub
End Class

Pada Form4 buatlah sepeti ini :

Lalu buatlah code berikut ini :
Public Class Form4

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        plaintext.Text = ""
        kunci.Text = ""
        chippertext.Text = ""
    End Sub

    Private Sub enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enkripsi.Click
        Dim J As Integer
        Dim jum As Integer
        Dim skey As String
        Dim nkata As Integer
        Dim nkunci As Integer
        Dim skata As String
        Dim splain As String = ""
        Dim nenc As Integer
        J = 0
        skata = plaintext.Text
        jum = Len(skata)
        skey = kunci.Text
        For i = 1 To jum
            If J = Len(skey) Then
                J = 1
            Else
                J = J + 1
            End If
            nkata = Asc(Mid(skata, i, 1)) - 26
            nkunci = Asc(Mid(skey, J, 1)) - 10
            nenc = ((nkata + nkunci) Mod 26)
            splain = splain & Chr((nenc) + 65)
        Next i
        chippertext.Text = splain
    End Sub

    Private Sub plaintext_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles plaintext.KeyPress
        e.KeyChar = UCase(e.KeyChar)
        Dim tombol As Integer = Asc(e.KeyChar)
        If Not (((tombol >= 65) And (tombol <= 90)) Or (tombol = 8)) Then
            e.Handled = True
        End If
    End Sub

    Private Sub kunci_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles kunci.KeyPress
        e.KeyChar = UCase(e.KeyChar)
        Dim tombol As Integer = Asc(e.KeyChar)
        If Not (((tombol >= 48) And (tombol <= 57)) Or (tombol = 8)) Then
            e.Handled = True
        End If
    End Sub
End Class

Pada Fom5 bentuklah tampilannya seperti di bawah ini :


Lalu pastekan code di bawah ini pada form5 :
Public Class Form5

    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        plaintext.Text = ""
        kunci.Text = ""
        chippertext.Text = ""
    End Sub

    Private Sub enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enkripsi.Click
        Dim J As Integer
        Dim jum As Integer
        Dim skey As String
        Dim nkata As Integer
        Dim nkunci As Integer
        Dim skata As String
        Dim splain As String = ""
        Dim nenc As Integer
        J = 0
        skata = plaintext.Text
        jum = Len(skata)
        skey = kunci.Text
        For i = 1 To jum
            If J = Len(skey) Then
                J = 1
            Else
                J = J + 1
            End If
            nkata = Asc(Mid(skata, i, 1)) - 65
            nkunci = Asc(Mid(skey, J, 1)) - 65
            nenc = ((nkata + nkunci) Mod 26)
            splain = splain & Chr((nenc) + 65)
        Next i
        chippertext.Text = splain
    End Sub

    Private Sub plaintext_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles plaintext.KeyPress
        e.KeyChar = UCase(e.KeyChar)
        Dim tombol As Integer = Asc(e.KeyChar)
        If Not (((tombol >= 65) And (tombol <= 90)) Or (tombol = 8)) Then
            e.Handled = True
        End If
    End Sub

    Private Sub kunci_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles kunci.KeyPress
        e.KeyChar = UCase(e.KeyChar)
        Dim tombol As Integer = Asc(e.KeyChar)
        If Not (((tombol >= 32) And (tombol <= 47)) Or (tombol = 8)) Then
            e.Handled = True
        End If
    End Sub
End Class


Selesai Deh. Coba anda jalankan dengan menekan tombol F5

Tidak ada komentar:

Posting Komentar