Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help With Alphabet Cipher

 
John Willows
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

This reply is based solely on this part of your post.

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).

Based on the above answer:
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

But basically, we need more information. And I don't mean a mind-dump of your source; I mean sufficient of it to allow us to help you out.

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I'll test my encoder some more. I think I understand what you mean.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic