Log in

View Full Version : [SHARE] Enkripsi Teks: Caesar,Mono-Polyalphabetic, Vigenere, Permutasi/Trasposisi


PHPmaster
20th November 2011, 11:44 PM
Dear All,

Member baru, mau share dasar-dasar enkripsi berbasis teks, sebelum menguasai enkripsi berbasis byte (Stream cipher atau block cipher).



http://u.kaskus.us/11/zs9xaak6.jpg

http://dtugasalgoritma.blogspot.com/...r-chipher.html (http://dtugasalgoritma.blogspot.com/2010/08/caesar-chipher.html)

http://dtugasalgoritma.blogspot.com/...ic-cipher.html (http://dtugasalgoritma.blogspot.com/2010/09/monoalphabetic-cipher.html)



http://u.kaskus.us/11/zcdcgnop.jpg

http://dtugasalgoritma.blogspot.com/...-pemutasi.html (http://dtugasalgoritma.blogspot.com/2010/09/enkripsi-tranposisi-pemutasi.html)

http://dtugasalgoritma.blogspot.com/...ic-cipher.html (http://dtugasalgoritma.blogspot.com/2010/10/polyalphabetic-cipher.html)



http://u.kaskus.us/11/wsgi3v9y.jpg

http://dtugasalgoritma.blogspot.com/...-vigenere.html (http://dtugasalgoritma.blogspot.com/2010/11/enkripsi-vigenere.html)



Berikut ini kode program:

Caesar Cipher


Code:

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


Code:

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 0 Then
kunci = String.Concat(kunci, TextBoxKunci.Text.Substring(0, i))
End If
End If
End Sub

End Class

Enkripsi Transposisi/Permutasi

<div style="margin:20px; margin-top:5px">
Code:

import java.util.Scanner;
public class Transposisi {
public static void main(String[] args) {
// Baca data dari keyboard
Scanner baca = new Scanner(System.in);
System.out.print("Masukkan kalimat (min 3 karakter)? ");
String kal = baca.nextLine().trim().toUpperCase();
if (isValid(kal)) {
// Baca lebar matrix transposisi
System.out.print("Masukkan lebar kolom transposisi (min 2 digit) ? ");
int kolom = baca.nextInt();
if (kolom < 2) {
System.out.print("Lebar kolom transposisi (min 2 digit) !");
System.exit(-1);
}
int baris = kal.length() / kolom;
if (kal.length() % kolom > 0)
baris++;

// Ciptakan matrix dan isi dengan data
char[][] ch = new char[baris][kolom];
int pos = 0;
for (int i=0; i