Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Help With Alphabet Cipher

John Willows
Greenhorn
Posts: 3
So I had to create a program in my computer science course that will generate a random cipher alphabet and then allow the user to input a message and then have the program encode and decode that message with the cipher alphabet. Here is my code on that:

The encoding part works, I think. The output seems accurate based on what the user enters, however I can't get the decoding part to work. I can't seem to come up with any code that will make the decipher method to work. Does anyone know how I can decode the user's message as well? Thanks!

Winston Gutkowski
Bartender
Posts: 10417
63
Forrest Waldron wrote:The encoding part works, I think. The output seems accurate based on what the user enters, however I can't get the decoding part to work...

Don't think. KNOW.
Why does your encoding part work? You should not only be able to explain that, but prove it by good testing (or, if you prefer, by code path analysis; but it can be quite a bit more complicated).

Why are you attempting to decode until you KNOW that your encoding works?

I suspect this an old "cart before the horse" problem.

Winston

John Willows
Greenhorn
Posts: 3
That's the thing though. I have use a random value to create the cipher alphabet. So each time it encodes a message it is using a different cipher alphabet pretty much. Now from testing this by hand on paper, the encoding part does work. However, I can't be 100% sure until I do the opposite actions to decode the message to see if it matches the first message or not. But everytime I try to decode it, it just comes up with special characters such as %, @, and &. That's by pretty much doing the opposite action to the alphabet. Basically instead of shifting it forward by the shift key, I shift it backwards by it. It doesn't work though.

Winston Gutkowski
Bartender
Posts: 10417
63
Forrest Waldron wrote:That's the thing though. I have use a random value to create the cipher alphabet. So each time it encodes a message it is using a different cipher alphabet pretty much. Now from testing this by hand on paper, the encoding part does work. However, I can't be 100% sure until I do the opposite actions to decode the message to see if it matches the first message or not. But everytime I try to decode it, it just comes up with special characters such as %, @, and &. That's by pretty much doing the opposite action to the alphabet. Basically instead of shifting it forward by the shift key, I shift it backwards by it. It doesn't work though.

I think you're getting bogged down in the "random" part of your problem, and also in other issues that probably have nothing to do with it.

Correct me if I'm wrong, but I assume that the idea of your encryption is that, given some random number, it should be able to encrypt your "contents". Then, given that same number, your "decrypter" should be able to decrypt your encrypted data.

Another question: Is the "data" that you want to want to encrypt text or binary data? Because they are NOT the same - at least certainly not for Java - and Readers and Writers may be involved in getting it to work properly.

Winston

Winston Gutkowski
Bartender
Posts: 10417
63
John Willows wrote:Now from testing this by hand on paper, the encoding part does work. However, I can't be 100% sure until I do the opposite actions to decode the message to see if it matches the first message or not.

Precisely. You can't know you can decode it. ALWAYS. And that may have something to do with the way you encode it.

You have run into one of the rare cases of a complex function the has an inverse.
Simply put: data → encode() → decode() must return data. ALWAYS.

Therefore, decode() must be the inverse of encode() in ALL cases; and the way that Java deals with text is just one of the reasons why it might not be so.

My advice: Deal with one thing at a time. Get your encoder working ; and test it thoroughly. The chances are that, by the time you can prove it works in ALL cases, you will already have written 98% of your decoder.

Winston

John Willows
Greenhorn
Posts: 3
Ok I'll test my encoder some more. I think I understand what you mean.