How to resolve the algorithm Caesar cipher step by step in the Objeck programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Caesar cipher step by step in the Objeck programming language

Table of Contents

Problem Statement

Implement a Caesar cipher, both encoding and decoding. The key is an integer from 1 to 25. This cipher rotates (either towards left or right) the letters of the alphabet (A to Z). The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A). So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to "BC". This simple "mono-alphabetic substitution cipher" provides almost no security, because an attacker who has the encoded message can either use frequency analysis to guess the key, or just try all 25 keys. Caesar cipher is identical to Vigenère cipher with a key of length 1. Also, Rot-13 is identical to Caesar cipher with key 13.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Caesar cipher step by step in the Objeck programming language

Source code in the objeck programming language

class Caesar {
  function : native : Encode(enc : String, offset : Int) ~ String {
    offset := offset % 26 + 26;
    encoded := "";
    enc := enc->ToLower();
    each(i : enc) {
      c := enc->Get(i);
      if(c->IsChar()) {
        j := (c - 'a' + offset) % 26;
        encoded->Append(j + 'a');
      }
      else {
        encoded->Append(c);
      };
    };
    
    return encoded;
  }
  
  function : Decode(enc : String, offset : Int) ~ String {
    return Encode(enc, offset * -1);
  }
  
  function : Main(args : String[]) ~ Nil {
    enc := Encode("The quick brown fox Jumped over the lazy Dog", 12);
    enc->PrintLine();
    Decode(enc, 12)->PrintLine();
  }
}

  

You may also check:How to resolve the algorithm ISBN13 check digit step by step in the Ruby programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Lua programming language
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Python programming language
You may also check:How to resolve the algorithm Solve a Hidato puzzle step by step in the Mathprog programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the BBC BASIC programming language