Minggu, 30 Juni 2013

Aplikasi Kriptografi


Form 1 :





Public Class Form1

    Private Sub ChaesarChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChaesarChiperToolStripMenuItem.Click
        Caesar_Chiper.Show()
    End Sub

    Private Sub GronsfieldChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GronsfieldChiperToolStripMenuItem.Click
        Gronsfeld_Chiper.Show()
    End Sub

    Private Sub VernamChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VernamChiperToolStripMenuItem.Click
        Vernam_Chiper.Show()
    End Sub

    Private Sub VigenereChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VigenereChiperToolStripMenuItem.Click
        Vigenere_Chiper.Show()
    End Sub

    Private Sub DesChiperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DesChiperToolStripMenuItem.Click
        Des_Chiper.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


Form Caesar Chiper :








Public Class Caesar_Chiper

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

    Private Sub enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enkripsi.Click
        Dim jumlah As Double = Len(plaintext.Text)
        Dim x As String
        Dim x_kalimat As String = ""
        Dim i As Double
        Dim bil As Integer
        For i = 1 To jumlah
            x = Mid(plaintext.Text, i, 1)
            bil = Asc(x) + 3
            x = Chr(bil)
            x_kalimat = x_kalimat + x
        Next i
        chipertext.Text = x_kalimat
    End Sub
End Class


Form Gronsfeld Chiper :








