Hi, i am trying to make a c++ program to encrypt and decrypt text file using random variable function. Operating system that is used is Ubuntu, My code to encrypt and decrypt are as follow:
#include <iostream>
#include<cstring>
#include<stdio.h>
#include<stdlib.h>
usingnamespace std;
int main(){
int i=0, seed; //seed value for random variables
char ch;
char linestr[10000],copy[10000];
cout<<"enter the seed value ";
cin>>seed;
FILE *input;
FILE *output;
input=fopen("in.txt","r"); //reading in.txt from the current directory
output=fopen("output.txt","w");//encoded output to the file output.txt
fgets(linestr,9999,input);//reads the first line of input and puts it into linestr array
srand(seed);
while(!feof(input)){
strcpy(copy,linestr);
while(copy[i+1]!='\0'){
ch=copy[i];
ch=(ch+rand())%128;
copy[i]=ch;
i++;
}/*copying the data into the copy array and then decoding it using this formulae which includes random variable*/
fputs(copy,output);//copying it to the output file
fgets(linestr,9999,input);//reading the next line
i=0;
}
return 0;
}
#include <iostream>
#include<cstring>
#include<stdio.h>
#include<stdlib.h>
usingnamespace std;
int main(){
int i=0, seed;
char ch;
char linestr[10000],copy[10000];
cout<<"enter the seed value ";
cin>>seed;
FILE *input;
FILE *output;
input=fopen("output.txt","r");//reading the data from output.txt
output=fopen("output1.txt","w");//output of decoded data is suppused to goto output1.txt
fgets(linestr,9999,input);
srand(seed);
while(!feof(input)){
strcpy(copy,linestr);
while(copy[i+1]!='\0'){
ch=copy[i];
ch=(ch-rand())%128;
if (ch<=0)
{ch=ch+128;}
copy[i]=ch;
i++;
}//formuale for decoding
fputs(copy,output);
fgets(linestr,9999,input);
i=0;
}
return 0;
}
I am facing a few problems with this code. As i enter some small text(2-3 lines) into the text file(in.txt) it works fine with some seed values but for some it shows crap in the end and if i put some large text into the text file and insert any seed value it gives me the correct decryption only for a small initial part of that file and after that it start giving crap again. So as i observe i face problem with this random function because for different seed values the extent of decryption is also different. I am supposed to submit a project in my college within this week. Kindly help me making this program work successfully, i also need to do the same for .bmp files i.e. decrypt and encrypt them. Please help me find the right code for my program so that it works flawlessly.
Waiting for a quick reply.
i hope the tagging is appropriate now. I think the loop that reads the file is correct because we are not facing the problem with the whole data, it just fails to decrypt data which comes later in case of large files and for small files it works fine depending upon the seed values. Although if u find any mistake then please do mention.
Do you mind editing your code and place code format tags around the code? To begin with, the loop that reads the file doesn't look right and the lack of formatting makes it tricky to read.
Hey i have edited the code in the same post so please look at it.
Thanku saurabh for that reference but that is a paid software where as i need to make my own program so i can only use its code as a reference which is not available free of cost so i request u too kindly help me in my code by pointing out the mistake and helping me solve it. I need quick replies as i need to work for .bmp files as well and have to complete it all by before 11th.
even if ch+rand()=128 in ch=(ch+rand())%128; then the encryption will be zero and while decryption we will get back the value of ch.
suppose ch=2 and rand()=126 so new ch=0 which is stored and now while decryption it subtracts 126 from 0 and we get -126 which upon applying % operator gives -126 and after adding 128 we get back 2.
Problem arises only when actual value of ch=0 but its no harm to our program because we assume that while entering normal text the user won't use such characters whose ascii value is zero.
If you read from the text file via your fgets function, we have no issues.
We receive a line from the text file.
When we however, encrypt the line, we have no control of the characters that the line will be converted into.
In particular - we may convert the line to an "invalid" sequence of text chars - ie have it shortened by a false null terminator, ... or have a alse CRLF within sequence that when we later try decrypting the file we accidentally read the false line break which would then change our original file.
Just simply work with file as binary file - even if it is a text file - that way you won't be handling special character like line breaks differently - ie each will go through the same transformation.
Don't also concern yourself about wrapping values - this will happen automatically. To make things more intuitive for yourself you can convert to unsigned char.
Also be careful of using the rand function in you encrypt decrypt. You may only have a guarentee when encrypting and decrypting on same box that we would get back the same sequence of random values for a particular chosen seed value - you have no gaurentee that this would always be so on every system - rand is ideally supposed to return a random value and not a predictable one.
Also take care of your logic around your while loop - seems like a file with only one line may not enter the while loops true condition.