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 use c++ language 1. Crypto(string) -

ID: 3682835 • Letter: C

Question

Create (fill-in) the following 6 functions use c++ language

1. Crypto(string) - Class constructor which accepts a string password to be used in all encryption and decryption for these other functions.

Crypto::Crypto(const string& key_temp){
// TODO: Fill me in
}

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. A good way to generate the offset is to sum up the ascii values in the key.

string Crypto::caesarEncrypt(const string& input){
// TODO: Fill me in
return "";
}
3. caesarDecrypt(string) - Given a string, return the caesar decrypted string. See encrypt for key description.

string Crypto::caesarDecrypt(const string& input){
// TODO: Fill me in
return "";
}

4. vigenereEncrypt(string) - Given a string, return the vigenere cypher encrypted string.

string Crypto::vigenereEncrypt(const string& input){
// TODO: Fill me in
return "";
}
5. vigenereDecrypt(string) - Given a string, return the vigenere cypher decrypted string.

string Crypto::vigenereDecrypt(const string& input){
// TODO: Fill me in
return "";
}
6. letterFreqStDev(string) - Given a string, find the character counts (convert all to lowercase), and find the standard deviation of the character count list. The list will always be of length 26. The string ‘abcdefghijklmnopqrstuvwxyz’ should return the value 0 because each letter has an occurrence of 1. 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.

double Crypto::letterFreqStDev(const string& input){
// TODO: Fill me in
return 0.0;
}

Explanation / Answer

1.

#include<iostream>
#include<string>
#include<stdio.h>
using std :: cout;
using std :: endl;
void encrypt( char [ ] );
void decrypt( char * ePtr );
int main( )
{
// Enter the password
//char password[ ] = "this is a secret!";
char password[30];
std::cout << "Please, enter your password: ";
std::cin >> password;
cout << "Original password is: " << password << endl;
encrypt(password);
// call to the function encrypt( )
cout << "Encrypted password is: " << password << endl;
decrypt(password);
// call to the function decrypt( )
cout << "Decrypted password is: " << password << endl;
return 0;
}// main

//encrypt data
void encrypt (char e[] )
{
for( int i=0; e[i] != ''; ++i ) ++e[i];
} // encrypt

//decrypt data
void decrypt( char * ePtr ) {
for( ; * ePtr != ''; ++ ePtr ) --(* ePtr);
}

2.

#include<iostream>
#include<string>

using std:: cout;
using std:: endl;
using std:: cin;
using std::string;

string Encrypt(string, int);

int main(int argc, char *argv[])
{
string Source;
int Key;

cout<<"Source: ";
getline(cin, Source);

cout<<"Key: ";
cin>> Key;

cout<<"Encrypted: "<< Encrypt(Source, Key) << endl;
}

string Encrypt(string Source, int Key)
{
string Crypted = Source;

for(int Current = 0; Current < Source.length(); Current++)
Crypted[Current] += Key;

return Crypted;
}

3.

#include <iostream>
#include <cstring>
using namespace std;

void caesar_encrypt(char src[100], int key, int dist)
{
for (int i=0; i < dist; i++)
{
if (src[i] >= 'A' && src[i] <= 'Z')
{
src[i] = (char)(((src[i] + key - 'A' + 26) % 26) + 'A');
}
else if (src[i] >= 'a' && src[i] <= 'z')
{
src[i] = (char)(((src[i] + key - 'a' + 26) % 26) + 'a');
}
}
}

void caesar_decrypt(char src[100], int key, int dist)
{
caesar_encrypt(src, 26 - (key % 26), dist);
}

int main ()
{
char caesar[10];
static const char encrypt[] = "encrypt";
static const char decrypt[] = "decrypt";
int key;
char src[100];
int result1;
int result2;
int dist;

cout << "Enter operation: encrypt or decrypt" << endl;
cin >> caesar;
cout << "Enter key" << endl;
cin >> key;
cout << "Enter text to encrypt/decrypt" << endl;
cin >> src;

dist = strlen (src);

result1 = strcmp (caesar, encrypt);
result2 = strcmp (caesar, decrypt);

if(result1 == 0)
{
caesar_encrypt(src, key, dist);
}
if(result2 == 0)
{
caesar_decrypt(src, key, dist);
}

cout << "Result:" << endl;
cout << src << endl;
}