For my computing GCSE course i had to create a keyword cipher and most of my class did it in VB console, so so did it. Little did I know, it was the most difficult project i have had to code. I can't figure out what is wrong with the code!
E.G Its supposed to be AB + AB = BD but it is being outputted as AB + AB = BC

Any help is appreciated
(I have commented on the code if that helps)

Dim final As String 'variables for the function final is declared
        Dim offset As String
        Dim plain As String
        Dim encoded_message() As Integer

        Console.WriteLine("Enter text to encrypt: ")
        plain = Console.ReadLine.ToUpper 'enter text to encrypt
        Console.WriteLine("Input encryption keyword:")
        offset = Console.ReadLine 'enter offset
        offset = Asc(offset)

        Console.WriteLine("Your Encrypted text is: ")

        ReDim Preserve encoded_message(0 To plain.Length - 1)
        For stepper As Integer = 0 To plain.Length - 1
            encoded_message(stepper) = Asc(plain(stepper)) + offset.Length - 1 'encrypts the text

            If encoded_message(stepper) > 90 Then 'if ascii value is above 90
                encoded_message(stepper) = (encoded_message(stepper) + offset) - 96 'ascii value decreases by 26
                final = Chr(encoded_message(stepper))
            ElseIf encoded_message(stepper) = "35" Then 'if ascii value is 35 (space)
                encoded_message(stepper) = "0" 'changes ascii value to 0 (blank)
                final = Chr(encoded_message(stepper))
            ElseIf encoded_message(stepper) >= "48" And encoded_message(stepper) <= "57" Then 'if string inputted contaions a number
                encoded_message(stepper) = "0" 'numbers ascii value is changed to 0 (blank)
                final = Chr(encoded_message(stepper)) 'output encrypted text
                final = Chr(encoded_message(stepper)) 'output encrypted text
            End If
            Console.Write(Chr(encoded_message(stepper))) 'output encrypted text

A cypher is an algorithm for encrypting/decrypting.

Why are you adding two strings - AB + AB - I don't understand that. Can you explain please?

Say every letter in the alphabet had a number.
A=1 B=2 C=2 D=4 etc
i want it so you enter what to encrypt, say AB, and a keyword to encrypt it, AB again. The first letters of the keyword and what you want to encrypt is A, and A=1 so 1+1=2 which is B. The next two is B and B, B=2 so 2+2=4 which is D. The encrypted text would be BD

So are you saying whatever you want to encrypt you had to provide a key of the same length?

So if you want to encrypt a 20 character string you have to provide a 20 character key?

No, if the text you want to encrypt is longer than the keyword, it loops back round and goes from the start of the keyword

Sorry but that just looks like an awful mess and just looks all wrong to me.

I wouldn't code it that way.

Also if you encode numbers to 0 then when you are decrypting how to you know what zero is since 1 through to 9 would encrypt to 0 right?

I honestly don't know, thats how I coded my Caesar cipher and that worked fine so I thought I should try coding it that way. How would you code it?

Well I can code you an example if you like but I would need to ask some questions first.

So first question - what is the range of permissable characters you wish to encode - is it just letters and numbers for exmaple or can we have spaces, special characters etc?

only letters and numbers, if it is a space or other character i want it to change the ascii to 0 (blank)

You can restrict which characters are allowed, that's fine. But you cannot encrypt like that as you will not be able to decrypt. Does that make sense?

Ok and the same characters for the key?

Also any min and max size for the input and for the key?

Ok an some more thing.

I don't have VB6 installed. Can I code this in for you? If it has to be VB6 I won't be able to do it.

Well he is writing it as a console program as he states in his first sentence. So, yes you can use to create a console project. In my previous post as just missed the (?) to complete my sentence.

This article has been dead for over six months. Start a new discussion instead.