Public Class Gronsfeld_Chiper

    Private Sub enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enkripsi.Click
        chipertext.Text = enkrips(plaintext.Text, key.Text)
    End Sub
    Function enkrips(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
        enkrips = sPlain
    End Function


Form Vernam Chiper :








Public Class Vernam_Chiper

    Private Sub btn_enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_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
        chipertext.Text = sPlain
    End Sub

    Private Sub Vernam_Chiper_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        plaintext.Text = ""
        chipertext.Text = ""
        kunci.Text = ""
    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


Form Vigenere Chiper :








Public Class Vigenere_Chiper
    Private Function VigenereEncipher(ByVal strPlaintext As String, ByRef strKey As String) As String
        Dim strPlaintext2 As String
        Dim strKey2 As String
        Dim strCiphertext As String
        Dim strCiphertext2 As String
        Dim i As Integer
        Dim j As Integer
        Dim c1 As Integer
        Dim nShift As Integer
        Dim pAlphabet As Integer
        Dim cAlphabet As Integer
        '1. Hilangkan semua karakter yang bukan alfabet dari strPlaintext
        ' dan simpan sebagai strPlaintext2
        strPlaintext2 = ""
        For i = 1 To strPlaintext.Length
            c1 = Asc(Mid(strPlaintext, i, 1))
            If (c1 >= 65 And c1 <= 90) Then
                strPlaintext2 = strPlaintext2 & Chr(c1)
            End If
        Next i
        '2. Hilangkan semua karakter yang bukan alfabet dari strKey
        ' dan simpan sebagai strKey2
        strKey2 = ""
        For i = 1 To strKey.Length
            c1 = Asc(Mid(strKey, i, 1))
            If (c1 >= 65 And c1 <= 90) Then
                strKey2 = strKey2 & Chr(c1)
            End If
        Next i
        '3. Susun key sepanjang plaintext
        If (strKey2.Length < strPlaintext2.Length) Then
            j = 0
            For i = 1 To strPlaintext2.Length

                c1 = Asc(Mid(strPlaintext, i, 1))
                strKey2 = strKey2 & Mid(strKey2, j + 1, 1)
                j = (j + 1) Mod strKey2.Length
            Next i
        End If
        '4. Geser masing-masing huruf pada plaintext
        ' dengan huruf yang terkait pada key
        strCiphertext = ""
        For i = 1 To strPlaintext2.Length
            c1 = Asc(Mid$(strPlaintext2, i, 1))
            nShift = Asc(Mid$(strKey2, i, 1)) - 65
            If ((c1 >= 65) And (c1 <= 90)) Then
                pAlphabet = c1 - 65 ' get the alphabet sequence
                cAlphabet = (pAlphabet + nShift) Mod 26 ' shifted alphabet
                c1 = cAlphabet + 65 ' get character in 65 ... 90
            End If
            strCiphertext = strCiphertext & Chr(c1)
        Next i
        '5. Susun strCiphertext sesuai dengan urutan strPlaintext
        strCiphertext2 = ""
        strKey = ""
        j = 1
        For i = 1 To strPlaintext.Length
            c1 = Asc(Mid$(strPlaintext, i, 1))
            If ((c1 >= 65) And (c1 <= 90)) Then
                strCiphertext2 = strCiphertext2 & Mid(strCiphertext, j, 1)
                strKey = strKey & Mid(strKey2, j, 1)
                j = j + 1
            Else
                strCiphertext2 = strCiphertext2 & Chr(c1)
                strKey = strKey & " "
            End If
        Next i
        Return strCiphertext2
    End Function
  
    Private Sub btn_enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_enkripsi.Click
        chipertext.Text = VigenereEncipher(plaintext.Text, kunci.Text)
    End Sub
End Class


Form Des Chiper :








Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class Des_Chiper
 
    Private Sub btn_enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_enkripsi.Click
        plaintext.Text = "abcdef"

        Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()

        Dim unicodeText As Byte()

        unicodeText = DirectCast(Array.CreateInstance(GetType(Byte), plaintext.Text.Length * 2), Byte())

        enc.GetBytes(plaintext.Text.ToCharArray(), 0, plaintext.Text.Length, unicodeText, 0, True)

        Dim objmd5 As MD5 = New MD5CryptoServiceProvider ' Untuk enkripsi MD5


        'Dim objmd5 As MD5 = New SHA1CryptoServiceProvider() ' Untuk enkripsi SHA1



        'Dim objmd5 As MD5 = New DESCryptoServiceProvider() ' Untuk enkripsi DES


        'Dim objmd5 As MD5 = New RSACryptoServiceProvider ' Untuk enkripsi RSA


        Dim result As Byte() = objmd5.ComputeHash(unicodeText)

        Dim sb As StringBuilder = New StringBuilder()

        Dim i As Integer

        For i = 0 To result.Length - 1
            sb.Append(result(i).ToString("X2"))
        Next

        chipertext.Text = sb.ToString()

        'Anda dapat mengganti "abcdef" dengan kata yang anda butuhkan, dan menggunakan tipe enkripsi yang lain yang anda inginkan dengan mengaktifkan baris berikut :

        'Dim objmd5 As MD5 = New MD5CryptoServiceProvider ' Untuk enkripsi MD5

        'Dim objmd5 As MD5 = New SHA1CryptoServiceProvider() ' Untuk enkripsi SHA1

        'Dim objmd5 As MD5 = New DESCryptoServiceProvider() ' Untuk enkripsi DES

        'Dim objmd5 As MD5 = New RSACryptoServiceProvider ' Untuk enkripsi RSA
    End Sub
End Class


Form RC4 :








Public Class RC4_Chiper
    Private Function Rc4(ByVal message As String, ByVal password As String) As String
        Dim s = Enumerable.Range(0, 256).ToArray
        Dim i, j As Integer
        For i = 0 To s.Length - 1
            j = (j + Asc(password(i Mod password.Length)) + s(i)) And 255
            Dim temp = s(i)
            s(i) = s(j)
            s(j) = temp
        Next
        i = 0
        j = 0
        Dim sb As New System.Text.StringBuilder(message.Length)
        For Each c As Char In message
            i = (i + 1) And 255
            j = (j + s(i)) And 255
            Dim temp = s(i)
            s(i) = s(j)
            s(j) = temp
            sb.Append(Chr(s((s(i) + s(j)) And 255) Xor Asc(c)))
        Next
        Return sb.ToString
    End Function

    Private Sub btn_enkripsi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_enkripsi.Click
        chipertext.Text = Rc4(plaintext.Text, kunci.Text)
    End Sub

Mesran.blogspot.com

Tidak ada komentar:

Posting Komentar