In: Computer Science
Please solve all the following questions. I need the text file and screenshots of encryption and decryption.
Exchange of encrypted data.
a. Encrypt a file (e.g., a text file) with an algorithm and a key length of your choice.
b. Exchange the file and the necessary credentials for decryption (i.e., algorithm, key) with your neighbor.
c. Decrypt the secret of your neighbor.
a.
To encrypt the file, we will use the modified Caesar cipher algorithm. In the modified Caesar cipher every character in the plaintext is moved by key number of places. Where key is common for both the sender and receiver.
Encryption:
Program:
#include <iostream>
#include <string.h>
using namespace std;
string encrypt(char text[], int
s)
{
string result = "";
for (int i=0;i<strlen(text);i++)
{
if(text[i]>='0'&&text[i]<='9')
{
result+=char(int(text[i]+s-48)%10+48);
}
else
if(text[i]>='A'&&text[i]<='Z')
{
result+=char(int(text[i]+s-65)%26+65);
}
else if(text[i]>='a'&&text[i]<='z')
{
result+=char(int(text[i]+s-97)%26+97);
}
else
{
result+=text[i];
}
}
return result;
}
int main()
{
char plainText[100];
int key;
cout<<"Enter message: ";
cin.getline(plainText,100);
cout<<"Enter key: ";
cin>>key;
cout<<"\nPlain text : "<<plainText;
cout<<"\nKey: "<<key;
cout<<"\nEncrypted Text:
"<<encrypt(plainText,key);
return 0;
}
Screenshot of the Code:
Output:
b.
Message to share: Ymnx nx f xnruqj rjxxflj. 678
Key: 5
c.
Decryption:
#include <iostream>
#include <string.h>
using namespace std;
string decrypt(char text[], int
s)
{
string result = "";
for (int i=0;i<strlen(text);i++)
{
if(text[i]>='0'&&text[i]<='9')
{
result+=char(int(text[i]+s-48)%10+48);
}
else
if(text[i]>='A'&&text[i]<='Z')
{
result+=char(int(text[i]+s-65)%26+65);
}
else if(text[i]>='a'&&text[i]<='z')
{
result+=char(int(text[i]+s-97)%26+97);
}
else
{
result+=text[i];
}
}
return result;
}
int main()
{
char text[100];
int key;
cout<<"Enter text to decrypt: ";
cin.getline(text,100);
cout<<"Enter key: ";
cin>>key;
cout<<"\nText to decrypt : "<<text;
cout<<"\nKey: "<<key;
cout<<"\nDecrypted Text:
"<<decrypt(text,26-key);
return 0;
}
Screenshot of the Code:
Output: