Wednesday, October 04, 2017

Monoalphabetic Substitution Cipher in C++

#include <iostream>
#include <string>
#include <ctime>
#include <chrono>
#include <thread>

int main()
{

  char plain[100] = "devharsh";
  char cipher[100];
 
  char alphabets[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  char mappings[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
 
  alphabets[26] = '\0';
  mappings[26] = '\0';
 
  std::cout << alphabets << "\n";
  std::cout << mappings << "\n";
 
  for (int i=0; i<26; i++)
  { 
    srand((int)time(0));
    std::this_thread::sleep_for(std::chrono::milliseconds(300));
    int irand = rand() % 26;
    char temp = mappings[i];
    mappings[i] = mappings[irand];
    mappings[irand] = temp;
  }
 
  std::cout << alphabets << "\n";
  std::cout << mappings << "\n";
 
  for (int i=0; i<sizeof(plain) / sizeof(char); i++)
  {
      for (int j=0; j<26; j++)
      {
          if (plain[i] == alphabets[j])
          {
              cipher[i] = mappings[j];
          }
      }
  }
 
  std::cout << plain << "\n";
  std::cout << cipher << "\n";

}