17.【加試題】某數(shù)據(jù)加密方法描述如下:
(1)以字節(jié)為單位進(jìn)行加密處理;
(2)將1個(gè)字節(jié)的8位二進(jìn)制數(shù)分割成前4位與后4位兩個(gè)二進(jìn)制數(shù);
(3)分別將上述兩個(gè)4位二進(jìn)制數(shù)轉(zhuǎn)換為十進(jìn)制數(shù);
(4)將每個(gè)十進(jìn)制數(shù)轉(zhuǎn)換為1個(gè)加密字符,對(duì)應(yīng)的“密碼表”如下:
值(十進(jìn)制) |
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 |
0 |
s |
t |
c |
a |
r |
d |
小明按照上述方法,設(shè)計(jì)了一個(gè)字符串(僅包含ASCII字符)加密的VB程序,功能如下:?jiǎn)螕簟凹用堋卑粹oCommandl,程序依次將文本框Textl中每個(gè)字符的ASCII碼值作為1個(gè)字節(jié)轉(zhuǎn)換為兩個(gè)加密字符,連接這些加密字符,最后在文本框Text2中輸出加密結(jié)果.如圖1顯示了字符串中一個(gè)字符的加密過程:
程序運(yùn)行效果如圖2所示.
實(shí)現(xiàn)上述功能的VB程序如下:
(1)請(qǐng)?zhí)钊牒线m代碼.
Private Sub Command1_Click ( ?。?br />Dim n As Integer,s As String,i As Integer,ss As String
Dim a As Integer'存儲(chǔ)加密前字符的ASCII碼
Dim b1As Integer,b2As Integer'分別存儲(chǔ)分割、轉(zhuǎn)換后的兩個(gè)十進(jìn)制數(shù)
s=Text1.Text
①
For i=1To n
a=Asc(Mid(s,i,1))
b1=a\16
b2=a Mod 16
ss=ss+Code2Char(b1)+Code2Char(b2)
Text2.Text=ss
Next i
End Sub
'十進(jìn)制值轉(zhuǎn)換為加密字符的函數(shù)
Function Code2Char(c As Integer) As String
Dim s As String
s=“IlikeCHNpostcard”
Code2Char=②
End Function
(2)若將“密碼表”中值為“0”對(duì)應(yīng)的加密字符“I”改成“i”,加密后的密文可能無法解密,原因是
.