Senin, 24 Juni 2013

Visual Basic





Public Class Form1

    Private Sub VigenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VigenToolStripMenuItem.Click
        Form2.MdiParent = Me
        Form2.Show()
    End Sub

    Private Sub CaesarChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CaesarChiperToolStripMenuItem.Click
        Form3.MdiParent = Me
        Form3.Show()
    End Sub

    Private Sub GransfeldChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GransfeldChiperToolStripMenuItem.Click
        Form4.MdiParent = Me
        Form4.Show()
    End Sub

    Private Sub VernamChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VernamChiperToolStripMenuItem.Click
        Form5.MdiParent = Me
        Form5.Show()
    End Sub

    Private Sub DesChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DesChiperToolStripMenuItem.Click
        Form6.MdiParent = Me
        Form6.Show()
    End Sub

    Private Sub RC4ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RC4ToolStripMenuItem.Click
        Form7.MdiParent = Me
        Form7.Show()
    End Sub

    Private Sub KeluarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        End
    End Sub
End Class


Caesar Chiper



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 bil As Integer
        Cipher.Text = ""
        For i = 1 To Len(Plain.Text)
            x = Microsoft.VisualBasic.Mid(Plain.Text, i, 1)
            bil = Asc(x)
            bil = bil + 3
            x = Chr(bil)
            Cipher.Text = Cipher.Text & x
        Next
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Plain.Text = ""
        Cipher.Text = ""

    End Sub
End Class



Gransfeld Chiper



Vernam Chiper

contoh:
Saya memiliki sebuah plaintext yaitu RUSDI dan memiliki sebuah kunci yaitu
CRASH (ingat panjang kunci harus sama dengan plaintext dan sebaiknya tidak
ada karakter yang diulang).

Pertama kita harus mendapatkan kode ASCII dari plaintext kemudian diubah
ke bentuk biner


kode 
kode

 Setelah itu masing-masing karakter di XOR-kan dengan Key

R = 0101 0010 U = 0101 0101 S = 0101 0011 D = 0100 0100 I = 0100 1001
C = 0100 0011 R = 0101 0010 A = 0100 0001 S = 0101 0011 H = 0100 1000
XOR -------------------------------------------------------------------------
Cipher: 0001 0001 0000 0111 0001 0010 0001 0111 0000 0001
-----------------------------------------------------------------------------
ASCII : Ctrl-Q Ctrl-G Ctrl-R Ctrl-W Ctrl-A

Proses dekripsi pesan juga melakukan operasi yang sama yaitu XOR antara Cipher
dengan key.


Cipher: 0001 0001 0000 0111 0001 0010 0001 0111 0000 0001
Key: 0100 0011 0101 0010 0100 0001 0101 0011 0100 1000
XOR -------------------------------------------------------------------------
Plain : 0101 0010 0101 0101 0101 0011 0100 0100 0100 1001
-----------------------------------------------------------------------------
ASCII : R U S D I











code
Option Explicit
Private RByte() As Variant

Private Sub cmdDecrypt_Click()
TxtPlain.Text = crypt(TxtCipher.Text, False)
End Sub

Private Sub cmdEncrypt_Click()
TxtCipher.Text = crypt(TxtPlain.Text, True)
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
FrmAbout.Show
End Sub

Private Function crypt(strInput As String, _
ByVal bEncrypt As Boolean) As String

Dim I As Long
Dim NewAsc As Long
Dim keypos As Long

GetKey TxtKey
For I = 1 To Len(strInput)
If bEncrypt Then
NewAsc = Asc(Mid$(strInput, I, 1)) + RByte(keypos)
Else
NewAsc = Asc(Mid$(strInput, I, 1)) - RByte(keypos)
End If
Do While NewAsc < newasc =" NewAsc"> 255
NewAsc = NewAsc - 255
Loop
Mid$(strInput, I, 1) = Chr$(NewAsc)
keypos = keypos + 1
If keypos > UBound(RByte) Then
keypos = 0
End If
Next I
crypt = strInput

End Function

Private Sub GetKey(StrA As String)

Dim I As Long
If Len(StrA) Then
ReDim RByte(Len(StrA) - 1) As Variant
For I = 1 To Len(StrA)
RByte(I - 1) = Asc(Mid$(StrA, I, 1))
Next I
End If
End Sub


Vigenere Chiper







Public Class Form2

    Private Sub btnProses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProses.Click
        Chipertext.Text = Enkripsi(Plaintext.Text, Kunci.Text)
    End Sub

    Function Enkripsi(ByVal Teks As String, ByVal Kunci As String) As String
        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
        jum = Len(Teks)
        sPlain = ""
        sKey = Kunci
        sKata = Teks
        For i = 1 To jum
            If j = Len(sKey) Then
                j = 1
            Else
                j = j + 1
            End If
            nKata = Asc(Mid(sKata, i, 1))

            nKunci = Asc(Mid(sKey, j, 1))

            nEnc = ((nKata + nKunci) Mod 256)

            sPlain = sPlain & Chr((nEnc))
        Next i
        Enkripsi = sPlain
    End Function
End Class

Des Chiper

RC4
Public Function RC4(ByVal Expression As String, ByVal Password As String) As String
On Error Resume Next
Dim RB(0 To 255) As Integer, X As Long, Y As Long, Z As Long, Key() As Byte, ByteArray() As Byte, Temp As Byte
If Len(Password) = 0 Then
    Exit Function
End If
If Len(Expression) = 0 Then
    Exit Function
End If
If Len(Password) > 256 Then
    Key() = StrConv(Left$(Password, 256), vbFromUnicode)
Else
    Key() = StrConv(Password, vbFromUnicode)
End If
For X = 0 To 255
    RB(X) = X
Next X
X = 0
Y = 0
Z = 0
For X = 0 To 255
    Y = (Y + RB(X) + Key(X Mod Len(Password))) Mod 256
    Temp = RB(X)
    RB(X) = RB(Y)
    RB(Y) = Temp
Next X
X = 0
Y = 0
Z = 0
ByteArray() = StrConv(Expression, vbFromUnicode)
For X = 0 To Len(Expression)
    Y = (Y + 1) Mod 256
    Z = (Z + RB(Y)) Mod 256
    Temp = RB(Y)
    RB(Y) = RB(Z)
    RB(Z) = Temp
    ByteArray(X) = ByteArray(X) Xor (RB((RB(Y) + RB(Z)) Mod 256))
Next X
RC4 = StrConv(ByteArray, vbUnicode)
End Function