You are on page 1of 2

Private Sub btnDeskripsi_Click(sender As Object, e As EventArgs) Handles

btnDeskripsi.Click
If txtChipText.Text = "" Or txtKey.Text = "" Then
MsgBox("tidak boleh kosong!")
Else
aturKunci("deskrip")
For i = 1 To pjgchiper
ci = Asc(Mid(txtChipText.Text, i)) - 97
k = Asc(Mid(kunci, i)) - 97
pi = (ci - k + 26) Mod 26
d = pi + 97
hsl = hsl + Chr(d)
Next i
lblHasilDes.Text = hsl
hsl = ""
End If

End Sub
Private Sub btnEnkripsi_Click(sender As Object, e As EventArgs) Handles
btnEnkripsi.Click
If txtPtext.Text = "" Or txtKey.Text = "" Then
MsgBox("tidak boleh kosong!")
Else
aturKunci("enkrip")
For i = 1 To pjgplain
pi = Asc(Mid(txtPtext.Text, i)) - 97
k = Asc(Mid(kunci, i)) - 97
ci = (pi + k) Mod 26
d = ci + 97
hsl = hsl + Chr(d)
Next i
txtChipText.Text = hsl
lblHasilEn.Text = hsl
hsl = ""
End If
End Sub
Private Sub aturKunci(jenis As String)
Dim jns As Integer
pjgkey = Len(txtKey.Text)

If jenis = "enkrip" Then


jns = Len(txtPtext.Text)
pjgplain = Len(txtPtext.Text)
Else
jns = Len(txtChipText.Text)
pjgchiper = Len(txtChipText.Text)
End If

If jns <= pjgkey Then 'kondisi bila inputan kurang dari kunci
kunci = txtKey.Text.Substring(0, jns) 'mengambil key sesuai panjang inputan
Else
kunci = ""
For i = 1 To Floor((jns / pjgkey))
kunci = kunci + txtKey.Text
Next i
i = jns Mod pjgkey
If i > 0 Then
kunci = kunci + (txtKey.Text.Substring(0, i))
End If
End If
End Sub

Private Sub txtPtext_KeyPress(sender As Object, e As KeyPressEventArgs) Handles


txtPtext.KeyPress
e.KeyChar = LCase(e.KeyChar) 'lower abjad menjadi kecil
Dim KeyAscii As Short = Asc(e.KeyChar)
If (e.KeyChar Like "[A-Za-z]" _
OrElse KeyAscii = Keys.Back _
OrElse KeyAscii = Keys.Return _
OrElse KeyAscii = Keys.Delete) Then
KeyAscii = 0
End If
e.Handled = CBool(KeyAscii)
End Sub

Private Sub txtKey_KeyPress(sender As Object, e As KeyPressEventArgs) Handles


txtKey.KeyPress
e.KeyChar = LCase(e.KeyChar)
Dim KeyAscii As Short = Asc(e.KeyChar)
If (e.KeyChar Like "[A-Za-z]" _
OrElse KeyAscii = Keys.Back _
OrElse KeyAscii = Keys.Return _
OrElse KeyAscii = Keys.Delete) Then
KeyAscii = 0
End If
e.Handled = CBool(KeyAscii)
End Sub

Private Sub txtChipText_KeyPress(sender As Object, e As KeyPressEventArgs) Handles


txtChipText.KeyPress
e.KeyChar = LCase(e.KeyChar)
Dim KeyAscii As Short = Asc(e.KeyChar)
If (e.KeyChar Like "[A-Za-z]" _
OrElse KeyAscii = Keys.Back _
OrElse KeyAscii = Keys.Return _
OrElse KeyAscii = Keys.Delete) Then
KeyAscii = 0
End If
e.Handled = CBool(KeyAscii)
End Sub
End Class

You might also like