0% found this document useful (0 votes)
34 views9 pages

Visual Basic 6 Kakom

This document defines variables and tables used in a Visual Basic program that implements the Data Encryption Standard (DES) algorithm. It includes: - Variables to store binary data, keys, intermediate values during encryption/decryption - Tables containing the defined DES permutations and substitutions - Subroutines for key generation, encryption, decryption, and the core DES logic The program allows encrypting/decrypting strings by applying the DES algorithm using generated keys. It demonstrates the basic logic and data structures needed for DES encryption and decryption in Visual Basic.

Uploaded by

izzahp
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views9 pages

Visual Basic 6 Kakom

This document defines variables and tables used in a Visual Basic program that implements the Data Encryption Standard (DES) algorithm. It includes: - Variables to store binary data, keys, intermediate values during encryption/decryption - Tables containing the defined DES permutations and substitutions - Subroutines for key generation, encryption, decryption, and the core DES logic The program allows encrypting/decrypting strings by applying the DES algorithm using generated keys. It demonstrates the basic logic and data structures needed for DES encryption and decryption in Visual Basic.

Uploaded by

izzahp
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Visual Basic 6.

0 DES

Option Explicit

Public tamp As String


Public j, k, l, m, n, looping As Integer
Public i As Integer
Public index1, index2, counter, geser_kiri As Integer
Public jumlah As Integer
Public Y As Integer
Public bila_file As Boolean

Public arrbiner(1 To 64) As Integer


Public arrtampung1(1 To 64) As Integer
Public arrpc1(1 To 56) As Integer
Public arrpc2(1 To 48) As Integer
Public arrC(1 To 28) As Integer
Public arrD(1 To 28) As Integer
Public arrtampC(1 To 28) As Integer
Public arrtampD(1 To 28) As Integer
Public arrCD(1 To 56) As Integer

'Tabel-tabel
Public tabel_pc_1 As Variant
Public tabel_ls As Variant
Public tabel_pc_2 As Variant
Public message As String
Public tabel_ip As Variant
Public tabel_ip_1 As Variant
Public tabel_p As Variant
Public tabel_e As Variant
Public tabel_S(1 To 8) As Variant
Public tabel_s1(1 To 64) As Integer
Public tabel_s2(1 To 64) As Integer
Public tabel_s3(1 To 64) As Integer
Public tabel_s4(1 To 64) As Integer
Public tabel_s5(1 To 64) As Integer
Public tabel_s6(1 To 64) As Integer
Public tabel_s7(1 To 64) As Integer
Public tabel_s8(1 To 64) As Integer
Public arr_s1 As Variant
Public arr_s2 As Variant
Public arr_s3 As Variant
Public arr_s4 As Variant
Public arr_s5 As Variant
Public arr_s6 As Variant
Public arr_s7 As Variant
Public arr_s8 As Variant

'Key
Public arrKEY(1 To 16) As Variant
Public arrKey1(1 To 48) As Integer
Public arrKey2(1 To 48) As Integer
Public arrKey3(1 To 48) As Integer
Public arrKey4(1 To 48) As Integer
Public arrKey5(1 To 48) As Integer
Public arrKey6(1 To 48) As Integer
Public arrKey7(1 To 48) As Integer
Public arrKey8(1 To 48) As Integer
Public arrKey9(1 To 48) As Integer
Public arrKey10(1 To 48) As Integer
Public arrKey11(1 To 48) As Integer
Public arrKey12(1 To 48) As Integer
Public arrKey13(1 To 48) As Integer
Public arrKey14(1 To 48) As Integer
Public arrKey15(1 To 48) As Integer
Public arrKey16(1 To 48) As Integer

'Encryption
Public arrIp(1 To 64) As Integer
Public arrL(1 To 32) As Integer
Public arrR(1 To 32) As Integer
Public arrtempL(1 To 32) As Integer
Public arrER(1 To 48) As Integer
Public arrHasil_XOR(1 To 48) As Integer
Public arrhasil_S(1 To 32) As Integer
Public arrhasil_P(1 To 32) As Integer
Public arrR1(1 To 32) As Integer
Public brs As Integer
Public klm As Integer
Public tamp_S As Integer
Public arrIp_1(1 To 64) As Integer
Public arrHasil_Ip_1(1 To 64) As Integer

