题型:综合题 题类:常考题 难易度:普通
浙江省高中信息技术 加密问题练习
加密算法如下:
1)将明码中每个字符的八位二进制ASCII码(不足八位的左端补0,凑足八位)分成两段(左4位一段,右4位为另一段),如字符“C”的二进制ASCII值为01000011,分段后为0100,0011;
2)将高位段(左边4位)左移一位,并将原第一位数码移到第4位(如0100转化为1000),再转化为十六进制数(如1000化为8);
3)对另一段(右4位)执行2)所示算法,如0011→0110→6;
4)顺次连接两位十六进制数,得到该字符的暗码,如“C”的暗码为“86”;
5)将每个字符的暗码按照明码的顺序连接。
实现上述功能的VB程序如下,请回答下列问题:
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim m As Integer, k As Integer, c As String
Dim x As Integer, y As Integer
Dim d(1 To 8) As Integer ‘数组d存储字符ASCII码二进制从左到右的各位数码
Dim mw As String ‘mw存储暗码
mw =“”
For i = 1 To Len(Text1.Text)
c = Mid(Text1.Text,i,1)
For j = 1 To 8
d(j) = 0
Next j
m = Asc(c)
Do While m > 0
d(k) = m Mod 2
m = m \ 2
k =k-1
Loop
x = d(1) :y = d(5)
For j = 1 To 3
d(j) =d(j+1)
Next j
d(4) = x:d(8) = y
mw = mw + btoh(d)
Next i
Text2.Text = mw
End Sub
‘将数组元素中的二进制数转换成对应的十六进制数
Function btoh(d() As Integer) As String
Dim s As Integer, i As Integer, str1 As String, ch As String
str1 = “0123456789ABCDEF”: s = 0: ch =“”
For i = 1 To 8
s = s * 2 + d(i)
If i = 4 Then
ch = Mid(str1,s+1,1)
s = 0
End If
Next i
End Function
试题篮