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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Caesar cipher step by step in the Seed7 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 Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

const func string: rot (in string: stri, in integer: encodingKey) is func
  result
    var string: encodedStri is "";
  local
    var char: ch is ' ';
    var integer: index is 0;
  begin
    encodedStri := stri;
    for ch key index range stri do
      if ch >= 'a' and ch <= 'z' then
        ch := chr((ord(ch) - ord('a') + encodingKey) rem 26 + ord('a'));
      elsif ch >= 'A' and ch <= 'Z' then
        ch := chr((ord(ch) - ord('A') + encodingKey) rem 26 + ord('A'));
      end if;
      encodedStri @:= [index] ch;
    end for;
  end func;

const proc: main is func
  local
    const integer: exampleKey is 3;
    const string: testText is "The five boxing wizards jump quickly";
  begin
    writeln("Original:  " <& testText);
    writeln("Encrypted: " <& rot(testText, exampleKey));
    writeln("Decrypted: " <& rot(rot(testText, exampleKey), 26 - exampleKey));
  end func;

  

You may also check:How to resolve the algorithm Execute a system command step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Wart programming language
You may also check:How to resolve the algorithm Sub-unit squares step by step in the Nim programming language
You may also check:How to resolve the algorithm Untouchable numbers step by step in the F# programming language
You may also check:How to resolve the algorithm Play recorded sounds step by step in the Phix programming language