Public CipherText As String

'Decryption
Public arrkembaliIp(1 To 64) As Integer
Public arrkembaliL(1 To 32) As Integer
Public arrkembaliR(1 To 32) As Integer
Public arrkembalitempL(1 To 32) As Integer
Public arrkembalitempR(1 To 32) As Integer
Public arrkembaliER(1 To 48) As Integer
Public arrkembaliHasil_XOR(1 To 48) As Integer
Public arrkembalihasil_S(1 To 32) As Integer
Public arrkembalihasil_P(1 To 32) As Integer
Public arrkembaliR1(1 To 32) As Integer
Public kembalibrs As Integer
Public kembaliklm As Integer
Public kembalitamp_S As Integer
Public arrkembaliIp_1(1 To 64) As Integer
Public arrkembaliHasil_Ip_1(1 To 64) As Integer
Public arrkembali(1 To 64) As Integer

Public PlainText As String

Public Sub GenerateKey()


Static ubah As Integer
Static tampung, counter As Integer
tamp = Form1.txtkey.Text
If (Len(tamp) < 7) Then
While Len(tamp) < 7
tamp = tamp & " "
Wend
End If
jumlah = 0 'untuk mengisi nilai arrbiner: jumlah, y dan arrbiner di nol kan lagi
Y=1
For i = 1 To 64 Step 1
arrbiner(i) = 0
Next
j=1
For i = 1 To Len(tamp)
Call konvert_from_desimal(Asc(Mid(tamp, i, 1)), 2)
Next

'key
For i = 1 To 64
If i Mod 8 = 0 Then
counter = counter + 1
End If
arrtampung1(i) = arrbiner(i - counter)
Next
counter = 0
For i = 0 To 55
index1 = tabel_pc_1(i)
arrpc1(i + 1) = arrtampung1(index1)
Next
For i = 1 To 28
arrC(i) = arrpc1(i)
Next
For i = 29 To 56
arrD(i - 28) = arrpc1(i)
Next

n=1
For i = 1 To 16
geser_kiri = tabel_ls(i - 1)
If geser_kiri = 1 Then
For k = 1 To 28
If k <> 28 Then
arrtampC(k) = arrC(k + 1)
arrtampD(k) = arrD(k + 1)
ElseIf k = 28 Then
arrtampC(k) = arrC(1)
arrtampD(k) = arrD(1)
End If
Next
ElseIf geser_kiri = 2 Then
For k = 1 To 28
If k <> 27 And k <> 28 Then
arrtampC(k) = arrC(k + 2)
arrtampD(k) = arrD(k + 2)
ElseIf k = 27 Then
arrtampC(k) = arrC(1)
arrtampD(k) = arrD(1)
ElseIf k = 28 Then
arrtampC(k) = arrC(2)
arrtampD(k) = arrD(2)
End If
Next
End If
For l = 1 To 28
arrCD(l) = arrtampC(l)
Next
For l = 29 To 56
arrCD(l) = arrtampD(l - 28)
Next
For m = 0 To 47
index2 = tabel_pc_2(m)
arrKEY(i)(m + 1) = arrCD(index2)
Next m
For l = 1 To 28
arrC(l) = arrtampC(l)
arrD(l) = arrtampD(l)
Next
Next
End Sub

Public Sub Encryption(tamp As String)


Form1.txtCiphertext.Text = ""
Static tampung, counter As Integer

Y = 1 'untuk mengisi nilai arrbiner: jumlah, y dan arrbiner di nol kan lagi
jumlah = 0
For i = 1 To 64 Step 1
arrbiner(i) = 0
Next

