我正在尝试在Visual Basic 2010 Express(控制台应用程序)中创建基本的凯撒密码,但是每当对字符串进行加密时,它都会正确加密第一个字符,并用该相同的加密字符替换所有其他字符。
我已经尽力想了一切,并且在Internet上进行了搜索,找不到能够工作的基本控制台应用程序方式,我知道可以将其设置为家庭作业,并且可以完成其他工作在班上谁解决了这个问题。
Sub Main()
Dim Message As String
Dim EncMessage As String = ""
Dim counter As Integer = 0
Do Until counter = 1 'This just repeats the program
Console.Write("Please enter a string to encrypt: ")
Message = Console.ReadLine
Dim LetterArray() As Char = Message.ToCharArray
For i = 0 To LetterArray.Length - 1
EncMessage = EncMessage & Chr(Asc(LetterArray) + 3) 'Encrypts the characters one by one (i think this is where the issue is as it encrypts the first one correct but then just outputs the same character on top the correct amount of times)
'此if语句只是将字符X Y Z x y z循环回到字母的开头,因为它仅用于加密字母
If EncMessage = "{" Then
EncMessage = Chr(Asc(LetterArray) - 23)
ElseIf EncMessage = "|" Then
EncMessage = Chr(Asc(LetterArray) - 23)
ElseIf EncMessage = "}" Then
EncMessage = Chr(Asc(LetterArray) - 23)
ElseIf EncMessage = "[" Then
EncMessage = Chr(Asc(LetterArray) - 23)
ElseIf EncMessage = "\" Then
EncMessage = Chr(Asc(LetterArray) - 23)
ElseIf EncMessage = "]" Then
EncMessage = Chr(Asc(LetterArray) - 23)
End If
Next
Console.WriteLine(EncMessage)
Console.ReadKey()
EncMessage = ""
Loop
End Sub
例如,如果我输入“ sausage”,它将输出“ vvvvvvv”,但应输出“ vdtvdjh”,因为我希望它向前移动3个字符。请帮忙,谢谢。