File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Cezar cipher problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cezar cipher problem" Watch "Cezar cipher problem" New topic

Cezar cipher problem

Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Hello I have been forced by my maths teacher to create website for competition about promoting mathematics.

I chose mathematics in IT/programming. I'm presenting some of my own experience, especially in android game development.

But I wanted to include something easier as well, and created section about cryptology, and wanted to create Java applet with Cezar cipher

And I have problem, encryption works as it should, but decryption doesn't.

Thats how I understand it:

(sorry for polish description in gif above)

1. type the alphabet

2. Move every character in right direction (based on key, in this case its 3)

3. Put last 3 chars at begining.


I'm using those formulas to encrypt/decrypt

n - number of latter we want to encrypt/decrypt

k - number of movement (in our case 3)

x - number of chars in our alphabet (26 in our case)


So I created following code, but as I mentioned it seems to work only in one way (encrypt for example key 3, but decryption gives wrong values for example for key -3)

I will be glad if someone might help me correct it, thanks!
Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1545

Hi Mathew Mintalm,

Mathew Mintalm wrote:wanted to create Java applet with Cezar cipher

It is Caesar cypher (not Cezar cypher)

Mathew Mintalm wrote:encryption works as it should, but decryption doesn't.

Well, why should decryption work? You've wrote a method encryptDecrypt which only encrypts a character array. It won't decrypt it. So, no matter whatever input you give to it (encrypted, non-encrypted, double encrypted or whatever), it will further encrypt it.

Please write one method for encryption and another for decryption (or at least keep some way - say a boolean parameter - to understand the method whether it should do encryption or decryption - just in case you want to do it with only one method. you'll have to write decryption code anyway).

So, please write code for decryption, and let us know if you face any issues.

I hope this helps.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Thanks for reply, so here's code to encrypt:

and decrypt

but still I can't make decrypt method to work properly.

About your previous reply, I did it in one method, because I thought its depends on key, if is >0 its encryption and while <0 decryption.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
Not happy about that: you have got a side-effect that you are changing the chars array. I suggest you use the clone() method to take a copy of that, as a local variable. Note clone() on an array gives you a “shallow” clone, so it is more difficult to use for a char[][]. This version maintains the original array unchanged.
Also: don’t mix tabs and spaces for indenting: you are best off using spaces only. Don’t routinely double-space lines in code; they are not easier to read.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
You don’t need to get the char out of an array. If you have index, such that a = 0 and z = 25, you can simply writeThat will of course give you capital letters only, and will not work for spaces or numbers of punctuation.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Mathew Mintalm wrote:Thanks for reply, so here's code to encrypt:

and decrypt

That doesn't look right to me. There's a significant difference which I don't understand, namely "key + 1" to encrypt but "key" to decrypt. Could you explain that?
I agree. Here's the link:
subject: Cezar cipher problem
It's not a secret anymore!