开发者

problem with programming playfair cipher

开发者 https://www.devze.com 2023-03-03 14:59 出处:网络
I have problem with my code Public Class Form1 Dim lm(4, 4) As Char Dim pt As String = \"\" Dim ct As String = \"\"

I have problem with my code

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

it gives error in this line ct = ct & lm(n3, m3) & lm(n4, m4)array out of range I cuoldn't find why can someone help me ?


Ah, spotted : )

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

You set it to false before the first iteration and inside the loop just before the (q+1)

    leave = False
    For n2 = 0 To 4  

You need to reset leave = False every time you loop:

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 


For a shorter code, try:

(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="")

OK maybe not that short. Or simple

0

精彩评论

暂无评论...
验证码 换一张
取 消