Due to writing limit, I have posted the code here: http://pastebin.com/cSGcxXGp Hi guys, I'm trying to go trace some code to see that I understand what's happening as we are asked to trace code in the exam. I was looking through this encrypting program that apparently used cesars' cipher but I can only understand how it works to where my last comment in the program is i.e "char c = s.charAt(i)". Any feedback would be appreciated.
I'm not too familiar with Java, but I'll try my best to help. Is the code working 100% correctly?
Not exactly like it should. It needs to shift 3 letters. For e.g, meet should be encrypted as phhw but it only shifts once so it is encrypted as ldds.
But it does compile and run. Sorry if it seems confusing :/
I'll just run through what it's currently doing: after char c = s.charAt(i); (which I assume gets the character at index i of the input string): 3 things happen within the loop: 1. The character's ASCII character code is stored as charCode: (int charCode = (int) c;)
2. after that, the charactercode is decremented once, corresponding to a shift of one letter by the cipher.
3. It appends that encrypted character to the output string
Ohh ok I'm pretty sure I get it now. I'll see if I can mess around and get it shifting by 3 :) Thanks so much!!
Yeah, but be sure to test it against the entire alphabet, both upper and lowercase.
because if you shift A (65 in ascii) 1 unit down, it becomes @ (64 in ascii), which is not intended :(
Will do. I'll use the asciitable site to see if I can work it out then. Thanks again :)
I've found a decent example of a Caeser Cipher implemented in Java: http://rosettacode.org/wiki/Caesar_cipher#Java
Caesar* :-P
Lol I knew there was an "ae" somewhere :P I'll look at both codes and see if I can understand them.
for (int i=0; i < s.length(); i++) { char c = s.charAt(i); // This returns the character at the specified index in a string int charCode = (int) c; charCode = charCode + 3; // This converts a string to HTML Unicode if ( c == ' ' ) { charCode = 32; } else if ( c == 'x' ) { charCode = 65; } else if (c == 'y' ) { charCode = 66; } else if ( c == 'z' ) { charCode = 67; } output = output + (char)charCode; } System.out.println("The encrypted message is: " + output); // This prints out the encrypted output } } I tested the program with the quick brown fox jumps over the lazy dog. But x y z return symbols instead of a b c. I looked up the ascii code for a b c and used them in the if statements but the program seems to only execute the first if statement?
Just to make it easier, 32 is a space 65 is a 66 is b and 67 is c
charCode = charCode + 3 how does this convert it to utf-8?
what is the whole if-elif block trying to accomplish?
you need to shift only by 1 letter?
Well, if I input THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG I get an output of WKH TXLFN EURZQ IR[ MXPSV RYHU WKH OD]\ GRJ As you can see, where x y and z should be replaced with a b and c, it is instead replaced with [ ] / The first if statement works, so that if there is a space between a word, it outputs a space instead of # which is what was in place of spaces before I put the if statement in.
I need to shift by 3 letters.
try using the modulo operator somewhere in your code, as it was done in those links I've shown you.
charCode = (charCode + 3) % (int)('a' + 26)
you should use modulo 26 because there are only 26 letters and if Z+3 it wont be C it wil be ] we also just studied this encryption :D
but you should also remember that you may have capital and non capital letter hElLo woRLd so you need seperate conversion for upper case and lower case
Or you could simply normalize the characters in the input string (all to uppercase) and then work on those without having to worry about lowercase characters.
and delete non-alphabetic characters
Actually, I think I got it. The code worked if I used all lowercase letters in the input. It was only once I entered all uppercase letters that
I got the symbols so now I just did this if you're interested: if ( c == ' ' ) { charCode = 32; } else if ( c == 'x' ) { charCode = 97; } else if (c == 'y' ) { charCode = 98; } else if ( c == 'z' ) { charCode = 99; } else if ( c == 'X' ) { charCode = 65; } else if (c == 'Y' ) { charCode = 66; } else if ( c == 'Z' ) { charCode = 67; } And it works fine now. But thanks to you both for all your help! :)
it works but it's actually crappy, if in exam they will ask you to write this you won't be able because you won't know ASCII numbers
Good point. I'm in college tomorrow so I'll ask my lab tutor if there is an alternative way that I can understand.
There's actually no need to use the raw numbers, you can just type in the actual characters enclosed in single quotes and then cast them to int
like for that last one: else if ( c == 'Z' ) { charCode = (int) 'C'; }
but as Tomas.A said, there's a smarter, easier way to implement a Caesar Cipher than this :-P
you should use modular division then you can write encoder which can be shifter by any number
right. study that implementation in the http://rosettacode.org/wiki/Caesar_cipher#Java and you might find ideas.
Wow that's pretty handy. Thanks agdgdgdgwngo. And Tomas, I'll look at the link agdgdgdgwngo gave me to see if I can work it out. Thanks again guys, I really appreciate it.
Join our real-time social learning platform and learn together with your friends!