Caesar ciphers Download

Christina Cacioppo

characters, strings, for loops Student approved


Introduction

During the Gallic Wars, Julius Caesar needed to pass messages to his commander Cicero. So he'd write a note and give it to his messenger – but sometimes, those notes were intercepted by his enemies, and his battle plans revealed.

Caesar needed to make sure his notes would stay secret, even if they fell into the wrong hands. So he started encoding them using what we now call a Caesar shift.

Caesar and Cicero would agree on a number – say, 3. Then, when Caesar wrote his notes, he'd replace each letter with the letter three places further down in the alphabet. So ABC would become DEF and CAESAR would become FDHVDU.

If the Gauls intercepted the messages, they'd see gibberish, but Cicero would know how to decode them. Pretty witty, eh?


Task

Edit the encode and decode functions to both encode (scramble) and decode (unscramble) the message that's passed using a shift size of shift.


Starter code

class Cipher {
	public static void main(String[] args) {
        String secret = "The wizard quickly jinxed the gnomes before they vaporized";
        int shift = 12;

        String encoded = Cipher.encode(secret, shift);
        System.out.println(enc);
        System.out.println(Cipher.decode(enc, shift));
    }
 
    public static String encode(String message, int shift) {
            /* Your code here	*/
    }

    public static String decode(String enc_message, int shift) {
            /* Your code here   */
    } 
}

Expected output

When you're done, calling (for example):

    String secret = "the wizard quickly jinxed the gnomes before they vaporized";
    int shift = 12;
    String encoded = Cipher.encode(secret, shift);
    System.out.println(encoded);
    System.out.println(Cipher.decode(encoded, shift));

should generate:

ftq iulmdp cguowxk vuzjqp ftq szayqe nqradq ftqk hmbadulqp
the wizard quickly jinxed the gnomes before they vaporized

Hints



Useful tools to solve the problem


Related links