This project/code uses Ceasar Cipher algorithm to encrypt and decrypt files using a key provided by the user in C++ program.
Nowadays, as with the increasing cyber-issues it feels unsafe to send some important documents over Internet or something. One of the easier ways is to encrypt your file and send it to the other person along with the key used so that they can decrypt it later and even if the text file was accessed by any third-party, it would be an encryption version. So over here we provide a file which needs to be encrypted/decrypted and the code creates a new file which contains the encrypted/decrypted version.
Lets take a look over the general flow of the code:
1. Import the text document which you want to modify:
int main(){ int choice; char data[90], temp; int key, count,n; std::fstream file1; //input file std::ofstream file2; //output file //MyFile.close(); file1.open("D:\\thefile.txt"); file2.open("D:\\editedfile.txt");
Here we have specified the location of file1 and we have also specified the location of file2 where the output is generated. Here the file2 which is the 'editedfile.txt' does not exist so the code creates a file named that and if in any case the file exists, it is overwritten.
2. Now ask the user whether to encrypt or decrypt the given file along with the key.
while (1) { printf("\n1. Encryption\n2. Decryption\n3. Exit\n"); printf("\nEnter You Choice:\t"); std::cin>>choice; std::cout << "Enter a Key:\t"; std::cin >> key;
While(1) is given in order to have an endless loop
std::cout << "case1"; if (file1.is_open()) { for (std::string line; std::getline(file1, line); ) { int n = line.length(); std::cout << line; char *data; data = (char*)malloc((n+1)*sizeof(std::string)); strcpy_s(data,100,line.c_str());
Here the getline function is used to extract data from the given file, line-by-line, as we encrypt/decrypt the file, one line at a time. And while encrypting as the encryption algorithm takes an input as one char variable at a time, we create a char array using dynamic memory allocation as the length of the line is unknown. So here, data is the char array of the given line which is ready for encryption/decryption.
for (count = 0; data[count] != '\0'; count++) { temp = data[count]; if (temp >= 'a' && temp <= 'z') { temp = temp + key; if (temp > 'z') { temp = temp - 'z' + 'a' - 1; } data[count] = temp; } else if (temp >= 'A' && temp <= 'Z') { temp = temp + key; if (temp > 'Z') { temp = temp - 'Z' + 'A' - 1; } data[count] = temp; } } std::cout << "\nEncrypted Message:\t\n" << data; file2 << data; }
So as everyone knows the ceasar cipher algorithm, I'm not going into the explanation of the algorithm as it's not much needed but its pretty simple if yall wanna know the mechanism as it is one of the primitive encryption algorithms. So as the given line 'data' is encrypted, we write it into the new file.
And similarly, the decryption code works.
Now lets try to ecrypt the given file:
Lets run the code:
Lets note the fact that the key provided here is '5'.
Lets have a look at the decrypted file:
Now lets decrypt the same file with the same key to check whether our code is working:
Now its time to check whether we have the same file back:
So finally it's verified and shows that our algorithm works fine!
Submitted by Abhita Lakkabathini (Abhita)
Download packets of source code on Coders Packet
Comments