Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create (fill-in) the following 6 functions 1. Crypto(string) - Class constructor

ID: 3677748 • Letter: C

Question

Create (fill-in) the following 6 functions
1. Crypto(string) - Class constructor which accepts a string password to be used in all encryption and decryption for these other functions.
2. caesarEncrypt(string) - Given a string, return the caesar cypher encrypted string. Use the saved key in any way you want to generate an offset number.
3. caesarDecrypt(string) - Given a string, return the caesar decrypted string. Use the saved key in any way you want to generate an offset number.
4. vigenereEncrypt(string) - Given a string, return the vigenere cypher encrypted string.
5. vigenereDecrypt(string) - Given a string, return the vigenere cypher decrypted string.
6. letterFreqStDev(string) - Given a string, find the character counts (convert all to lowercase), and find the standard deviation of the character count list.

This function can be used to find how uniform the letter distribution of a string is. Distributions of an encrypted string are harder to crack if they have a lower standard deviation; if they are close to uniform.

Explanation / Answer

#include <iostream>
#include<string>
#include<math.h>
using namespace std;
class Crypto
{
public:
string password;
string key;

int key1;
Crypto(string pass) { password=pass; key1=3; key="abc";}
double letterFreqStDev(string text)
{ int countchar[26]={0};
int sum=0;
double mean=0,s[26]={0},result=0;
for(int i=0;i<text.length();i++)
{
char c = text[i];

if(c >= 'A' && c <= 'Z')
text[i]=text[i]+'a'-'A';
else
continue;

}
for(int i=0;i<26;i++)
{
if(text[i]>='a' && text[i]<='z')
{
countchar[text[i]-97]++; sum++;
}
}
mean= sum/26;
for(int i=0;i<26;i++) { s[i]=(countchar[i]-mean)*(countchar[i]-mean);}
for(int i=0;i<26;i++) { mean=mean+s[i];} mean=mean/26;
result = sqrt (mean);
return result;
}
string CaesarEncrypt(string text)
{
for(int i=0;i<text.length();i++)
{
if(text[i]=='X') text[i]='A';
else if(text[i]=='Y') text[i]='B';
else if(text[i]=='Z') text[i]='C';
else if(text[i]=='x') text[i]='a';
else if(text[i]=='y') text[i]='b';
else if(text[i]=='z') text[i]='c';
else if((text[i]>='A' && text[i]<'Z')||(text[i]>='a' && text[i]<'z')) text[i]=text[i]+key1;
}
return text;
}
string CaesarDecrypt(string text)
{
for(int i=0;i<text.length();i++)
{
if(text[i]=='A') text[i]='X';
else if(text[i]=='B') text[i]='Y';
else if(text[i]=='C') text[i]='Z';
else if(text[i]=='a') text[i]='x';
else if(text[i]=='b') text[i]='y';
else if(text[i]=='c') text[i]='z';
else if((text[i]>'A' && text[i]<='Z')||(text[i]>'a' && text[i]<='z')) text[i]=text[i]-key1;
}
return text;
}
string vigenereEncrypt(string text)
{
string out;

for(int i = 0, j = 0; i < text.length(); ++i)
{
char c = text[i];

if(c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if(c < 'A' || c > 'Z')
continue;

out += (c + key[j] - 2*'A') % 26 + 'A';
j = (j + 1) % key.length();
}

return out;
}

string vigenereDecrypt(string text)
{
string out;

for(int i = 0, j = 0; i < text.length(); ++i)
{
char c = text[i];

if(c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if(c < 'A' || c > 'Z')
continue;

out += (c - key[j] + 2*'A') % 26 + 'A';
j = (j + 1) % key.length();
}

return out;
}
};
int main()
{
Crypto cipher("IAMCIPHER");

string original = cipher.password;
string e = cipher.CaesarEncrypt(original);
string d = cipher.CaesarDecrypt(e);
cout << original << endl;
cout << "Encrypted: " << e << endl;
cout << "Decrypted: " << d << endl;
string encrypted = cipher.vigenereEncrypt(original);
string decrypted = cipher.vigenereDecrypt(encrypted);

cout << original << endl;
cout << "Encrypted: " << encrypted << endl;
cout << "Decrypted: " << decrypted << endl;
cout<<cipher.letterFreqStDev(original);
}