• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple logic error in cipher-shifting program?  RSS feed

 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! Thanks for clicking on this post. I could use a fresh pair of eyes.
I was tasked with creating a program that encrypts a line of text (for example, CANDY) by shifting the letters X amount of times. For example, if the user inputs the sentence CANDY and selects a shift of 5, the output would be:
HFSID
I got this part working fine. The issue I am having is with the decryption part of the program. This is simply the reverse of the above, as the user would enter the phrase HFSID, with a shift of 5, and the program would output:
CANDY
It works fine, all except for one letter, being the "F" letter. With my code, when I enter the above word to be decrypted it outputs:
C[NDY
Obviously, that [ bracket is not an 'A'. I realise the issue falls with the equation, but upon messing around with the program for about four hours, it is all starting to blur together. I just need a point in the right direction...
Here is my code:


Any help is appreciated!
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 34: I think you meant 26 not 25.
Line 35: Less than zero.
Line 36: rethink this formula.
I would also correct for negative values before doing the modulus.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why isn't your decryption a simple reversal of your encryption?

You can reverse the modulus by simply adding 26 once and then applying the modulus.

( 5 + 26) % 26 = 5
(-2 + 26) % 26 = 24

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!