⑴授权码由10位字符组成,前8位为日期的密文,后2位为验证码;
⑵取日期的第1位字符,若该字符第一次出现,则直接取其对应加密字符,否则取下一个不重复的加密字符。按此操作,依次取出该日期余下各位对应的加密字符;
⑶求出所有日期字符数值的和,将和除16取余加1得到一位验证码字符;若和不小于16,则将和除16取整加1得到另一位验证码字符,否则另一位验证码字符为“X”。取出验证码重复时,处理规则与(2)相同。加密(验证码)字符对应表如下:
值 (十进制) |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
加密 (验证码) 字符 |
K |
n |
G |
j |
L |
t |
W |
b |
O |
a |
P |
H |
Z |
q |
Y |
c |
小明设计了一个生成8位日期(YYYYMMDD格式)授权码的VB程序。程序功能如下:在文本框Text1中输入一个8位有效日期,单击“生成授权码”按钮Command1,在标签Label2中显示出该日期的授权码。运行效果如图所示。实现上述功能的VB程序段如下:
Dim f(0 To 15) As Integer
Const Code = “KnGjLtWbOaPHZqYc”
Private Sub Command1_Click()
Dim rq As String, sq As String
Dim c As Integer, i As Integer, d As Integer
For i = 0 To 15
f(i) = 0
Next i
rq = Text1.Text
sq=“”:d = 0
If Len(rq) <> 8 Then
Label2.Caption =“请输入8位日期!”
Else
For i = 1 To 8
c = Val(Mid(rq, i, 1))
d = d + c
sq =+sq
Next i
If d >= 16 Then
yz = GetChar(d Mod 16 + 1) + GetChar(d\16 + 1)
Else
yz = GetChar(d Mod 16 + 1) + “X”
End If
Label2.Caption = sq + yz
End If
End Sub
‘获取不重复的加密字符
Function GetChar(x As Integer) As String
Dim flag As Boolean
flag = False
Do While flag = False
f(x) = f(x) + 1
If f(x) = 1 Then
f(x) = f(x) + 1
flag = True
Else
x = (x + 1) Mod 10
End If
Loop
End Function