【发布时间】:2015-04-12 03:27:06
【问题描述】:
我想在 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
【问题讨论】:
标签: vb.net encryption caesar-cipher