如何推进字母表中的字符串 3 个字母(凯撒密码)?
How to advance string 3 letters in the alphabet (Caesar cipher)?
我正在尝试编写一个程序来加密用户提交的字符串。我想使用一种加密技术,其中字符串在字母表中提前 3 个字母。
示例:abc
将变为 def
.
目前我有一个文本框(TextBox1
)和一个按钮(Button1
)。
到目前为止我的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rawText As String
rawText = TextBox1.Text
Dim letterTxt As String = Chr(Asc(rawText) + 3)
MsgBox(letterTxt)
End Sub
问题是当我运行它时,它只输出1个字母。
我做错了什么?
一个Caesar cipher方法。接受正移和负移以及可选的一些字母。
后者,将使用与通常的 US-ASCII 不同的 ASCII 表进行测试。
它不会改变数字(跳过)但如果需要,您可以使用相同的模式修改它。
使用 Scramble
参数 select 打乱 (True) 或取消打乱 (False)。
示例测试代码:
Dim Scrambled1 As String = CaesarCipher("ABCXYZabcxyz", 3, True)
Dim Scrambled2 As String = CaesarCipher("ABCXYZabcxyz", -5, True)
'Scrambled1 is now DEFABCdefabc
'Scrambled2 is now VWXSTUvwxstu
Dim Unscrambled As String = CaesarCipher(Scrambled2, -5, false)
'Unscrambled is now ABCXYZabcxyz
Function CaesarCipher(Input As String, CaesarShift As Integer, Scramble As Boolean, Optional AlphabetLetters As Integer = 26) As String
Dim CharValue As Integer
Dim MinValue As Integer = AscW("A"c)
Dim MaxValue As Integer = AscW("Z"c)
Dim ScrambleMode As Integer = If((Scramble), 1, -1)
Dim output As StringBuilder = New StringBuilder(Input.Length)
If Math.Abs(CaesarShift) >= AlphabetLetters Then
CaesarShift = (AlphabetLetters * Math.Sign(CaesarShift)) - Math.Sign(CaesarShift)
End If
For Each c As Char In Input
CharValue = AscW(c)
If Not Char.IsNumber(c) Then
CharValue = CharValue + (CaesarShift * ScrambleMode) Mod AlphabetLetters
CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) > MaxValue, CharValue - AlphabetLetters, CharValue)
CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) < MinValue, CharValue + AlphabetLetters, CharValue)
End If
output.Append(ChrW(CharValue))
Next
Return output.ToString()
End Function
我正在尝试编写一个程序来加密用户提交的字符串。我想使用一种加密技术,其中字符串在字母表中提前 3 个字母。
示例:abc
将变为 def
.
目前我有一个文本框(TextBox1
)和一个按钮(Button1
)。
到目前为止我的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rawText As String
rawText = TextBox1.Text
Dim letterTxt As String = Chr(Asc(rawText) + 3)
MsgBox(letterTxt)
End Sub
问题是当我运行它时,它只输出1个字母。
我做错了什么?
一个Caesar cipher方法。接受正移和负移以及可选的一些字母。
后者,将使用与通常的 US-ASCII 不同的 ASCII 表进行测试。
它不会改变数字(跳过)但如果需要,您可以使用相同的模式修改它。
使用 Scramble
参数 select 打乱 (True) 或取消打乱 (False)。
示例测试代码:
Dim Scrambled1 As String = CaesarCipher("ABCXYZabcxyz", 3, True)
Dim Scrambled2 As String = CaesarCipher("ABCXYZabcxyz", -5, True)
'Scrambled1 is now DEFABCdefabc
'Scrambled2 is now VWXSTUvwxstu
Dim Unscrambled As String = CaesarCipher(Scrambled2, -5, false)
'Unscrambled is now ABCXYZabcxyz
Function CaesarCipher(Input As String, CaesarShift As Integer, Scramble As Boolean, Optional AlphabetLetters As Integer = 26) As String
Dim CharValue As Integer
Dim MinValue As Integer = AscW("A"c)
Dim MaxValue As Integer = AscW("Z"c)
Dim ScrambleMode As Integer = If((Scramble), 1, -1)
Dim output As StringBuilder = New StringBuilder(Input.Length)
If Math.Abs(CaesarShift) >= AlphabetLetters Then
CaesarShift = (AlphabetLetters * Math.Sign(CaesarShift)) - Math.Sign(CaesarShift)
End If
For Each c As Char In Input
CharValue = AscW(c)
If Not Char.IsNumber(c) Then
CharValue = CharValue + (CaesarShift * ScrambleMode) Mod AlphabetLetters
CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) > MaxValue, CharValue - AlphabetLetters, CharValue)
CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) < MinValue, CharValue + AlphabetLetters, CharValue)
End If
output.Append(ChrW(CharValue))
Next
Return output.ToString()
End Function