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

Published on 12 May 2024 09:40 PM

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

Source code in the cobol programming language

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CAESAR.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  MSG        PIC X(50)
           VALUE "The quick brown fox jumped over the lazy dog.".
       01  OFFSET     PIC 9(4) VALUE 7 USAGE BINARY.
       01  FROM-CHARS PIC X(52).
       01  TO-CHARS   PIC X(52).
       01  TABL.
           02         PIC X(26) VALUE "abcdefghijklmnopqrstuvwxyz".
           02         PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
           02         PIC X(26) VALUE "abcdefghijklmnopqrstuvwxyz".
           02         PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".

       PROCEDURE DIVISION.
       BEGIN.
           DISPLAY MSG
           PERFORM ENCRYPT
           DISPLAY MSG
           PERFORM DECRYPT
           DISPLAY MSG
           STOP RUN.

       ENCRYPT.
           MOVE TABL (1:52) TO FROM-CHARS
           MOVE TABL (1 + OFFSET:52) TO TO-CHARS
           INSPECT MSG CONVERTING FROM-CHARS TO TO-CHARS.

       DECRYPT.
           MOVE TABL (1 + OFFSET:52) TO FROM-CHARS
           MOVE TABL (1:52) TO TO-CHARS
           INSPECT MSG CONVERTING FROM-CHARS TO TO-CHARS.

       END PROGRAM CAESAR.


       >>SOURCE FORMAT IS FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. caesar-cipher.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
    FUNCTION encrypt
    FUNCTION decrypt.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  plaintext                 PIC X(50).
01  offset                    USAGE BINARY-CHAR.
01  encrypted-str             PIC X(50).

PROCEDURE DIVISION.
    DISPLAY "Enter a message to encrypt: " WITH NO ADVANCING
    ACCEPT plaintext
    DISPLAY "Enter the amount to shift by: " WITH NO ADVANCING
    ACCEPT offset
    MOVE encrypt(offset, plaintext) TO encrypted-str
    DISPLAY "Encrypted: " encrypted-str
    DISPLAY "Decrypted: " decrypt(offset, encrypted-str).

END PROGRAM caesar-cipher.

IDENTIFICATION DIVISION.
FUNCTION-ID. encrypt.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
    FUNCTION ALL INTRINSIC.

DATA DIVISION.
LOCAL-STORAGE SECTION.
01  i                         USAGE INDEX.
01  a                         USAGE BINARY-CHAR.
LINKAGE SECTION.
01  offset                    USAGE BINARY-CHAR.
01  str                       PIC X(50).
01  encrypted-str             PIC X(50).

PROCEDURE DIVISION USING offset, str RETURNING encrypted-str.
    PERFORM VARYING i FROM 1 BY 1 UNTIL i > LENGTH(str)
        IF str(i:1) IS NOT ALPHABETIC OR str(i:1) = SPACE
            MOVE str(i:1) TO encrypted-str(i:1)
            EXIT PERFORM CYCLE
        END-IF
        IF str(i:1) IS ALPHABETIC-UPPER
            MOVE ORD("A") TO a
        ELSE
            MOVE ORD("a") TO a
        END-IF
        MOVE CHAR(MOD(ORD(str(i:1)) - a + offset, 26) + a)
            TO encrypted-str(i:1)
    END-PERFORM
    EXIT FUNCTION.

END FUNCTION encrypt.

IDENTIFICATION DIVISION.
FUNCTION-ID. decrypt.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
    FUNCTION encrypt.

DATA DIVISION.
LOCAL-STORAGE SECTION.
01  decrypt-offset            USAGE BINARY-CHAR.
LINKAGE SECTION.
01  offset                    USAGE BINARY-CHAR.
01  str                       PIC X(50).
01  decrypted-str             PIC X(50).

PROCEDURE DIVISION USING offset, str RETURNING decrypted-str.
    SUBTRACT offset FROM 26 GIVING decrypt-offset
    MOVE encrypt(decrypt-offset, str) TO decrypted-str
    EXIT FUNCTION.

END FUNCTION decrypt.


  

You may also check:How to resolve the algorithm Window creation/X11 step by step in the Julia programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the OCaml programming language
You may also check:How to resolve the algorithm Align columns step by step in the EasyLang programming language
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 Ruby programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the XProfan programming language