I am trying to write a caesar cipher program in c++. This is my codes template:
int chooseKEY (){
//choose key shift from 1-26
}
void encrypt (char * w, char *e, int key)
{
//Encryption function, *w is the text in the beginning, the *e is the encrypted text
//Encryption in being made only in letters noy in numbers and punctuations // *e = *w + key }
void decrypt (char * e, char *w, int key)
{
// Decryption function, *e is the text in the beginning, the *w is the decrypted text
//Dencryption in being made only in letters no numbers and punctuations // *w = *e - key
}
void Caesar (char * inputFile, char * outputFile, int key, int mode)
{
// Read the inputfile which contains some data. If mode ==1 then the data is being
//encrypted else if mode == 0 the data is being decrypted and is being written in
//the output file
}
void main()
{
// call the Caesar function
}
The program has four functions, chooseKey function have to return an int as a shift key from 1-26. Encrypt function has three parameters, *w is the text in the beginning, *e is the encrypted text and the key is from the choosekey function.For encryption : Only letters have to be encrypted not numbers or punctuation and the letters are counting cyclic. Decrypt function has three parameters *e is the encrypted text, *w is the beginning text and the key. Caesar function has four parameters, inputfile which is the file that contains the beginning text, output file which contains the encrypted text, the key and the mode (if mode==1) encryption, (mode ==0) decryption. This is my sample code:
#include <iostream>
#include <fstream>
using namespace std;
int chooseKey()
{
int key_number;
cout << "Give a number from 1-26: ";
cin >> key_number;
while(key_number<1 || key_number>26)
{
cout << "Your number have to be from 1-26.Retry: ";
cin >> key_number;
}
return key_number;
}
void encryption(char *w, char *e, int key){
char *ptemp = w;
while(*ptemp){
if(isalpha(*ptemp)){
if(*ptemp>='a'&&*ptemp<='z')
{
*ptemp-='a';
*ptemp+=key;
*ptemp%=26;
*ptemp+='A';
}
}
ptemp++;
}
w=e;
}
void decryption (char *e, char *w, int key){
char *ptemp = e;
while(*ptemp){
if(isalpha(*ptemp))
{
if(*ptemp>='A'&&*ptemp<='Z')
{
*ptemp-='A';
*ptemp+=26-key;
*ptemp%=26;
*ptemp+='a';
}
}
ptemp++;
}
e=w;
}
void Caesar (char *inputFile, char *outputFile, int key, int mode)
{
ifstream input;
ofstream output;
char buf, buf1;
input.open(inputFile);
output.open(outputFile);
buf=input.get();
while(!input.eof())
{
if(mode == 1){
encryption(&buf, &buf1, key);
}else{
decryption(&buf1, &buf, key);
}
output << buf;
buf=input.get();
}
input.close();
output.close();
}
int main(){
int key, mode;
key = chooseKey();
cout << "1 or 0: ";
cin >> mode;
Caesar("test.txt","coded.txt",key,mode);
system("pause");
}
I am trying to run the code and it is being crashed (Debug Assertion Failed). I think the problem is somewhere inside Caesar function.How to call the encrypt and decrypt functions. But i don't know what exactly is. Any ideas?