Teknik membuat Enkripsi

Code berikut ini cocok untuk dijadikan latihan untuk memdalam kemampuan logika dalam programing Visual Basic.

Membuat function EncDec:

Function EncDec(inData As Variant, Optional inPW As Variant = "") As Variant
On Error Resume Next
Dim arrSBox(0 To 255) As Integer
Dim arrPW(0 To 255) As Integer
Dim Bi As Integer, Bj As Integer
Dim mKey As Integer
Dim i As Integer, j As Integer
Dim x As Integer, y As Integer
Dim mCode As Byte, mCodeSeries As Variant

EncDec = ""
If Trim(inData) = "" Then
Exit Function
End If

If inPW <> "" Then
j = 1
For i = 0 To 255
arrPW(i) = Asc(Mid$(inPW, j, 1))
j = j + 1
If j > Len(inPW) Then
j = 1
End If
Next i
Else
For i = 0 To 255
arrPW(i) = 0
Next i
End If

For i = 0 To 255
arrSBox(i) = i
Next i

j = 0
For i = 0 To 255
j = (arrSBox(i) + arrPW(i)) Mod 256
x = arrSBox(i)
arrSBox(i) = arrSBox(j)
arrSBox(j) = x
Next i

mCodeSeries = ""
Bi = 0: Bj = 0
For i = 1 To Len(inData)
Bi = (Bi + 1) Mod 256
Bj = (Bj + arrSBox(Bi)) Mod 256
' Tukar
x = arrSBox(Bi)
arrSBox(Bi) = arrSBox(Bj)
arrSBox(Bj) = x

'siapkan kunci untuk XOR
mKey = arrSBox((arrSBox(Bi) + arrSBox(Bj)) Mod 256)

'gunakan operasi XOR
mCode = Asc(Mid$(inData, i, 1)) Xor mKey
mCodeSeries = mCodeSeries & Chr(mCode)
Next i
EncDec = mCodeSeries
End Function


Ilmu Visual Basic



lihat belajar asp.net dari nol