Write a cryptographic program that works similarly to my crypto program:• Prompt the user to enter a secret key and use it to compute the seed of a random number generator • Prompt the user to give the names of an input file and the output coded/decoded file• Create the sequence of random bytes from a random number generator with the seed obtained from user’s secret key. • Perform the coding of bit x using a random bit r as follows: x⊕ = r • Since ∀r ∈{0,1}, r ⊕r = 0, perform decoding using the same sequence of random bytes and the same operation x⊕ = r . Decoding is based on the operation x⊕r ⊕r = x⊕(r ⊕r) = x⊕0 = x
Below is the code that works for encryption but when it come to decryption, the cipher text contains only 3 characters. I have not been able to figure why the decoding does not work. I am using Dev-C++. Your help will be very appreciated.
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
int main()
{
char n, line[1024], keystring[100]; char FitoEncr[100], NewFiCrypt[100];
char FiletobeDecrypted[100];
ifstream IS ("FitoEncr.txt", ios::in);
ofstream OS ("NewFiCrypt.txt", ios::out);
unsigned int psswd=0, number;
cout<<"Please, enter a secret key :";
cin.getline(keystring, 100);
for( int i=0;keystring[i]!='\0'; i++)
psswd=(psswd+3)*keystring[i];
cout<<"initial password: "<<keystring<<endl;
cout<<"encrypted password: "<<psswd<<endl;
cout<<"please, enter the name of the input file: ";
cin.getline(FitoEncr,20);
cout<<"please, enter the name of the output file: ";
cin.getline(NewFiCrypt,20);
srand(psswd); //not sure about creating the sequence of random bytes from a...
number=rand() % 255; //random number generator with the seed obtained from user's secret key
//ENCRYPTION
while(IS.get(n))
{
if(IS.get(n))
{
n^=rand();
OS<<n;
cout<<"character coded:"<<n;
}
}
IS.close();
OS.close();
//DECRYPTION
ifstream IS1;
ofstream OS1;
IS1.open("NewFiCrypt.txt", ios::in);
OS1.open("FilDecr.txt", ios::out);
while(IS1.get(n))
{
if(IS1.get(n))
{
n^=rand(); //I think the problem starts on this line...
OS1<<n;
cout<<"character coded:"<<n;
}
}
IS1.close();
OS1.close();
getch();
return 0;
}
You seed the random number generator before encrypting, but not before decrypting, so the series of random numbers will be different; obviously they would have to be the same for this to work. If you add another
before the decryption second, that will get you closer.
On closer inspection, though, there are some significant other problems. For example, each time you call
get(n), you consume a character from your input file. You’re callingget(n)in your while loop conditions, but then immediately calling it again in yourif; that means you’ll effectively be skipping every other character on input and again on output; the decrypted file would then end up with just 25% of the characters from the original file! Just remove theifstatements altogether, and your loop will be right. Hopefully having fixed both of these things your program will work.