Encrypt and Decrypt Data với ASCII
Hôm trước mình có giới thiệu đến các bạn cách mã hóa MD5, thật ra có nhiều cách mã hóa khác nhau, hôm nay mình giới thiệu tiếp các bạn thuật toán mã hóa ASCII.
Bạn thiết kế form đơn giản như trên, sau đó các bạn viết cho mình 2 thủ tục Encrypt và Decrypt.
- Mã hóa dữ liệu.
Public Sub Encrypt()
Dim Enctypt As String = ""
Dim letter As Char
Dim i, charInFile As Short
SaveFileDialog1.Filter = "Text file |*.txt"
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Try
charInFile = TextBox1.Text.Length
For i = 0 To charInFile - 1
letter = TextBox1.Text.Substring(i, 1)
Enctypt = Enctypt & Chr(Asc(letter) + 1)
Next
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, Enctypt, False)
TextBox1.Text = Enctypt
TextBox1.Select(0, 0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
- Giải mã dữ liệu
Public Sub Decrypt()
Dim AllText As String
Dim i, CharInFile As Short
Dim letter As Char
Dim Decrypt As String = ""
OpenFileDialog1.Filter = "Text file |*.txt"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Try
AllText = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
CharInFile = AllText.Length
For i = 0 To CharInFile - 1
letter = AllText.Substring(i, 1)
Decrypt = Decrypt & Chr(Asc(letter) - 1)
Next
TextBox1.Text = Decrypt
TextBox1.Select(0, 0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
- Những bạn nào làm biết code có thể tải demo phía dưới nhé.