Vous êtes sur la page 1sur 6

Public Class Form1

Dim huruf(26) As Char


Dim i, p, k, c As Integer
Dim plainText, cipherText As String
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
' Mengisi array abjad A s/d Z, Kode ASCII A=65
For i = 0 To 25
huruf(i) = Chr(65 + i)
Next i
End Sub
Private Sub ButtonEncrypt_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ButtonEncrypt.Click
cipherText = ""
k = CInt(TextBoxGeser.Text)
If (TextBoxPlain.Text.Length <> 0) Then
For i = 0 To TextBoxPlain.Text.Length - 1
p = Asc(TextBoxPlain.Text.Substring(i, 1)) - 65
c = (p + k) Mod 26
cipherText = String.Concat(cipherText, huruf(c))
Next i
TextBoxCipher.Text = cipherText
End If
End Sub
Private Sub ButtonDecrypt_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ButtonDecrypt.Click
Dim geserKiri As Integer
plainText = ""
k = CInt(TextBoxGeser.Text)

geserKiri = 26 - (k Mod 26)


If (TextBoxCipher.Text.Length <> 0) Then
For i = 0 To TextBoxCipher.Text.Length - 1
c = Asc(TextBoxCipher.Text.Substring(i, 1)) - 65
p = (c + geserKiri) Mod 26
plainText = String.Concat(plainText, huruf(p))
Next i
TextBoxPlain2.Text = plainText
End If
End Sub
Poly Alphabetic Cipher
Public Class Form1
Dim huruf(26) As Char
Dim i, p, k, c As Integer
Dim plainText, cipherText As String
Dim kunci As String
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
' Mengisi array abjad A s/d Z, Kode ASCII A=65
For i = 0 To 25
huruf(i) = Chr(65 + i)
Next i
End Sub
Private Sub ButtonEncrypt_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ButtonEncrypt.Click
cipherText = ""
If (TextBoxPlain.TextLength <> 0 And
TextBoxKunci.TextLength <> 0) Then
aturKunci()
For i = 0 To TextBoxPlain.TextLength - 1

p = Asc(TextBoxPlain.Text.Substring(i, 1)) - 65
k = Asc(kunci.Substring(i, 1)) - 65
c = (p + k) Mod 26
cipherText = String.Concat(cipherText, huruf(c))
Next i
TextBoxCipher.Text = cipherText
End If
End Sub
Private Sub ButtonDecrypt_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ButtonDecrypt.Click
plainText = ""
If (TextBoxCipher.TextLength <> 0 And
TextBoxKunci.TextLength <> 0) Then
aturKunci()
For i = 0 To TextBoxCipher.TextLength - 1
c = Asc(TextBoxCipher.Text.Substring(i, 1)) - 65
k = Asc(kunci.Substring(i, 1)) - 65
p = (c + 26 - k) Mod 26
plainText = String.Concat(plainText, huruf(p))
Next i
TextBoxPlain2.Text = plainText
End If
End Sub
Private Sub TextBoxPlain_GotFocus(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
TextBoxPlain.GotFocus
TextBoxPlain.SelectAll()
End Sub
Private Sub TextBoxPlain_KeyPress(ByVal sender As
System.Object, ByVal e As

System.Windows.Forms.KeyPressEventArgs) Handles
TextBoxPlain.KeyPress
e.Handled = Not (Char.IsLetter(e.KeyChar) Or
Asc(e.KeyChar) < 32)
e.KeyChar = UCase(e.KeyChar)
End Sub
Private Sub TextBoxPlain_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
TextBoxPlain.TextChanged
TextBoxCipher.Text = ""
End Sub
Private Sub TextBoxCipher_GotFocus(ByVal sender As Object,
ByVal e As System.EventArgs) Handles TextBoxCipher.GotFocus
TextBoxCipher.SelectAll()
End Sub
Private Sub TextBoxCipher_KeyPress(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
TextBoxCipher.KeyPress
e.Handled = Not (Char.IsLetter(e.KeyChar) Or
Asc(e.KeyChar) < 32)
e.KeyChar = UCase(e.KeyChar)
End Sub
Private Sub TextBoxCipher_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
TextBoxCipher.TextChanged
TextBoxPlain2.Text = ""
End Sub
Private Sub TextBoxKunci_KeyPress(ByVal sender As

System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
TextBoxKunci.KeyPress
e.Handled = Not (Char.IsLetter(e.KeyChar) Or
Asc(e.KeyChar) < 32)
e.KeyChar = UCase(e.KeyChar)
End Sub
Private Sub aturKunci()
If TextBoxPlain.Text.Length <= TextBoxKunci.Text.Length
Then
kunci = TextBoxKunci.Text.Substring(0,
TextBoxPlain.TextLength)
Else
kunci = ""
For i = 1 To (TextBoxPlain.TextLength /
TextBoxKunci.TextLength)
kunci = String.Concat(kunci, TextBoxKunci.Text)
Next i
i = TextBoxPlain.TextLength Mod
TextBoxKunci.TextLength
If i > 0 Then
kunci = String.Concat(kunci,
TextBoxKunci.Text.Substring(0, i))
End If
End If
End Sub
End Class

Public Function encrypt(ByVal input As String) 'input is a variable within the funcion
Dim n as Integer
Dim i As Integer
n = key.Text Mod 26 'gets what is in the text box of 'key' and sets it as n
' the key is a multiple of 26 so 26 will = 0
'need to remove white spaces
While input.Contains(" ")
'when the input text contains a space
input = input.Replace(" ", "") 'replaces it with no space.
End While
For i = 1 To Len(input)
'find the length of the input
Mid(input, i, 1) = Chr(Asc(Mid(input, i, 1)) + n)
'chr returns the character associated
with the specified character code
'
Next
encrypt = input
End Function

Vous aimerez peut-être aussi