Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   ROT Decryptor (http://www.programmingforums.org/showthread.php?t=4806)

Intimidat0r Jul 8th, 2005 4:03 PM

ROT Decryptor
 
I have tried to make a program to decrypt simple ROT decryptions which is where if it's like ROT 4 you move each letter 4 spaces down, so A becomes E, W becomes Z and so on. Here is my code:

:

    Dim ROTIndex As Integer
    Dim Encrypted As String
    Dim Decrypted() As String = ""

    Sub Main()
        Console.WriteLine("Rotation index: ")
        ROTIndex = Console.ReadLine()
        Console.WriteLine("Encrypted text: ")
        Encrypted = Console.ReadLine()
        For Index As Integer = 0 To Encrypted.Length
            Decrypted = Decrypted & Chr(Asc(Encrypted.Substring(Index, 1) + 13))
        Next
        Console.WriteLine("Decrypted Text: " & Decrypted)
    End Sub


It looks all well and good but when I try to run it I get a bug saying "Invalid cast exception -> cast from string "h" to type 'Double' is not valid" and it says it's occurring on the line that says this:

:

Decrypted = Decrypted & Chr(Asc(Encrypted.Substring(Index, 1) + 13))

Does anybody know why it may be doing this?

Rory Jul 8th, 2005 8:12 PM

Your adding 13 (byte) to a character which should work as they are actually the same thing but no in VB's universe.
:

Chr(Asc(Encrypted.Substring(Index, 1)) + 13)

For some reason VB's so Unicodified (Grrr! English IS the only language in the world). This works though:

:

    Dim ROTIndex As Byte
    Dim Buffer As Char()

    Sub Main()
        Console.Write("Rotation index: ")
        ROTIndex = Console.ReadLine()

        Console.Write("Encrypted text: ")
        Buffer = Console.ReadLine()

        For Index As Integer = Buffer.GetLowerBound(0) To Buffer.GetUpperBound(0)
            Buffer(Index) = Chr(Asc(Buffer(Index)) + ROTIndex)
        Next

        Console.WriteLine("Decrypted Text: " & Buffer)

        Console.ReadLine()
    End Sub



All times are GMT -5. The time now is 5:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC