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 Cipher and Decipher: Out of Bounds Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cipher and Decipher: Out of Bounds Exception" Watch "Cipher and Decipher: Out of Bounds Exception" New topic
Author

Cipher and Decipher: Out of Bounds Exception

Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
Okay I am currently stuck and have not been able to get past this assignment in my Computer Science Class. My program has to create a randomly generated alphabet then cipher a message. Both the alphabet and message are then printed to a text file. Part 2 of it has reading the random alphabet and decoding the message. My encryption class works fine but the decryption class is the killer. If you could help it would be greatly appreciated. The project folder and assignment instructions are attached.





Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Hi Christopher,

Welcome to CodeRanch!

I could not find the attached project folder and assignment instructions.

Besides, at CodeRanch, we strongly encourage users to learn on their own, instead of providing ready-made solutions. Needless to say, you can ask/discuss any doubt you are facing during your assignments and we'll help you in finding the solution (instead of giving you the solution )

Also, please be specific about the problem. You've only provided a small mention of problem (in subject line) - you are facing out of bounds exception. Can you please be more specific about it (e.g. providing call stack etc)?

From initial look, I can see that in your ChrisGoadDecipher class, you are looping over cipherAlphabet for 26 times. What if cipherAlphabet's size is less than 26?

I hope this will help.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
I'm sorry I implied I wanted the answers. That is not what I meant.
Anyway, I know the problem is in my decryption class. The error always occurs when reading back the cipher alphabet from the txt file. Could you compile my code and take a look for yourself?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Christopher Goad wrote:Anyway, I know the problem is in my decryption class. The error always occurs when reading back the cipher alphabet from the txt file.

Actually, I suspect that's not the case. The problem looks to be in your createNewAlphabet() method, which I'm still not quite sure what it's doing, but it doesn't appear to use a Caesar shift.

Assuming that you simply want to use a random combo of the original letters to encrypt a message (and if you want your encryption to allow blanks, it should probably have 27, not 26 - and 53 if you want to allow mixed case), consider using Collections.shuffle(); and if you want your decrypt() function to work nice and fast, I'd suggest that you put the result of that shuffle into a HashMap (specifically, a HashMap<Character, Integer>).

If you truly want to use a Caesar shift, then your new "alphabet" MUST be a rotation of the original - so have a look at the 'mod' (%) operator.

HIH

Winston

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

Joined: Mar 25, 2013
Posts: 27
That's what I meant. Anyway in my createNewAlphabet() method i am trying to read to read the random alpabet i made in the txt file and then assign it to the cipherAlphabet array.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Christopher Goad wrote:That's what I meant.

Erm...what's what you meant?

Anyway in my createNewAlphabet() method i am trying to read to read the random alpabet i made in the txt file and then assign it to the cipherAlphabet array.

So, do you:
(a) Know whether you read it back in correctly.
(b) Know whether your method is translating it back correctly?
And the easiest way to track that is with debug statements (or, indeed, a debugger)...

The fact is that, to decrypt, the ONLY thing you should need is your jumbled "alphabet".

Winston
Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
I meant the problem was in the ChrisGoadDecipher class but more specificly in the createNewAlphabet() method. I do not. Could you help me figure the problem out because I am not sure what is going on.
Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
I figured out the problem in my createnewalphabet() method. I didnt have it changed to a char.

Now I have one last problem. I need to reverse the cipher in my decrypt method. How could this be done?

Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
Ahah, I got it. All I had to do was flip the cipherAlphabet and Alphabet!

It works perfect now!



One last problem.

Whenever I enter yes to tryAgain it does nothing


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Christopher Goad wrote:One last problem.
Whenever I enter yes to tryAgain it does nothing

You know what? Compared to what you've just solved that's a fart in a storm; so I'm going to leave it up to you.

Two tips (and I say this without even having looked at your code):
1. AvoidTheEqualityOperator (←click).
2. Add debug code, and TEST.

There are some things you just can't get around.

Winston
Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
Okay, I understand. Can't believe I got that by myself(With some small hints). Thanks for the help.

I'll be back lol
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Christopher Goad wrote:Okay, I understand. Can't believe I got that by myself(With some small hints). Thanks for the help.

Glad to help - and isn't it much better when you DO get it yourself?

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cipher and Decipher: Out of Bounds Exception
 
Similar Threads
Need some help with output to file
i/o about printwriter
Problem with char/string representation
Help With Alphabet Cipher
Counting one character at a time from a file