So a few months ago, I had an old homework assignment for school in which I had to make a Caesar Cypher encrypting machine. I already did it, and I would like to see some of the solutions some of you come up with for this. To make it fair to you guys, here are all of the original instructions that I was given for this assignment:
MINI LESSON ON CHARACTERS
Every character has an associated number with it, according to the table of ASCII values. Capital letter 'A'
corresponds to 65
, 'B'
to 66
, … , 'Z'
to 90
.
That means you can perform mathematical operations on characters, and you get a numeric result. To get the result as a character, you just cast it to a char after doing your operation.
Example :
char newLetter = (char)(‘A’ + 2) => newLetter holds the char ‘C’
But there’s a catch .
If you add 2 to Z, you want it loop back to B. But, 92 (i.e. 90 + 2) is not the ASCII value for Y, it’s the ASCII value for ‘\’.
There’s a solution for this:
Before doing your mathematical operation on your character, subtract 65 from the character . Now ‘A’ will correspond to 0, ‘B’ to 1, etc.
Do your desired math operations, and then mod the result by 26 to make sure your answer is between 0 and 25.
Add 65 to get the character back to its numeric ASCII value, and cast it to a char .
THE LAB
A Caesar cipher, named after Julius Caesar, is a simple algorithm for encrypting secret messages. It is a type of substitution cipher in which each letter is the message is replaced by a letter some fixed number of positions down the alphabet.
Example:
If using a left shift of 3, D would be replaced by A, E would become B, and so on.
If using a right shift of 2, D would be replaced by F, E would become G, and so on.
Write a method named encode
that takes in a String representing secret message (without spaces) and an int representing how many positions to shift the letters by to encode, and returns the encoded version of the message. You can assume it will always be a right shift.
In the main
method, ask the user for a secret message to encode, and then print out their encoded message using the encode
method.
Examples:
encode("CAT", 2)
returns ECV
encode("MEETMEATTHREE", 5)
returns RJJYRJFYYMWJJ
encode("ZOOM", 3)
returns CRRP
``
Lastly, here’s the sample input and output:
Output = Enter your secret message and an amount to shift by:
Input = MEETMEATTHREE 5
Output = Encoded message: RJJYRJFYYMWJJ
Here’s the original answer for this problem:
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner sneaky = new Scanner(System.in);
System.out.println("Enter your secret message and an amount to shift by:");
String definitelyNotASecretMessage = sneaky.next();
int shifting = sneaky.nextInt();
String hacks = encode(definitelyNotASecretMessage, shifting);
System.out.println("Encoded message: " + hacks);
}
public static String encode(String spy, int times) {
String hackerSteve = spy;
for (int i = times; i > 0; i--) {
String hackerWalter = "";
for (int h = 0; h < hackerSteve.length(); h++ ) {
char iSpilledWaterOnMyLaptop = (char)(((((hackerSteve.charAt(h)) - 65) + 1) % 26) + 65);
hackerWalter += "" + iSpilledWaterOnMyLaptop;
}
hackerSteve = hackerWalter;
}
return hackerSteve;
}
}
That was the correct code needed to get the assignment right, but before I ended up here, I got a little creative and ended up with this alternate solution:
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner sneaky = new Scanner(System.in);
System.out.println("Enter your secret message and an amount to shift by:");
String definitelyNotASecretMessage = sneaky.next();
int shifting = sneaky.nextInt();
String hacks = encode(definitelyNotASecretMessage, shifting);
System.out.println("Encoded message: " + hacks);
}
public static String encode(String spy, int times) {
String hackerSteve = spy;
String alphabet = "?abcdefghijklmnopqrstuvwxyzab";
alphabet = alphabet.toUpperCase();
for (int i = times; i > 0; i--) {
String hackerJake = "";
for (int h = 0; h < hackerSteve.length(); h++) {
hackerJake += "" + alphabet.charAt((alphabet.indexOf(hackerSteve.charAt(h))) + 1);
}
hackerSteve = hackerJake;
}
return hackerSteve;
}
}
// Steve is a nimrod, Jake is a coffee addict
I’m interested to see what people think of my alternate solution. and/or what other ways there are to end up with the right solution.
Good luck on this, and I’m excited to see what you come up with!