1)以字節(jié)為單位進行加密處理;
2)將1個字節(jié)的8位二進制數(shù)分割成前4位與后4位兩個二進制數(shù);
3)分別將上述兩個4位二進制數(shù)轉(zhuǎn)換成十進制數(shù);
4)將每個十進制數(shù)轉(zhuǎn)換為1個加密字符,對應(yīng)的“密碼表”如下:
值(十進制) |
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 |
小華按照上述方法,設(shè)計了一個字符串(僅包含ASCII字符)解密的VB程序,功能如下:在文本框Text1中輸入一串密文,單擊“解密”按鈕Command1,在文本框Text2中顯示解密結(jié)果。
下表顯示了字符串中一個字符的加密過程:
m |
←轉(zhuǎn)換前字符m |
|||||||
109 |
←字符m的ASCII碼十進制值 |
|||||||
0 |
1 |
1 |
0 |
1 |
1 |
0 |
1 |
←對應(yīng)的二進制數(shù) |
6 |
13 |
←分割、轉(zhuǎn)換后的十進制數(shù) |
||||||
H |
a |
←對應(yīng)的加密字符 |
||||||
程序運行效果如圖所示。
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循環(huán)
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