• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cezar cipher problem

 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mathew Mintalm
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic