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

Published on 12 May 2024 09:40 PM

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

Source code in the erre programming language

PROGRAM CAESAR

!$INCLUDE="PC.LIB"

PROCEDURE CAESAR(TEXT$,KY%->CY$)
    LOCAL I%,C%
    FOR I%=1 TO LEN(TEXT$) DO
      C%=ASC(MID$(TEXT$,I%))
      IF (C% AND $1F)>=1 AND (C% AND $1F)<=26 THEN
        C%=(C% AND $E0) OR (((C% AND $1F)+KY%-1) MOD 26+1)
        CHANGE(TEXT$,I%,CHR$(C%)->TEXT$)
      END IF
    END FOR
    CY$=TEXT$
END PROCEDURE

BEGIN
    RANDOMIZE(TIMER)
    PLAINTEXT$="Pack my box with five dozen liquor jugs"
    PRINT(PLAINTEXT$)

    KY%=1+INT(25*RND(1))  ! generates random between 1 and 25
    CAESAR(PLAINTEXT$,KY%->CYPHERTEXT$)
    PRINT(CYPHERTEXT$)

    CAESAR(CYPHERTEXT$,26-KY%->DECYPHERED$)
    PRINT(DECYPHERED$)

END PROGRAM

  

You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the zkl programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Go programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Elena programming language
You may also check:How to resolve the algorithm String prepend step by step in the jq programming language