1)以字节为单位进行加密处理;
2)将1个字节的8位二进制数分割成前4位与后4位两个二进制数;
3)分别将上述两个4位二进制数转换成十进制数;
4)将每个十进制数转换为1个加密字符,对应的“密码表”如下:
值(十进制) |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
加密字符 |
I |
l |
i |
k |
e |
C |
H |
N |
p |
o |
s |
t |
c |
a |
r |
d |
小华按照上述方法,设计了一个字符串(仅包含ASCII字符)解密的VB程序,功能如下:在文本框Text1中输入一串密文,单击“解密”按钮Command1,在文本框Text2中显示解密结果。
下表显示了字符串中一个字符的加密过程:
m |
←转换前字符m |
|||||||
109 |
←字符m的ASCII码十进制值 |
|||||||
0 |
1 |
1 |
0 |
1 |
1 |
0 |
1 |
←对应的二进制数 |
6 |
13 |
←分割、转换后的十进制数 |
||||||
H |
a |
←对应的加密字符 |
||||||
程序运行效果如图所示。
Private Sub Command1_Click()
Dim a(1 To 100) As Integer
Dim s1 As String, s2 As String, c As String
Dim x As Integer, k As Integer, i As Integer, j As Integer
Const code = "IlikeCHNpostcard"
n = Len(code)
s1 = Text1.Text
x = 0: k = 0
For i = 1 To Len(s1)
c = Mid(s1, i, 1)
For j = 1 To n
If Then
pos = j - 1
x = x * 16 + pos
Exit For '退出当前For循环
End If
Next j
If i Mod 2 = 0 Then
a(k) = x
x = 0
End If
Next i
For i = 1 To k
Next i
Text2.Text = s2
End Sub