3

VB.netでシーザー暗号を使って暗号化したい。「ABC」を入力すると結果は「def」になりますが、「XYZ」を入力しても結果は「xyz」のままです。「XYZ」と入力すると、結果は「abc」になるはずです。皆さん、私を助けてもらえますか?

ソースコード

Public Function EncCaesar(ByVal s As String) As String
        Dim charSet1 As String = " ABCDEFGHIJKLMNOPQRSTUVWXYZ" 'my input string
        Dim charSet2 As String = " abcdefghijklmnopqrstuvwxyz" 'my encrypt key
        Dim i As Integer
        Dim pos, pos2 As Integer, encryptedChar, encryptedText
        For i = 1 To Len(s)
            pos = InStr(charSet1, Mid(s, i, 1))
            pos = pos + 3
            pos2 = InStr(charSet1, Mid(s, i, 1))
            pos2 = pos - 3
            If pos > 0 Then
                If pos2 > 24 Then
                    encryptedChar = Mid(charSet2, pos2, 1)
                    encryptedText = encryptedText + encryptedChar
                Else
                    encryptedChar = Mid(charSet2, pos, 1)
                    encryptedText = encryptedText + encryptedChar
                End If
            End If
        Next i
        EncCaesar = encryptedText
    End Function
4

2 に答える 2

2

あなたは私のアプローチを試すことができます。

暗号化:

(コードをクリーンアップ)

''' <summary>
''' Encrypts a string using Caesar's substitution technique.
''' </summary>
''' <remarks>
''' http://en.wikipedia.org/wiki/Caesar_cipher 
''' </remarks>
''' <param name="text">The text to encrypt.</param>
''' <param name="shift">The character shifting.</param>
''' <param name="charSet">The character set to use in encoding.</param>
''' <returns>The encrypted string.</returns>
Public Shared Function CaesarEncrypt(ByVal text As String,
                                     ByVal shift As Integer,
                                     Optional ByVal charSet As String =
                                                    "abcdefghijklmnopqrstuvwxyz" &
                                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & " ") As String

    Dim sb As New System.Text.StringBuilder With {.Capacity = text.Length}

    For Each c As Char In text

        Dim charIndex As Integer = charSet.IndexOf(c)

        If charIndex = -1 Then
            Throw New Exception(String.Format("Character '{0}' not found in character set '{1}'.", c, charSet))

        Else
            Do Until (charIndex + shift) < (charSet.Length)
                charIndex -= charSet.Length
            Loop

            sb.Append(charSet(charIndex + shift))

        End If

    Next c

    Return sb.ToString

End Function

復号化:

''' <summary>
''' Decrypts a string using Caesar's substitution technique.
''' </summary>
''' <remarks>
''' http://en.wikipedia.org/wiki/Caesar_cipher 
''' </remarks>
''' <param name="text">The encrypted text to decrypt.</param>
''' <param name="shift">The character shifting to reverse the encryption.</param>
''' <param name="charSet">The character set to use in decoding.</param>
''' <returns>The decrypted string.</returns>
Public Shared Function CaesarDecrypt(ByVal text As String,
                                     ByVal shift As Integer,
                                     Optional ByVal charSet As String =
                                                    "abcdefghijklmnopqrstuvwxyz" &
                                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & " ") As String

    Return CaesarEncrypt(text, shift, String.Join("", charSet.Reverse))

End Function

テストの使用と結果:

Dim value As String = "Hello World"

Dim encrypted As String = CaesarEncrypt(value, shift:=15)
Dim decrypted As String = CaesarDecrypt(encrypted, shift:=15)

Debug.WriteLine(String.Format("Unmodified string: {0}", value))
Debug.WriteLine(String.Format("Encrypted  string: {0}", encrypted))
Debug.WriteLine(String.Format("Decrypted  string: {0}", decrypted))

変更されていない文字列: Hello World

暗号化された文字列: WtAADokDGAs

復号化された文字列: Hello World

于 2015-04-11T10:31:00.253 に答える
1

これを試してみてください。この関数の Ascii コードを操作しました。


Public Function encCaesar(ByVal s As String) As String
    'abc 97 to 122
    'ABC 65 to 90
    Dim i As Integer
    Dim myString As String = Trim(s)
    Dim myAscii As Integer
    Dim myChar, myEncString As String
    myEncString = ""
    For i = 1 To myString.Length
        myChar = ""
        myAscii = Asc(Mid(myString, i, 1))
        Select Case myAscii
            Case 97 To 119 'smaller case a to w
                myChar = Chr(myAscii + 3)
            Case 65 To 87 'upper case A to W
                myChar = myAscii + 3
            Case 120 To 122 'smaller case x to z
                myChar = Chr((myAscii + 3) - 26)
            Case 88 To 90 ' upper case X to Z
                myChar = Chr((myAscii + 3) - 26)
            Case Else
                myChar = " "
        End Select
        myEncString = myEncString & myChar
    Next
    Return myEncString
End Function

以下のようにコードを変更します。このコードは小文字では機能しません。


Public Function EncCaesar(ByVal s As String) As String
    's = UCase(s)
    Dim charSet1 As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"
    Dim charSet2 As String = "abcdefghijklmnopqrstuvwxyzabc"
    Dim i As Integer
    Dim pos, pos2 As Integer, encryptedChar, encryptedText
    For i = 1 To Len(s)
        pos = InStr(charSet1, Mid(s, i, 1))
        pos = pos + 3
        pos2 = InStr(charSet1, Mid(s, i, 1))
        pos2 = pos - 3
        If pos > 0 Then
            encryptedChar = Mid(charSet2, pos, 1)
            encryptedText = encryptedText + encryptedChar
        End If
    Next i
    EncCaesar = encryptedText
End Function

于 2015-04-11T10:05:44.547 に答える