Reputation: 5
Imports 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
I would want the program to return regular characters because it outputs unicode characters.
Upvotes: 0
Views: 12790
Reputation:
The code works fine, since i got the same plain text after decryption : I execute the encryption code with input Encrypt="abcdef"
and key=""hxc"
it produces the output for temp
as follows
when
i=1 temp= 217 'Ù
i=2 temp= 197 'Å
i=3 temp= 203 'Ë
i=4 temp= 220 'Ò
i=5 temp= 200 'Ò
i=6 temp= 206 'Î
from further checking i got the conclusion that your algorithm will produce a value higher than 150 for most of the inputs. The reference says that, The extended ASCII codes (character code 128-255) There are several different variations of the 8-bit ASCII table. The table below is according to ISO 8859-1, also called ISO Latin-1. Codes 129-159 contain the Microsoft® Windows Latin-1 extended characters.
Comclusion: The problem for producing Unicode characters is that the algorithm you ware choosing will produce a value greater than 127
for temp
.
Upvotes: 0
Reputation: 2520
Straight from MSDN: You should use the Encoding.Convert
Method
Example code (from 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
Upvotes: 3