How to resolve the algorithm Caesar cipher step by step in the Logo programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Caesar cipher step by step in the Logo 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 Logo programming language
Source code in the logo programming language
; some useful constants
make "lower_a ascii "a
make "lower_z ascii "z
make "upper_a ascii "A
make "upper_z ascii "Z
; encipher a single character
to encipher_char :char :key
local "code make "code ascii :char
local "base make "base 0
ifelse [and (:code >= :lower_a) (:code <= :lower_z)] [make "base :lower_a] [
if [and (:code >= :upper_a) (:code <= :upper_z)] [make "base :upper_a] ]
ifelse [:base > 0] [
output char (:base + (modulo ( :code - :base + :key ) 26 ))
] [
output :char
]
end
; encipher a whole string
to caesar_cipher :string :key
output map [encipher_char ? :key] :string
end
; Demo
make "plaintext "|The five boxing wizards jump quickly|
make "key 3
make "ciphertext caesar_cipher :plaintext :key
make "recovered caesar_cipher :ciphertext -:key
print sentence "| Original:| :plaintext
print sentence "|Encrypted:| :ciphertext
print sentence "|Recovered:| :recovered
bye
You may also check:How to resolve the algorithm Polymorphism step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Raku programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the Nim programming language