j=1
For i = 1 To Len(tamp)
Call konvert_from_desimal(Asc(Mid(tamp, i, 1)), 2)
Next
For i = 1 To 64 Step 1
arrIp(i) = arrbiner(tabel_ip(i - 1))
Next i
For i = 1 To 64 Step 1 'membagi dua arr_Ip dalam arrR dan arrL
If (i <= 32) Then
arrL(i) = arrIp(i)
Else
arrR(i - 32) = arrIp(i)
End If
Next i
For looping = 1 To 15 Step 1
For j = 1 To 32 Step 1
arrtempL(j) = arrL(j)
Next
For j = 1 To 32 Step 1
arrL(j) = arrR(j)
Next j
Call RumusUntukEncryption(looping)
For l = 1 To 32
If ((arrtempL(l) + arrhasil_P(l)) Mod 2 = 0) Then
arrR(l) = 0
Else
arrR(l) = 1
End If
Next l
Next looping

Call RumusUntukEncryption(16)
For i = 1 To 32 Step 1
If ((arrL(i) + arrhasil_P(i)) Mod 2 = 0) Then
arrL(i) = 0
Else
arrL(i) = 1
End If
Next i
For i = 1 To 64 Step 1
If (i <= 32) Then
arrIp_1(i) = arrL(i)
Else
arrIp_1(i) = arrR(i - 32)
End If
Next i
For i = 1 To 64 Step 1
arrHasil_Ip_1(i) = arrIp_1(tabel_ip_1(i - 1))
Next i
Call konvert_to_desimal_UntukEncryption
If (bila_file = False) Then
Form1.txtCiphertext.Text = CipherText
Else
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("D:\cypertext.txt", ForWriting, True)
f.WRITE CipherText
End If
End Sub
Public Sub RumusUntukEncryption(Index As Integer)
Dim isi_S As Integer
Dim z As Integer
Dim counter As Integer
Dim X As Integer
For m = 1 To 48 Step 1 ' dikenakan tabel e
arrER(m) = arrR(tabel_e(m - 1))
Next m
For m = 1 To 48 Step 1
If ((arrER(m) + arrKEY(Index)(m)) Mod 2 = 0) Then
arrHasil_XOR(m) = 0
Else
arrHasil_XOR(m) = 1
End If
Next m
counter = 0
For z = 1 To 32 Step 1
arrhasil_S(z) = 0
Next z
For m = 6 To 48 Step 6
brs = 0
klm = 0
counter = counter + 1
brs = (arrHasil_XOR(m - 5) * 2 ^ 1) + (arrHasil_XOR(m) * 2 ^ 0)
klm = (arrHasil_XOR(m - 4) * 2 ^ 3) + (arrHasil_XOR(m - 3) * 2 ^ 2) +
(arrHasil_XOR(m - 2) * 2 ^ 1) + (arrHasil_XOR(m - 1) * 2 ^ 0)
tamp_S = (brs * 16) + klm + 1
isi_S = tabel_S(counter)(tamp_S)
X = counter * 4
While isi_S <> 0
arrhasil_S(X) = isi_S Mod 2
isi_S = isi_S / 2 - 0.49
X=X-1
Wend
Next m
For k = 1 To 32 'dikenakan tabel p , arrhasil_p sama dengan hasil dari rumus
arrhasil_P(k) = arrhasil_S(tabel_p(k - 1))
Next k
End Sub

Public Sub Decryption(tamp As String) 'seharusnya decrip tetap harus per blok
Form1.txtPlaintext.Text = ""
Static tampung, counter As Integer

Y = 1 'untuk mengisi nilai arrbiner: jumlah, y dan arrbiner di nol kan lagi
jumlah = 0
For i = 1 To 64 Step 1
arrbiner(i) = 0
Next

j=1
For i = 1 To Len(tamp)
Call konvert_from_desimal(Asc(Mid(tamp, i, 1)), 2)
Next

For i = 1 To 64 Step 1
arrkembaliHasil_Ip_1(i) = arrbiner(tabel_ip(i - 1))
Next i
For i = 1 To 64 Step 1
If (i <= 32) Then
arrkembaliL(i) = arrkembaliHasil_Ip_1(i)
Else
arrkembaliR(i - 32) = arrkembaliHasil_Ip_1(i)
End If
Next i
For i = 1 To 32 Step 1
arrkembalitempR(i) = arrkembaliR(i)
Next i

Call RumusUntukDecryption(16)
For i = 1 To 32 Step 1
If ((arrkembalihasil_P(i) + arrkembaliL(i)) Mod 2 = 0) Then
arrkembalitempL(i) = 0
Else
arrkembalitempL(i) = 1
End If
Next i

