LAMPIRAN 2 :
LISTING PROGRAM
Public Class Form1
'Triple DES ---
Dim PC1() As Integer = {57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4}
Dim PC2() As Integer = {14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32}
Dim LS() As Integer = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1}
Dim RS() As Integer = {0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1}
Dim IP() As Integer = {58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7}
Dim E() As Integer = {32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1}
Dim S(,,) As Integer = {{{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7}, _ {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8}, _
{{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10}, _ {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5}, _ {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15}, _ {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}}, _ _
{{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8}, _ {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1}, _ {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7}, _ {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}}, _ _
{{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15}, _ {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9}, _ {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4}, _ {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}}, _ _
{{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9}, _ {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6}, _ {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14}, _ {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}}, _ _
{{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11}, _ {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8}, _ {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6}, _ {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}}, _ _
{{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7}, _ {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2}, _ {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8}, _ {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}}}
Dim P() As Integer = {16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25}
Dim IPF() As Integer = {40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25}
Dim IP1 As String Dim K1(17) As String Dim C(17) As String Dim D(17) As String Dim L(17) As String Dim R(17) As String
'End Triple DES ---
Function E_ElGamal(ByVal Plain As String, ByVal p As Integer, ByVal g As Integer, ByVal x As Integer)
Next
Dim Kunci(100) As Integer
For i = 0 To Plain.Length - 1
Kunci(i) = CInt(Math.Ceiling(Rnd() * (p - 2))) + 1 Next
For i = 0 To Plain.Length - 1 a(i) = SnM(g, Kunci(i), p) Next
For i = 0 To Plain.Length - 1
b(i) = (SnM(y, Kunci(i), p) * X2(i) Mod p) Mod p Temp = Temp & Chr(a(i)) & Chr(b(i))
Next
Return Temp End Function
Function D_ElGamal(ByVal Cipher As String, ByVal p As Integer, ByVal g As Integer, ByVal x As Integer)
Dim Temp As String = "" Dim Temp2(100) As Integer For i = 0 To Cipher.Length - 1 Temp2(i) = Asc(Cipher(i)) Next
For i = 0 To Cipher.Length - 1
Temp = Temp & Chr((SnM(Temp2(i), p - 1 - x, p) * Temp2(i + 1) Mod p) Mod p)
Next
Return Temp End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtPlain.Text = "APLIKASI" txtKey1.Text = "257,17,11"
txtKey_3DES.Text = "AVDFEAD234defaWASSdsaw14" End Sub
Function SnM(ByVal x As Integer, ByVal y As Integer, ByVal n As Integer) Dim b As String
b = "" While y > 0
b = b & y Mod 2 y = y \ 2
End While
b = StrReverse(b) Dim z As Integer z = 1
For i = 0 To b.Length - 1 If b.Chars(i) = "1" Then z = (x * (z ^ 2)) Mod n Else
z = (z ^ 2) Mod n End If
Function fPC1(ByVal K As String) Dim Temp As String = ""
For i = 0 To PC1.Length - 1
Temp = Temp & K.Chars(PC1(i) - 1) Next
Return Temp End Function
Function fPC2(ByVal K As String) Dim Temp As String = ""
For i = 0 To PC2.Length - 1
Temp = Temp & K.Chars(PC2(i) - 1) Next
Return Temp End Function
Function fIPF(ByVal RL16 As String) Dim Temp As String = ""
For i = 0 To IPF.Length - 1
Temp = Temp & RL16.Chars(IPF(i) - 1) Next
Return Temp End Function
Function fP(ByVal SB As String) Dim Temp As String = "" For i = 0 To P.Length - 1
Return Temp End Function
Function fF(ByVal i As Integer)
Dim X As String = fRK(R(i - 1), K1(i)) Dim X1 As String = ""
For i = 1 To 8
X1 = X1 & fSB(i, X.Substring((i - 1) * 6, 6)) Next
Return fP(X1) End Function
Function fFI(ByVal i As Integer)
Dim X As String = fRK(R(i - 1), K1(16 - i)) Dim X1 As String = ""
For i = 1 To 8
X1 = X1 & fSB(i, X.Substring((i - 1) * 6, 6)) Next
Return fP(X1) End Function
Function fSB(ByVal i As String, ByVal B As String)
Return fDtB(S(i - 1, fBtD(B.Chars(0) & B.Chars(5)), fBtD(B.Substring(1, 4)))) End Function
Function fBtD(ByVal B As String) Dim Temp As Integer = 0 For i = 0 To B.Length - 1
Return Temp End Function
Function fHtD(ByVal B As String) Dim Temp As Integer = 0 Dim X As Integer
For i = 0 To B.Length - 1 Select Case B.Substring(i, 1) Case "A" : X = 10
Case "B" : X = 11 Case "C" : X = 12 Case "D" : X = 13 Case "E" : X = 14 Case "F" : X = 15
Case Else : X = CInt(B.Substring(i, 1)) End Select
Temp = Temp + 16 ^ (B.Length - i - 1) * X Next
Return Temp End Function
Function fDtH(ByVal D As Integer) Dim Temp As String = ""
While D > 0
Select Case D Mod 16 Case 10
Temp = Temp & "A" Case 11
Temp = Temp & "C" Case 13
Temp = Temp & "D" Case 14
Temp = Temp & "E" Case 15
Temp = Temp & "F" Case Else
Temp = Temp & D Mod 16 End Select
D = D \ 16 End While
Temp = StrReverse(Temp)
'Khusus untuk penmbahan 0 kalau kurang dari 2 digit For i = 1 To 2 - Temp.Length()
Temp = "0" & Temp Next
Return Temp End Function
Function fDtB(ByVal D As Integer) Dim Temp As String = ""
While D > 0
Temp = Temp & D Mod 2 D = D \ 2
End While
Temp = StrReverse(Temp)
Next
Return Temp End Function
Function fRK(ByVal R As String, ByVal K As String) Dim R1 As String = fE(R)
Return fXOR(R1, K) End Function
Function fXOR(ByVal X As String, ByVal Y As String) Dim Temp As String = ""
For i = 0 To X.Length - 1
Temp = Temp & (X.Substring(i, 1) Xor Y.Substring(i, 1)) Next
Return Temp End Function
Function fE(ByVal R As String) Dim Temp As String = "" For i = 0 To E.Length - 1
Temp = Temp & R.Chars(E(i) - 1) Next
Return Temp End Function
Function fHtB(ByVal X As String) Dim B As String = ""
If IsNumeric(X(i)) Then
Temp = fDtB(CInt(X.Substring(i, 1))) Else
Temp = fDtB(Asc(X(i)) - 55) End If
B = B & Temp Next
Return B End Function
Function E_DES(ByVal M As String, ByVal Key As String) Dim Cipher As String = ""
Dim CipherTemp As String = "" Dim K As String = fHtB(Key)
ts.Text = ts.Text & "K = " & K & vbCrLf
Dim Plain As String = fHtB(M)
'Pembentukan Key ---
K1(0) = fPC1(K)
ts.Text = ts.Text & "PC1(K) = " & K1(0) & vbCrLf 'C0 dan D0
C(0) = K1(0).Substring(0, 28) D(0) = K1(0).Substring(28, 28)
ts.Text = ts.Text & "C(0) = " & C(0) & vbCrLf ts.Text = ts.Text & "D(0) = " & D(0) & vbCrLf
C(i) = C(i - 1).Substring(LS(i - 1), C(i - 1).Length - LS(i - 1)) & C(i - 1).Substring(0, LS(i - 1))
D(i) = D(i - 1).Substring(LS(i - 1), D(i - 1).Length - LS(i - 1)) & D(i - 1).Substring(0, LS(i - 1))
ts.Text = ts.Text & "C(" & i & ") = " & C(i) & vbCrLf ts.Text = ts.Text & "D(" & i & ") = " & D(i) & vbCrLf Next
For i = 1 To 16
K1(i) = fPC2(C(i) & D(i))
ts.Text = ts.Text & "K(" & i & ") = " & K1(i) & vbCrLf Next
'End Pembentukan Key ---
'Langkah DES ---
IP1 = ""
For i = 0 To IP.Length - 1
IP1 = IP1 & Plain.Chars(IP(i) - 1) Next
ts.Text = ts.Text & "IP-1 = " & IP1 & vbCrLf 'MessageBox.Show("IP : " & IP1)
L(0) = IP1.Substring(0, 32) R(0) = IP1.Substring(32, 32)
ts.Text = ts.Text & "L(0) = " & L(0) & vbCrLf ts.Text = ts.Text & "R(0) = " & R(0) & vbCrLf For i = 1 To 16
R(i) = fXOR(L(i - 1), fF(i))
ts.Text = ts.Text & "L(" & i & ") = " & L(i) & vbCrLf ts.Text = ts.Text & "R(" & i & ") = " & R(i) & vbCrLf Next
'MessageBox.Show(L(16))
CipherTemp = fIPF(R(16) & "" & L(16))
ts.Text = ts.Text & "IPF = " & CipherTemp & vbCrLf
Cipher = ""
For i = 1 To CipherTemp.Length / 8
Cipher = Cipher & fDtH(fBtD(CipherTemp.Substring((i - 1) * 8, 8))) Next
ts.Text = ts.Text & "Cipher = " & Cipher & vbCrLf
'End Langkah DES ---
Return Cipher End Function
Function D_DES(ByVal M As String, ByVal Key As String) Dim Plain As String = ""
Dim PlainTemp As String = "" Dim K As String = fHtB(Key) Dim Cipher As String = fHtB(M)
'Pembentukan Key ---
'C16 dan D16
C(16) = fPC1(K).Substring(0, 28) D(16) = fPC1(K).Substring(28, 28)
For i = 15 To 0 Step -1
C(i) = C(i + 1).Substring(C(i + 1).Length - RS(15 - i), RS(15 - i)) & C(i + 1).Substring(0, C(i + 1).Length - RS(15 - i))
D(i) = D(i + 1).Substring(D(i + 1).Length - RS(15 - i), RS(15 - i)) & D(i + 1).Substring(0, D(i + 1).Length - RS(15 - i))
Next
For i = 15 To 0 Step -1 K1(i) = fPC2(C(i) & D(i)) Next
'End Pembentukan Key ---
'Langkah DES ---
IP1 = ""
For i = 0 To IP.Length - 1
IP1 = IP1 & Cipher.Chars(IP(i) - 1) Next
'MessageBox.Show("IP : " & IP1)
L(0) = IP1.Substring(0, 32) R(0) = IP1.Substring(32, 32)
L(i) = R(i - 1)
R(i) = fXOR(L(i - 1), fFI(i)) Next
'MessageBox.Show(L(16))
PlainTemp = fIPF(R(16) & "" & L(16))
Plain = ""
For i = 1 To PlainTemp.Length / 8
Plain = Plain & fDtH(fBtD(PlainTemp.Substring((i - 1) * 8, 8))) Next
'End Langkah DES ---
Return Plain End Function
Function E_3DES(ByVal P As String, ByVal K As String) Dim Temp As String = P
For i = 1 To 3 If i = 2 Then
Temp = D_DES(Temp, K.Substring((i - 1) * 16, 16)) Else
Temp = E_DES(Temp, K.Substring((i - 1) * 16, 16)) End If
Next
Return Temp End Function
For i = 3 To 1 Step -1 If i = 2 Then
Temp = E_DES(Temp, K.Substring((i - 1) * 16, 16)) Else
Temp = D_DES(Temp, K.Substring((i - 1) * 16, 16)) End If
Next
Return Temp End Function
Function fTtH(ByVal T As String) Dim Temp As String = "" For i = 0 To T.Length - 1
Temp = Temp & fDtH(Asc(T(i))) Next
If Temp.Length Mod 16 <> 0 Then
For i = 1 To (16 - Temp.Length Mod 16) / 2 Temp = Temp & fDtH(Asc(" "))
Next End If Return Temp End Function
Function fTtHKey(ByVal T As String) Dim Temp As String = ""
For i = 0 To T.Length - 1
Temp = Temp & fDtH(Asc(T(i))) Next
End Function
Function fHtT(ByVal T As String) Dim Temp As String = "" For i = 0 To T.Length / 2 - 1
Temp = Temp & Chr(fHtD(T.Substring(i * 2, 2))) Next
Return Temp End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If txtKey_3DES.Text.Length <> 24 Then
MessageBox.Show("Panjang Kunci untuk 3DES harus sama 24") Else
Dim Plain As String = txtPlain.Text Dim Cipher As String
Dim C As String = ""
Dim KEG() As String = txtKey1.Text.Split(",") Dim p As Integer = CInt(KEG(0))
Dim g As Integer = CInt(KEG(1)) Dim x As Integer = CInt(KEG(2)) If Lehmann(p) = True Then
Dim K As String = fTtHKey(txtKey_3DES.Text)
Plain = fTtH(Plain)
For i = 0 To Plain.Length \ 16 - 1
Next
Cipher = E_ElGamal(fHtT(C), p, g, x)
txtCipher.Text = Cipher Else
MessageBox.Show("Not Prime") End If
'txtCipher.Text = Cipher 'MessageBox.Show(Cipher)
'MessageBox.Show(D_ElGamal(Cipher, Kunci, p, g, x))
End If
End Sub
Function Lehmann(ByVal P As Integer) As Boolean Dim prime = False
Dim a As Integer For i = 1 To 3 Randomize()
a = CInt(Rnd() * (P - 2) + 1) If SnM(a, (P - 1) \ 2, P) = 1 Then prime = True
End If Next
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If txtKey_3DES.Text.Length <> 24 Then
MessageBox.Show("Panjang Kunci untuk 3DES harus sama 24") Else
Dim Plain As String
Dim Cipher As String = txtCipher.Text Dim C As String = ""
Dim KEG() As String = txtKey1.Text.Split(",") Dim p As Integer = CInt(KEG(0))
Dim g As Integer = CInt(KEG(1)) Dim x As Integer = CInt(KEG(2))
Dim K As String = fTtHKey(txtKey_3DES.Text)
Plain = D_ElGamal(Cipher, p, g, x)
Plain = fTtH(Plain)
For i = 0 To Plain.Length \ 16 - 1
C = C & D_3DES(Plain.Substring(i * 16, 16), K) Next
txtPlain.Text = fHtT(C)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim p As Integer
Dim prime As Boolean = False While Not prime
Randomize()
p = CInt(Rnd() * 300 + 6) If Lehmann(p) = True Then
If p Mod 2 <> 0 And p Mod 3 <> 0 And p Mod 5 <> 0 Then prime = True
End If End If
End While
txtKey1.Text = p & "," & CInt(Rnd() * (p - 2) + 1) & "," & CInt(Rnd() * (p - 2) + 1)
End Sub
Private Sub txtKey_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtKey_3DES.KeyDown
Dim X As Char Dim Pil As Integer txtKey_3DES.Text = "" Randomize()
If e.KeyCode = Keys.Enter Then For i = 1 To 24
Pil = CInt(Int((3 * Rnd()) + 1)) Select Case Pil
Case 1
Case 2
X = Chr(CInt(Int((26 * Rnd()) + 1)) + 96) Case Else
X = Chr(CInt(Int((9 * Rnd()) + 1)) + 47) End Select
txtKey_3DES.Text = txtKey_3DES.Text & X Next
End If End Sub