$ 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
Users Online
· Guests Online: 121
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
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.
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.
-
/*
-
* C++ Program to Implement the One Time Pad Algorithm.
-
*/
-
#include<iostream>
-
#include<vector>
-
#include<stdlib.h>
-
using namespace std;
-
void to_upper_case(vector<char>& text, int len)
-
{
-
for (int i = 0; i < len; i++)
-
{
-
if (text[i] >= 97 && text[i] <= 122)
-
text[i] -= 32;
-
}
-
}
-
void print_string(vector<char> text, int len)
-
{
-
for (int i = 0; i < len; i++)
-
{
-
cout << (char) (text[i] + 65);
-
}
-
cout << endl;
-
return;
-
}
-
size_t get_input(vector<char>& msg)
-
{
-
char a;
-
while (1)
-
{
-
a = getchar();
-
if (a == '\n')
-
break;
-
msg.push_back(a);
-
}
-
return msg.size();
-
}
-
int main()
-
{
-
vector<char> msg;
-
vector<char> enc_msg;
-
vector<char> dec_msg;
-
int *p;
-
int i;
-
size_t len;
-
cout << "Enter Message to Encrypt:";
-
len = get_input(msg);
-
to_upper_case(msg, len);
-
p = (int*) malloc(msg.size() * sizeof(int));
-
for (i = 0; i < len; i++)
-
{
-
p[i] = rand() % 26;
-
if (msg[i] >= 65 && msg[i] <= 90)
-
enc_msg.push_back((char) ((msg[i] - 65 + p[i]) % 26));
-
else if (msg[i] >= 97 && msg[i] <= 122)
-
enc_msg.push_back((char) ((msg[i] - 97 + p[i]) % 26));
-
else
-
enc_msg.push_back((char) msg[i]);
-
}
-
cout << "\nEncoded Message:";
-
print_string(enc_msg, len);
-
cout << "\nKey for decryption:\n";
-
for (i = 0; i < len; i++)
-
{
-
cout << (char) (p[i] + 65);
-
}
-
cout << endl;
-
cout << "\nDecrypted Message:";
-
for (i = 0; i < len; i++)
-
{
-
if ((enc_msg[i] - p[i]) < 0)
-
dec_msg.push_back((char) (enc_msg[i] - p[i] + 26));
-
else if ((enc_msg[i] - p[i]) >= 0)
-
dec_msg.push_back((char) (enc_msg[i] - p[i]));
-
else
-
dec_msg.push_back((char) enc_msg[i]);
-
}
-
print_string(dec_msg, len);
-
return 0;
-
}
Output:
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.