For i = 15 To 1 Step -1
For j = 1 To 32 Step 1
arrkembalitempR(j) = arrkembalitempL(j)
Next j
Call RumusUntukDecryption(i)
For j = 1 To 32 Step 1
If (arrkembaliR(j) + arrkembalihasil_P(j)) Mod 2 = 0 Then
arrkembalitempL(j) = 0
Else
arrkembalitempL(j) = 1
End If
Next j
For j = 1 To 32 Step 1
arrkembaliR(j) = arrkembalitempR(j)
Next j
Next i
For i = 1 To 64 Step 1
If (i <= 32) Then
arrkembaliIp(i) = arrkembalitempL(i)
Else
arrkembaliIp(i) = arrkembalitempR(i - 32)
End If
Next i
For i = 1 To 64 Step 1
arrkembali(i) = arrkembaliIp(tabel_ip_1(i - 1))
Next i

Call kembali_konvert_to_desimal_UntukDecryption
If (bila_file = False) Then
Form1.txtPlaintext.Text = PlainText
Else
'WRITE TEXT FILE
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("D:\PlainText.txt", ForWriting, True)
f.WRITE PlainText
End If
End Sub

Public Sub RumusUntukDecryption(Index As Integer)


Dim isi_S As Integer
Dim z As Integer
Dim counter As Integer
Dim X As Integer
For m = 1 To 48 Step 1 ' dikenakan tabel e
arrkembaliER(m) = arrkembalitempR(tabel_e(m - 1))
Next m
For m = 1 To 48 Step 1
If ((arrkembaliER(m) + arrKEY(Index)(m)) Mod 2 = 0) Then
arrkembaliHasil_XOR(m) = 0
Else
arrkembaliHasil_XOR(m) = 1
End If
Next m
counter = 0
For z = 1 To 32 Step 1
arrkembalihasil_S(z) = 0
Next z
For m = 6 To 48 Step 6
kembalibrs = 0
kembaliklm = 0
counter = counter + 1
kembalibrs = (arrkembaliHasil_XOR(m - 5) * 2 ^ 1) + (arrkembaliHasil_XOR(m)
* 2 ^ 0)
kembaliklm = (arrkembaliHasil_XOR(m - 4) * 2 ^ 3) + (arrkembaliHasil_XOR(m
- 3) * 2 ^ 2) + (arrkembaliHasil_XOR(m - 2) * 2 ^ 1) + (arrkembaliHasil_XOR(m - 1)
* 2 ^ 0)
kembalitamp_S = (kembalibrs * 16) + kembaliklm + 1
isi_S = tabel_S(counter)(kembalitamp_S)
X = counter * 4
While isi_S <> 0
arrkembalihasil_S(X) = isi_S Mod 2
isi_S = isi_S / 2 - 0.49
X=X-1
Wend
Next m
For k = 1 To 32 'dikenakan tabel p , arrhasil_p sama dengan hasil dari rumus
arrkembalihasil_P(k) = arrkembalihasil_S(tabel_p(k - 1))
Next k
End Sub

Public Sub konvert_to_desimal_UntukEncryption()


Dim desimal As Integer
For i = 8 To 64 Step 8
desimal = 0
For j = i To i - 7 Step -1
desimal = desimal + arrHasil_Ip_1(j) * (2 ^ (i - j))
Next j
CipherText = CipherText & CStr(Chr(desimal))
Next i
End Sub
Public Sub kembali_konvert_to_desimal_UntukDecryption()
Dim desimal As Integer
For i = 8 To 64 Step 8
desimal = 0
For j = i To i - 7 Step -1
desimal = desimal + arrkembali(j) * (2 ^ (i - j))
Next j
PlainText = PlainText & CStr(Chr(desimal))
Next i
End Sub
Public Sub konvert_from_desimal(temp As Integer, basis As Integer)
jumlah = jumlah + 1
Y = jumlah * 8
While temp <> 0
arrbiner(Y) = temp Mod 2
temp = temp / 2 - 0.49
Y=Y-1
Wend
End Sub

You might also like