Thanks for visiting! - Alex

Ceaser cipher and other things

Estimated reading time: 4 mins

Today, I was browing again in the textbook and for some reason I wanted to implement a simple Ceaser cipher. In the process, I also re-found-out that Strings are immutable in java. I suppose it makes sense then when StringBuilder and the such are used. I also found the following two statements interesting:

… that characters are represented in Unicode by integer code points …

… upper letters of the Latin alphabet are consecutive (for simplicity …

and you know what? No one has ever connected these two ideas together like that or pointed it out. I’ve used these exact ideas before and never considered how fortunate that everything just works (= Why — duhA is obviously 65 and B is 66.

Anywho, I wanted to implement this shifting algorithm and this is how far we got:

import static java.lang.System.exit;

public class CeaserCypher {
    public static void main(String[] args) {
        String encryption = "DEFGHIJKLMNOPQRSTUVWXYZABC";
//        String decryption = "XYZABCDEFGHIJKLMNOPQRSTUVW";
        char[] encryptionCode = encryption.toCharArray();
//        char[] decryptionCode = decryption.toCharArray();

        String msg = "THE EAGLE IS IN PLAY; MEET AT JOE’S.";
        String result = transform(msg, encryptionCode);
        String expected = "WKH HDJOH LV LQ SODB; PHHW DW MRH’V.";

        System.out.println(transform(msg, encryptionCode));

        char[] resultChar = result.toCharArray();
        char[] expectedChar = expected.toCharArray();

        if (resultChar.length != expectedChar.length) {
            exit(0);
        }

        for (int index = 0; index < resultChar.length; index++) {
            int _resultChar = (int) resultChar[index];
            int _expectedChar = (int) expectedChar[index];

            if ((_resultChar ^ _expectedChar) != 0) {
                System.out.println("resultChar[index] : " + resultChar[index] + "\t should be : " + expectedChar[index]);
                exit(0);
            }
        }

        System.out.println("All good!");
    }

    public static String transform(String messadge, char[] key) {
        char[] message = messadge.toCharArray();

        for (int i = 0; i < message.length; i++) {
            if (Character.isUpperCase(message[i])) {
                int replaceWith = message[i] - 'A';
                message[i] = key[replaceWith];
            }
        }

        return new String(message);
    }
}

… and I wish I could take credit for this but alas. Followed along with the book and after much back and forth I ended up with pretty much exactly the same version as in the book. This is why, if you notice, there is a decoding key but I never even explored that. Was fun for a minute =)

Side note: I am not sure why, but I have a special place for character encoding. I think language construction (tables, lex, state machines, and so on…) and reading The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) made me appreciate the idea that 65 is A. I like to revisit and re-read Joel from time to time.

… oh and probably the fact that I deal with such things day-to-day in my job also keeps the interest. You know… Canada… English and French ;-)

Oh and I finally configured and properly installed jenv, java11, and maven (& got it working with the other java installations). Should be much smoother now and I want to give heroku and java a try if I have a second this weekend.

that is it for now & I still have the pages for series and sequences open.

See you tomorrow.

· software engineering, computer science, algorithms
👈 Insertion Sort
Past few days 👉


⭐️ Learn Node.js ⭐️