编程playfair密码的问题

时间:2011-05-06 08:35:35

标签: vb.net encryption

我的代码有问题

Public Class Form1

Dim lm(4, 4) As Char
Dim pt As String = ""
Dim ct As String = ""
Dim key As String = ""
Dim n1, n2, n3, n4, m1, m2, m3, m4 As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    key = k.Text
    key.Replace(" ", "")
    pt = plaintext.Text
    If pt.Length Mod 2 <> 0 Then
        pt = pt & "e"
    End If
    pt = UCase(pt)
    pt.Replace("J", "I")
    Dim Value As String
    Dim NewValue As String
    key.Replace("J", "I")
    Value = key & "ABCDEFGHIKLMNOPQRSTUVWXYZ"
    NewValue = ""

    Value = UCase(Value)
    NewValue = Value(0)
    For i As Integer = 1 To Value.Length - 1
        If Not (InStr(1, NewValue, Value(i)) > 0) Then
            NewValue = NewValue & Value(i)
        End If
    Next
    Dim p As Integer = 0
    For n As Integer = 0 To 4
        For m As Integer = 0 To 4
            lm(n, m) = NewValue(p)
            p = p + 1
        Next
    Next
    Dim leave As Boolean = False
    For q As Integer = 0 To pt.Length - 1 Step 2
        For n1 = 0 To 4
            For m1 = 0 To 4
                If lm(n1, m1).Equals(pt(q)) Then
                    leave = True
                    Exit For
                End If
            Next
            If leave Then
                Exit For
            End If
        Next
        leave = False
        For n2 = 0 To 4
            For m2 = 0 To 4
                If lm(n2, m2).Equals(pt(q + 1)) Then
                    leave = True
                    Exit For
                End If
            Next
            If leave Then
                Exit For
            End If
        Next
        If n1 = n2 Then
            If m1 >= 4 Then
                m3 = 0
            Else
                m3 = m1 + 1
            End If
            If m2 >= 4 Then
                m4 = 0
            Else
                m4 = m2 + 1
            End If
        ElseIf m1 = m2 Then
            If n1 >= 4 Then
                n3 = 0
            Else
                n3 = n1 + 1
            End If
            If n2 >= 4 Then
                n4 = 0
            Else
                n4 = n2 + 1
            End If
        Else
            n3 = n1
            m3 = m2
            n4 = n2
            m4 = m1
        End If
        ct = ct & lm(n3, m3) & lm(n4, m4)
    Next
    ciphertext.Text = ct
    ct = ""
End Sub

End Class

它在此行中给出错误ct = ct&amp; lm(n3,m3)&amp; lm(n4,m4)数组超出范围 我不知道为什么有人会帮助我?

2 个答案:

答案 0 :(得分:1)

啊,发现:)

Dim leave As Boolean = False
For q As Integer = 0 To pt.Length - 1 Step 2
    For n1 = 0 To 4 

在第一次迭代之前和在(q + 1)

之前的循环内部将其设置为false
    leave = False
    For n2 = 0 To 4  

每次循环时都需要重置leave = False

Dim leave As Boolean
For q As Integer = 0 To pt.Length - 1 Step 2
leave = False  ' This was missing
    For n1 = 0 To 4 

答案 1 :(得分:0)

要获得更短的代码,请尝试:

(a,b,f,g,c)="".join(input("CODE: ").split()),input("Polybius S: "),"","",1
for(i)in(a):
    if(c%2)==0:
        g+=i
    else:
        f+=i
    c+=1
for(j)in(range(0,len(f))):
    if(b.find(f[j])%5)!=(b.find(g[j])%5)and(int(((b.find(f[j]))-(b.find(f[j])%5))/5))!=(int(((b.find(g[j]))-(b.find(g[j])%5))/5)):
        print(b[((int(((b.find(f[j]))-(b.find(f[j])%5))/5))*5)+(b.find(g[j])%5)],end="")
        print(b[((int(((b.find(g[j]))-(b.find(g[j])%5))/5))*5)+(b.find(f[j])%5)],end="")
    elif(b.find(f[j])%5)==(b.find(g[j])%5)and(int(((b.find(f[j]))-(b.find(f[j])%5))/5))!=(int(((b.find(g[j]))-(b.find(g[j])%5))/5)):
        print(b[((((int(((b.find(f[j]))-(b.find(f[j])%5))/5))-1)%5)*5)+b.find(f[j])%5],end="")
        print(b[((((int(((b.find(g[j]))-(b.find(g[j])%5))/5))-1)%5)*5)+b.find(g[j])%5],end="")
    elif(b.find(f[j])%5)!=(b.find(g[j])%5)and(int(((b.find(f[j]))-(b.find(f[j])%5))/5))==(int(((b.find(g[j]))-(b.find(g[j])%5))/5)):
        print(b[((int(((b.find(f[j]))-(b.find(f[j])%5))/5))*5)+((b.find(f[j])%5)-1)%5],end="")
        print(b[((int(((b.find(g[j]))-(b.find(g[j])%5))/5))*5)+((b.find(g[j])%5)-1)%5],end="")

好吧也许不那么简短。或者简单