File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help With Alphabet Cipher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help With Alphabet Cipher" Watch "Help With Alphabet Cipher" New topic
Author

Help With Alphabet Cipher

John Willows
Greenhorn

Joined: Feb 25, 2013
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

Joined: Mar 17, 2011
Posts: 7545
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
John Willows
Greenhorn

Joined: Feb 25, 2013
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

Joined: Mar 17, 2011
Posts: 7545
    
  18

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

Joined: Mar 17, 2011
Posts: 7545
    
  18

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

Joined: Feb 25, 2013
Posts: 3
Ok I'll test my encoder some more. I think I understand what you mean.
 
jQuery in Action, 2nd edition
 
subject: Help With Alphabet Cipher
 
Similar Threads
Unusual behavior of Simple Cipher Program
Java array help
Crypto Challenge Instructions
Need some help with output to file
Getting an array out of bounds error