导入Microsoft.VisualBasic
Public Class VigenereCipher
Public Shared Function Encrypt(ByVal cipherTxt As String, ByVal key As String)
Dim encryptedText As String = ""
For i As Integer = 1 To cipherTxt.Length
Dim temp As Integer = Asc(GetChar(cipherTxt, i)) _
+ Asc(GetChar(key, i Mod key.Length + 1))
encryptedText += Chr(temp)
Next
Return encryptedText
End Function
Public Shared Function Decrypt(ByVal cipherTxt As String, ByVal key As String)
Dim decryptedText As String = ""
For i As Integer = 1 To cipherTxt.Length
Dim temp As Integer = Asc(GetChar(cipherTxt, i)) _
- Asc(GetChar(key, i Mod key.Length + 1))
decryptedText += Chr(temp)
Next
Return decryptedText
End Function
End Class
我希望程序返回常规字符,因为它会输出unicode字符。
答案 0 :(得分:1)
直接来自MSDN:您应该使用Encoding.Convert
方法
示例代码(来自MSDN):
Public Function UnicodeToAscii( Byval unicodeString as String) As String
Dim ascii As Encoding = Encoding.ASCII
Dim unicode As Encoding = Encoding.Unicode
' Convert the string into a byte array.
Dim unicodeBytes As Byte() = unicode.GetBytes(unicodeString)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)
' Convert the new byte array into a char array and then into a string.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
Return asciiString
End Function
答案 1 :(得分:0)
代码工作正常,因为我在解密后获得相同的纯文本:我使用输入Encrypt="abcdef"
和key=""hxc"
执行加密代码,它生成temp
的输出,如下所示
何时
i=1 temp= 217 'Ù
i=2 temp= 197 'Å
i=3 temp= 203 'Ë
i=4 temp= 220 'Ò
i=5 temp= 200 'Ò
i=6 temp= 206 'Î
从进一步检查得出的结论是,对于大多数输入,您的算法将产生高于150的值。 reference表示扩展的ASCII码(字符代码128-255) 8位ASCII表有几种不同的变体。下表符合ISO 8859-1,也称为ISO Latin-1。代码129-159包含Microsoft®WindowsLatin-1扩展字符。
结论:生成Unicode字符的问题在于,您选择的算法会为127
生成大于temp
的值。