This code implements a simple two-stage encryption system using two...

August 25, 2025 at 09:47 AM

#include <iostream> #include <string> #include <vector> const std::vector<std::vector<std::string>> table1 = { {"AB", "DC", "CC", "CB"}, {"CA", "DA", "CD", "DD"}, {"BC", "AA", "BA", "DB"}, {"BD", "AD", "BB", "AC"}, }; const std::vector<std::vector<std::string>> table2 = { {"CD", "BB", "AC", "CC"}, {"CB", "DB", "AD", "DD"}, {"DA", "DC", "BC", "BD"}, {"AA", "BA", "CA", "AB"} }; std::string encrpyt1(std::vector<std::vector<int>> before){ std::string output; for(int i = 0; i<=before.size()-1; i++){ output.append(table1[before[i][0]][before[i][1]]); } return output; } std::string encrpyt2(std::vector<std::vector<int>> before){ std::string output; for(int i = 0; i<=before.size()-1; i++){ output.append(table2[before[i][0]][before[i][1]]); } return output; } int charToCoord(std::string letter){ if(letter == "A"){ return 0; }else if(letter == "B"){ return 1; }else if(letter == "C"){ return 2; }else{ return 3; } } std::vector<std::vector<int>> turnToCoords(const std::vector<std::vector<std::string>> &blocks){ std::vector<std::vector<int>> output = {}; for(int i = 0; i <= blocks.size()-1; i++){ std::vector<int> currentBlock = {}; currentBlock.push_back(charToCoord(blocks[i][0])); currentBlock.push_back(charToCoord(blocks[i][1])); output.push_back(currentBlock); } return output; } std::vector<std::vector<std::string>> turnToBlocks(const std::string &userInput){ int stringSize = userInput.length() -1; std::vector<std::vector<std::string>> output = {}; for(int i = 0; i <= stringSize;){ std::vector<std::string> currentBlock; currentBlock.push_back(std::string(1, userInput[i])); i++; currentBlock.push_back(std::string(1, userInput[i])); output.push_back(currentBlock); i++; } return output; } int main(){ std::string userInput; std::getline(std::cin, userInput); std::vector<std::vector<std::string>> blocks = turnToBlocks(userInput); std::vector<std::vector<int>> coordBlocks = turnToCoords(blocks); std::string first = encrpyt1(coordBlocks); blocks = turnToBlocks(first); coordBlocks = turnToCoords(blocks); std::string final = encrpyt2(coordBlocks); std::cout << final; }

This code implements a simple two-stage encryption system using two static lookup tables (table1 and table2) to map pairs of characters to new pairs of characters. Here's what the different parts of the code do:


Key Features of the Code

  1. Inputs and Parsing:

    • The program reads a std::string input from the user during runtime via std::getline(std::cin, userInput).
    • It assumes the input is a sequence of uppercase letters (e.g., "AABBCCDD") composed of pairs of characters, with a total even number of characters.
  2. Breaking Input into Blocks of Pairs:

    • The function turnToBlocks splits the input string into a vector of string pairs. For example:
      • Input: "AABBCC"
      • Output: {{"A", "A"}, {"B", "B"}, {"C", "C"}}
  3. Mapping Characters to Coordinates:

    • The function turnToCoords transforms each letter pair (e.g., {"A", "B"}) into a coordinate pair of integers based on the following mapping:
      • "A" -> 0
      • "B" -> 1
      • "C" -> 2
      • "D" -> 3
      • Example:
        • Input: {{"A", "B"}, {"B", "B"}}
        • Output: {{0, 1}, {1, 1}}
  4. Using the Lookup Tables:

    • The encrpyt1 and encrpyt2 functions encrypt the pairs of coordinates (std::vector<std::vector<int>>) by mapping them to the values in their respective tables (table1 and table2):
      • In table1 or table2, the first coordinate (row) is used to index the row, and the second coordinate (column) is used to index the column.
      • The resulting value is a string from the table.
        • Example:
          • Input: {{0, 1}}
          • From table1: Row 0, Column 1"DC"
  5. Two-Stage Encryption Process:

    • First, the input pairs are encrypted using table1 (encrpyt1).
    • The resulting encrypted pairs are converted into blocks and coordinates again, and then passed through table2 for the second encryption step (encrpyt2).
  6. Final Output:

    • After the second encryption, the resulting string is printed as the output of the program.

Step-by-Step Example Execution

Let's break down what happens for an input string, say "AABB":

  1. Input Parsing:

    • Input: "AABB"
    • Parsed into string blocks: {{"A", "A"}, {"B", "B"}}
  2. Convert to Coordinates:

    • Blocks: {{"A", "A"}, {"B", "B"}}
    • Coordinates: {{0, 0}, {1, 1}}
  3. First Encryption (encrpyt1 with table1):

    • Coordinates: {{0, 0}, {1, 1}}
    • table1[0][0] = "AB"
    • table1[1][1] = "DA"
    • Result after first encryption: "ABDA"
  4. Convert to Blocks and Coordinates (Intermediate String):

    • New Input: "ABDA"
    • Blocks: {{"A", "B"}, {"D", "A"}}
    • Coordinates: {{0, 1}, {3, 0}}
  5. Second Encryption (encrpyt2 with table2):

    • Coordinates: {{0, 1}, {3, 0}}
    • table2[0][1] = "BB"
    • table2[3][0] = "AA"
    • Result after second encryption: "BBAA"
  6. Output:

    • Final encrypted string: "BBAA"

What the Code Does (In Summary)

This program:

  1. Takes a user-provided string as input.
  2. Interprets it as pairs of characters, processes them into coordinates, and encrypts the data using two static substitution tables (table1, table2).
  3. Outputs the final encrypted string after two stages of encryption.

It is effectively a simple substitution cipher that involves two rounds of transformations based on predefined lookup tables.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node