28 April 2008

Encrypt Decrypt VB6

Proses enkripsi biasa digunakan untuk menyimpan password. Kita akan membuat sebuah contoh enkripsi sederhana menggunakan Visual Basic 6.0.
Deklarasikan dua buah variable berikut:
Dim icChar As String
Dim i As Integer


Buat 2 buah fungsi bernama encrypt dan decrypt seperti di bawah ini.

Function Encrypt(ByVal icText As String) As String
 Dim icLen As Integer
 Dim icNewText As String
 icChar = ""
    icLen = Len(icText)
    For I = 1 To icLen
        icChar = Mid(icText, I, 1)
        Select Case Asc(icChar)
            Case 65 To 90
                icChar = Chr(Asc(icChar) + 127)
            Case 97 To 122
                icChar = Chr(Asc(icChar) + 121)
            Case 48 To 57
                icChar = Chr(Asc(icChar) + 196)
            Case 32
                icChar = Chr(32)
        End Select
        icNewText = icNewText + icChar
    Next
    Encrypt = icNewText
End Function


Function Decrypt(ByVal icText As String) As String
 Dim icLen As Integer
 Dim icNewText As String
 icChar = ""
    icLen = Len(icText)
    For I = 1 To icLen
        icChar = Mid(icText, I, 1)
        Select Case Asc(icChar)
            Case 192 To 217
                icChar = Chr(Asc(icChar) - 127)
            Case 218 To 243
                icChar = Chr(Asc(icChar) - 121)
            Case 244 To 253
                icChar = Chr(Asc(icChar) - 196)
            Case 32
                icChar = Chr(32)
        End Select
        icNewText = icNewText + icChar
    Next
    Decrypt = icNewText
End Function


Untuk contoh implementasi kita buah sebuah form dengan kontrol-kontrol berikut:

Tambahkan code:
Private Sub Command1_Click()
Text2.Text = Encrypt(Text1.Text)
End Sub

Private Sub Command2_Click()
Text2.Text = Decrypt(Text1.Text)
End Sub


Run dan coba hasilnya...

0 comments: