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 回答