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

Published on 12 May 2024 09:40 PM

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

Source code in the action! programming language

CHAR FUNC Shift(CHAR c BYTE code)
  CHAR base

  IF c>='a AND c<='z THEN
    base='a
  ELSEIF c>='A AND c<='Z THEN
    base='A
  ELSE
    RETURN (c)
  FI
  c==+code-base
  c==MOD 26
RETURN (c+base)

PROC Encrypt(CHAR ARRAY in,out BYTE code)
  INT i

  out(0)=in(0)
  FOR i=1 TO in(0)
  DO
    out(i)=Shift(in(i),code)
  OD
RETURN

PROC Decrypt(CHAR ARRAY in,out BYTE code)
  Encrypt(in,out,26-code)
RETURN

PROC Test(CHAR ARRAY in BYTE code)
  CHAR ARRAY enc(256),dec(256)

  PrintE("Original:") PrintE(in)
  Encrypt(in,enc,code)
  PrintF("Encrypted code=%B:%E",code) PrintE(enc)
  Decrypt(enc,dec,code)
  PrintF("Decrypted code=%B:%E",code) PrintE(dec)
  PutE()
RETURN

PROC Main()
  Test("The quick brown fox jumps over the lazy dog.",23)
  Test("The quick brown fox jumps over the lazy dog.",5)
RETURN

  

You may also check:How to resolve the algorithm War card game step by step in the Nim programming language
You may also check:How to resolve the algorithm Documentation step by step in the D programming language
You may also check:How to resolve the algorithm Chaocipher step by step in the QBasic programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Cistercian numerals step by step in the Java programming language