Users Online

· Guests Online: 110

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C++ Program to Implement the Monoalphabetic Cipher

C++ Program to Implement the Monoalphabetic Cipher

 

 

 

This is a C++ Program to implement monoalphaetic cipher. In cryptography, a substitution cipher is a method of encoding by which units of plaintext are replaced with ciphertext, according to a regular system; the “units” may be single letters (the most common), pairs of letters, triplets of letters, mixtures of the above, and so forth. The receiver deciphers the text by performing an inverse substitution.

 

Substitution ciphers can be compared with transposition ciphers. In a transposition cipher, the units of the plaintext are rearranged in a different and usually quite complex order, but the units themselves are left unchanged. By contrast, in a substitution cipher, the units of the plaintext are retained in the same sequence in the ciphertext, but the units themselves are altered.

There are a number of different types of substitution cipher. If the cipher operates on single letters, it is termed a simple substitution cipher; a cipher that operates on larger groups of letters is termed polygraphic. A monoalphabetic cipher uses fixed substitution over the entire message, whereas a polyalphabetic cipher uses a number of substitutions at different positions in the message, where a unit from the plaintext is mapped to one of several possibilities in the ciphertext and vice versa.

Here is source code of the C++ Program to Implement the Monoalphabetic Cypher. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <iostream>
  2. #include <istream>
  3. #include <ostream>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <string>
  7.  
  8. // the rot13 function
  9. std::string rot13(std::string s)
  10. {
  11.     static std::string const lcalph = "abcdefghijklmnopqrstuvwxyz", ucalph =
  12.             "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  13.  
  14.     std::string result;
  15.     std::string::size_type pos;
  16.  
  17.     result.reserve(s.length());
  18.  
  19.     for (std::string::iterator it = s.begin(); it != s.end(); ++it)
  20.     {
  21.         if ((pos = lcalph.find(*it)) != std::string::npos)
  22.             result.push_back(lcalph[(pos + 13) % 26]);
  23.         else if ((pos = ucalph.find(*it)) != std::string::npos)
  24.             result.push_back(ucalph[(pos + 13) % 26]);
  25.         else
  26.             result.push_back(*it);
  27.     }
  28.  
  29.     return result;
  30. }
  31.  
  32. // function to output the rot13 of a file on std::cout
  33. // returns false if an error occurred processing the file, true otherwise
  34. // on entry, the argument is must be open for reading
  35. int rot13_stream(std::istream& is)
  36. {
  37.     std::string line;
  38.     while (std::getline(is, line))
  39.     {
  40.         if (!(std::cout << rot13(line) << "\n"))
  41.             return false;
  42.     }
  43.     return is.eof();
  44. }
  45.  
  46. // the main program
  47. int main(int argc, char* argv[])
  48. {
  49.     if (argc == 1) // no arguments given
  50.         return rot13_stream(std::cin) ? EXIT_SUCCESS : EXIT_FAILURE;
  51.  
  52.     std::ifstream file;
  53.     for (int i = 1; i < argc; ++i)
  54.     {
  55.         file.open(argv[i], std::ios::in);
  56.         if (!file)
  57.         {
  58.             std::cerr << argv[0] << ": could not open for reading: " << argv[i]
  59.                     << "\n";
  60.             return EXIT_FAILURE;
  61.         }
  62.         if (!rot13_stream(file))
  63.         {
  64.             if (file.eof())
  65.                 // no error occurred for file, so the error must have been in output
  66.                 std::cerr << argv[0] << ": error writing to stdout\n";
  67.             else
  68.                 std::cerr << argv[0] << ": error reading from " << argv[i]
  69.                         << "\n";
  70.             return EXIT_FAILURE;
  71.         }
  72.         file.clear();
  73.         file.close();
  74.         if (!file)
  75.             std::cerr << argv[0] << ": warning: closing failed for " << argv[i]
  76.                     << "\n";
  77.     }
  78.     return EXIT_SUCCESS;
  79. }

Output:

$ g++ MonoalphabeticCipher.cpp
$ a.out
 
Dharmendra
Qunezraqen
Hingu
Uvath
Sanfoundry
Fnasbhaqel
------------------
(program exited with code: 0)
Press return to continue

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.88 seconds
10,267,537 unique visits