Users Online

· Guests Online: 90

· 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 One Time Pad Algorithm

C++ Program to Implement the One Time Pad Algorithm

 

 

This C++ program encodes any message using the technique of one time pad cipher technique. Input is not case sensitive and works only for all characters. White spaces are not ignored but are produced as random characters in the decoded message.
Note:Since the key is required for decryption, it is printed on stdout. However, it is not safe to make the key public.

 

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

  1. /*
  2.  * C++ Program to Implement the One Time Pad Algorithm.
  3.  */
  4. #include<iostream>
  5. #include<vector>
  6. #include<stdlib.h>
  7. using namespace std;
  8. void to_upper_case(vector<char>& text, int len)
  9. {
  10.     for (int i = 0; i < len; i++)
  11.     {
  12.         if (text[i] >= 97 && text[i] <= 122)
  13.             text[i] -= 32;
  14.     }
  15. }
  16. void print_string(vector<char> text, int len)
  17. {
  18.     for (int i = 0; i < len; i++)
  19.     {
  20.         cout << (char) (text[i] + 65);
  21.     }
  22.     cout << endl;
  23.     return;
  24. }
  25. size_t get_input(vector<char>& msg)
  26. {
  27.     char a;
  28.     while (1)
  29.     {
  30.         a = getchar();
  31.         if (a == '\n')
  32.             break;
  33.         msg.push_back(a);
  34.     }
  35.     return msg.size();
  36. }
  37. int main()
  38. {
  39.     vector<char> msg;
  40.     vector<char> enc_msg;
  41.     vector<char> dec_msg;
  42.     int *p;
  43.     int i;
  44.     size_t len;
  45.     cout << "Enter Message to Encrypt:";
  46.     len = get_input(msg);
  47.     to_upper_case(msg, len);
  48.     p = (int*) malloc(msg.size() * sizeof(int));
  49.     for (i = 0; i < len; i++)
  50.     {
  51.         p[i] = rand() % 26;
  52.         if (msg[i] >= 65 && msg[i] <= 90)
  53.             enc_msg.push_back((char) ((msg[i] - 65 + p[i]) % 26));
  54.         else if (msg[i] >= 97 && msg[i] <= 122)
  55.             enc_msg.push_back((char) ((msg[i] - 97 + p[i]) % 26));
  56.         else
  57.             enc_msg.push_back((char) msg[i]);
  58.     }
  59.     cout << "\nEncoded Message:";
  60.     print_string(enc_msg, len);
  61.     cout << "\nKey for decryption:\n";
  62.     for (i = 0; i < len; i++)
  63.     {
  64.         cout << (char) (p[i] + 65);
  65.     }
  66.     cout << endl;
  67.     cout << "\nDecrypted Message:";
  68.     for (i = 0; i < len; i++)
  69.     {
  70.         if ((enc_msg[i] - p[i]) < 0)
  71.             dec_msg.push_back((char) (enc_msg[i] - p[i] + 26));
  72.         else if ((enc_msg[i] - p[i]) >= 0)
  73.             dec_msg.push_back((char) (enc_msg[i] - p[i]));
  74.         else
  75.             dec_msg.push_back((char) enc_msg[i]);
  76.     }
  77.     print_string(dec_msg, len);
  78.     return 0;
  79. }

Output:

$ g++ OneTimePad.cpp
$ a.out
 
Enter Message to Encrypt: This is the demonstration of OTP algorithm
Encoded Message:IOYYaCEaTFPaOJPLSAKTVLKLTaPBaTGFaUICTENHGH
 
Key for decryption:
PHQGHUMEAYLNLFDXFIRCVSCXGGBWKFNQDUXWFNFOZV
 
Decrypted Message:THISZIS]THETDEMONSTRATION[OFWOTP^ALGORITHM
 
------------------
(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: 1.12 seconds
10,831,000 unique visits