用vb写了一个vigenere密码的密钥寻找程序,结果总是不全正确,请指点
我用频率对照的方法,通过算重复互指数来确定密钥。
找到了密钥的长度为6,并按密钥将密文分组,然后通过算重复互指数找各个位置上的密钥,程序如下:
Private Sub dcode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dcode.Click
Dim c(5) As String
c(0) = g1.Text
c(1) = g2.Text
c(2) = g3.Text
c(3) = g4.Text
c(4) = g5.Text
c(5) = g6.Text
Dim i As Integer '循环控制变量
Dim k As Integer '循环控制变量
Dim j As Integer '循环控制变量
Dim n As Integer '循环控制变量
Dim x As Integer '密钥长度
Dim m2(25) As Double '密文按密钥分组后个字母频率
Dim h(25) As Integer '密文按密钥分组后各个字母的频数
Dim dcd(5) As Integer '密文移位
Dim rtw(25) As Double '正常的字母频率
Dim yrtw(25) As Double '移位用数组
Dim rate(25) As Double '点积结果
Dim max1 As Double '找最大点积结果时的临时变量
For k = 0 To 25 '初始化
rate(k) = 0
Next k
For k = 0 To 5 '初始化
dcd(k) = 0
Next k
For i = 0 To 25 '初始化
h(i) = 0
Next i
x = movestep.Text
rtw(0) = 0.082 '输入个字母常规统计频率
rtw(1) = 0.015
rtw(2) = 0.028
rtw(3) = 0.043
rtw(4) = 0.127
rtw(5) = 0.022
rtw(6) = 0.02
rtw(7) = 0.061
rtw(8) = 0.07
rtw(9) = 0.002
rtw(10) = 0.008
rtw(11) = 0.04
rtw(12) = 0.024
rtw(13) = 0.067
rtw(14) = 0.075
rtw(15) = 0.019
rtw(16) = 0.001
rtw(17) = 0.06
rtw(18) = 0.063
rtw(19) = 0.091
rtw(20) = 0.028
rtw(21) = 0.01
rtw(22) = 0.023
rtw(23) = 0.001
rtw(24) = 0.02
rtw(25) = 0.001
For i = 0 To x - 1 '大循环,处理按密钥长度分组
For j = 0 To c(i).Length - 1 '统计第j个分组中个字母的频率
If c(i).Chars(j) = "a " Then
h(0) = h(0) + 1
End If
If c(i).Chars(j) = "b " Then
h(1) = h(1) + 1
End If
If c(i).Chars(j) = "c " Then
h(2) = h(2) + 1
End If
If c(i).Chars(j) = "d " Then
h(3) = h(3) + 1
End If
If c(i).Chars(j) = "e " Then
h(4) = h(4) + 1
End If
If c(i).Chars(j) = "f " Then
h(5) = h(5) + 1
End If
If c(i).Chars(j) = "g " Then
h(6) = h(6) + 1
End If
If c(i).Chars(j) = "h " Then
h(7) = h(7) + 1
End If
If c(i).Chars(j) = "i " Then
h(8) = h(8) + 1
End If
If c(i).Chars(j) = "j " Then
h(9) = h(9) + 1
End If
If c(i).Chars(j) = "k " Then
h(10) = h(10) + 1
End If
If c(i).Chars(j) = "l " Then
h(11) = h(11) + 1
End If
If c(i).Chars(j) = "m " Then
h(12) = h(12) + 1
End If
If c(i).Chars(j) = "n " Then
h(13) = h(13) + 1
End If
If c(i).Chars(j) = "o " Then
h(14) = h(14) + 1
End If
If c(i).Chars(j) = "p " Then
h(15) = h(15) + 1
End If
If c(i).Chars(j) = "q " Then
h(16) = h(16) + 1
End If
If c(i).Chars(j) = "r " Then
h(17) = h(17) + 1
End If
If c(i).Chars(j) = "s " Then
h(18) = h(18) + 1
End If
If c(i).Chars(j) = "t " Then
h(19) = h(19) + 1
End If
If c(i).Chars(j) = "u " Then
h(20) = h(20) + 1
End If
If c(i).Chars(j) = "v " Then
h(21) = h(21) + 1
End If
If c(i).Chars(j) = "w " Then
h(22) = h(22) + 1
End If
If c(i).Chars(j) = "x " Then
h(23) = h(23) + 1
End If
If c(i).Chars(j) = "y " Then
h(24) = h(24) + 1
End If
If c(i).Chars(j) = "z " Then
h(25) = h(25) + 1
End If
Next j
For k = 0 To 25
m2(k) = h(k) / c(i).Length
Next k
For k = 0 To 25
h(k) = 0
Next k
For j = 0 To 25 '将正常情况下字母平均统计频率移位,共26种情况
For k = 0 To 25 - j '移位操作
yrtw(k + j) = rtw(k)
Next k
If j <> 0 Then
k = 25 - j + 1
n = 0
Do While k < 26
yrtw(n) = rtw(k)
k = k + 1
n = n + 1
Loop
End If
For k = 0 To 25 '算移位数组和密钥分组数组的字母的频率的点积
rate(j) = rate(j) + yrtw(k) * m2(k)
Next k
Next j
'找最大最大点积,即为该位密钥
max1 = rate(0)
For k = 0 To 25
If rate(k) > max1 Then
max1 = rate(k)
dcd(i) = k
End If
Next k
For k = 0 To 25
rate(k) = 0
Next k
Next i
'将结果显示
dc1.Text = dcd(0)
dc2.Text = dcd(1)
dc3.Text = dcd(2)
dc4.Text = dcd(3)
dc5.Text = dcd(4)
dc6.Text = dcd(5)
End Sub
刚开始用vb函数的写法还不熟悉,所以程序中没用函数,而且写的也很繁琐,见笑了。
密文如下:
ocwyikoooniwugpmxwkt
zdwgtssayjzwyemdlbnq
aaavsuwdvbrflauploou
bfgqhgcscmgzlatoedcs
deidpbhtmuovpiekifpi
mfnoamvlpqfxejsmxmpg
kccaykwfzpyuavtelwhr
hmwkbbvgtguvtefjlodf
efkvpxsgrsorvgtajbsa
uhzrzalkwuowhgedefns
wmrciwcpaaavogpdnfpk
tdbalsisurlnpsjyeatc
uceesohhdarkhwotikbr
oqrdfmzghgucebvgwcdq
xgpbgqwlpbdaylooqdmu
hbdqgmyweuik
一共312个字母
[解决办法]
mark