How to resolve the algorithm Substitution cipher step by step in the PHP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Substitution cipher step by step in the PHP programming language

Table of Contents

Problem Statement

Substitution Cipher Implementation - File Encryption/Decryption

Encrypt an input/source file by replacing every upper/lower case alphabets of the source file with another predetermined upper/lower case alphabets or symbols and save it into another output/encrypted file and then again convert that output/encrypted file into original/decrypted file. This type of Encryption/Decryption scheme is often called a Substitution Cipher.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Substitution cipher step by step in the PHP programming language

This PHP code performs a simple character substitution encryption and decryption. It works by replacing each character in the input text with its corresponding character from a predefined key.

Key Generation:

The key used for encryption is $key, which is a string of 26 lowercase and uppercase English letters. This key defines the mapping between the original characters and the encoded characters.

Encoding:

In the file_put_contents line, the content of input.txt is read and then transformed using strtr. The strtr function takes two arguments: an input string and a character mapping table. In this case, the mapping table is created by passing the $alphabet and $key strings to strtr. The result of strtr is a new string where each character in the input string has been replaced by its corresponding character in the mapping table. This encoded string is then written to output.txt.

Decoding:

In the subsequent strtr line, the encoded text in output.txt is read and transformed again using strtr. This time, the mapping table is created by reversing the order of $alphabet and $key. This reverses the character substitution that was done during encoding, effectively decrypting the text and restoring the original characters.

Demonstration:

Finally, the source, encoded, and decoded text are printed to the console. This demonstrates the encryption and decryption process.

Example Usage:

Let's say you have an input text file named input.txt with the following content:

Hello World!

Running the PHP code will generate an encoded file named output.txt with the following content:

Pulyy Ydslh!

When you run the code again to decode output.txt, you will get the original text back:

Hello World!

Source code in the php programming language

<?php

$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$key      = 'cPYJpjsBlaOEwRbVZIhQnHDWxMXiCtUToLkFrzdAGymKvgNufeSq';

// Encode input.txt, and save result in output.txt
file_put_contents('output.txt', strtr(file_get_contents('input.txt'), $alphabet, $key));

$source  = file_get_contents('input.txt');
$encoded = file_get_contents('output.txt');
$decoded = strtr($encoded, $key, $alphabet);

echo
    '== SOURCE ==', PHP_EOL,
    $source, PHP_EOL, PHP_EOL,
    '== ENCODED ==', PHP_EOL,
    $encoded, PHP_EOL, PHP_EOL,
    '== DECODED ==', PHP_EOL,
    $decoded, PHP_EOL, PHP_EOL;


  

You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the GAP programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the C# programming language
You may also check:How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the Java programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the Erlang programming language