gpt4 book ai didi

vb.net - 加密 Caesar VB.net

转载 作者:行者123 更新时间:2023-12-02 06:43:35 25 4
gpt4 key购买 nike

我想通过 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

最佳答案

你可以试试我的方法。

加密:

(代码已清理)

''' <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))

Unmodified string: Hello World

Encrypted string: WtAADokDGAs

Decrypted string: Hello World

关于vb.net - 加密 Caesar VB.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29576001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com