Option Explicit
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' American Standard Code for Information Interchange - ASCII (แอสกี้)
' http://www.ascii.ca/cp874.htm
' http://en.wikipedia.org/wiki/ASCII
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_Load()
Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
txtData.Text = "This is a book"
txtEncrypt.Text = ""
txtDecrypt.Text = ""
End Sub
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' เริ่มต้นการเข้ารหัส และ ถอดรหัส
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdRun_Click()
If Trim(txtData.Text) = "" Or Len(Trim$(txtData.Text)) = 0 Then Exit Sub
' ให้เข้ารหัสด้วยการ eXclusive OR - XOR พร้อมกับตัวเข้ารหัส
' ตัวเข้ารหัส หรือ กุญแจ ที่ผมตั้งไว้คือ 255 เป็นเลขจำนวนเต็มขนาด 1 ไบต์
' ซึ่งเมื่อเกิดการกระทำระดับบิตกับ XOR ค่านี้จะเปลี่ยนแปลงเป็น
' 1 1 1 1 1 1 1 1 (ฐาน 2) = 255 (ฐาน 10)
txtEncrypt.Text = Encrypt(txtData.Text, 255)
' อ่านวิธีการแปลงเลขฐาน 10 เป็นเลขฐาน 2 ประกอบด้วยครับ
' http://www.g2gnet.com/News/activenews_view.asp?articleID=129
' ถอดรหัสด้วยการ eXclusive OR - XOR พร้อมกับตัวถอดรหัส
txtDecrypt.Text = Decrypt(txtEncrypt.Text, 255)
End Sub
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' การเข้ารหัสแบบง่ายๆอาศัยหลักการของ XOR - บิตใดเหมือนกันจะได้ 0 บิตใดที่ต่างกันจะได้ 1
' ให้ MyData รับค่าชุดข้อความเพื่อเข้ารหัส
' XORvalue รับค่าตัวเข้ารหัส หรือ กุญแจ
' และคืนค่ากลับจากฟังค์ชั่นไปด้วย
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function Encrypt(MyData As String, XORvalue As Integer) As String
Dim i As Integer
' วนรอบตามจำนวนของข้อมูลที่เข้ารหัส
For i = 1 To Len(MyData)
Encrypt = Encrypt & Chr(Asc(Mid(MyData, i, 1)) Xor XORvalue)
Next
End Function
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' การถอดรหัส (Decrypt) ก็แค่ทำย้อนกลับด้วยวิธีการเดิม ค่าเดิม ...
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function Decrypt(MyData As String, XORvalue As Integer) As String
Dim i As Integer
' วนรอบตามจำนวนของข้อมูลที่ถอดรหัส
For i = 1 To Len(MyData)
Decrypt = Decrypt & Chr(Asc(Mid(MyData, i, 1)) Xor XORvalue)
Next
End Function
|