Write a program in Python or C++ that has TWO functions: Function One is called:
ID: 3569688 • Letter: W
Question
Write a program in Python or C++ that has TWO functions:
Function One is called: _encrypt
Function Two is called: _decrypt
Function one takes plain text, and assesses the indexed value of each letter in the string. For example, a=1, b=2. It then adds three to the indexed value, and produces encrypted text, based off of the plain text.
Function two reverses this.
Here is sample output:
_encrypt('how do you do')
Unsecured: howdoyoudo
Secured: lsahscsyhs
_decrypt('lsahscsyhs')
Unsecured: lsahscsyhs
Secured: howdoyoudo
Explanation / Answer
Program code:
// EncryptionDecryption.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int size=0;
//declare the functions
string _encrypt(string s,char *encrypt, char *in);
void _decrypt(string s, char *encrypt,char * in);
int main()
{
//char array
char in[26]={'a','b','c','d','e', 'f', 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char encrypt[26];
//strings
string s;
string st;
char p[200];
int k=0;
//encrypt techinque
for(int i=0;i<26;i++)
{
if((i+4)<26)
{
encrypt[i]=in[i+4];
}
else
{
encrypt[i]=in[k];
k++;
}
}
cout<<"Enter a string: ";
getline(cin,s);
//remove the spaces in s
for(int i=0; i<s.length(); i++)
{
if(s[i] == ' ')
s.erase(i,1);
}
cout<<endl;
//call encryption store it in a string st
st=_encrypt(s,encrypt, in);
//call decrypt function
_decrypt(st, encrypt, in);
system("pause");
return 0;
}
//definition of _encrypt function and returns a string
string _encrypt(string s,char *encrypt, char *in)
{
int n=0;
char c[200];
//convert string to character
for(int i=0;i<s.size();i++)
{
c[n]=s.at(i);
n++;
}
size=n;
int k=0;
n=0;
char ch[200];
//logic to encrypt
for(int i=0; i<size;i++)
{
n=0;
n++;
while(n<26)
{
if(c[i]==in[n])
{
ch[i]=encrypt[n];
}
n++;
}
}
//print the output
cout<<"Unsecured: "<<s<<endl;
cout<<"Secured: ";
for(int i=0;i<size;i++)
{
s.at(i)=ch[i];
cout<<ch[i];
}
cout<<endl;
//return the modified string
return s;
}
//defintion of _decrypt
void _decrypt(string s,char *encrypt, char *in)
{
int n=0;
char c[200];
//convert string to char array
for(int i=0;i<s.size();i++)
{
c[n]=s.at(i);
n++;
}
int k=0;
n=0;
char ch[200];
//logic to decrypt
for(int i=0; i<size;i++)
{
n=0;
n++;
while(n<26)
{
if(c[i]==encrypt[n])
{
ch[i]=in[n];
}
n++;
}
}
//print the output
cout<<"Unsecured: "<<s<<endl;
cout<<"Secured: ";
for(int i=0;i<size;i++)
{
cout<<ch[i];
}
cout<<endl;
}
---------------------------------------------------------------------------------------
Sample output:
Enter a string: how do you do
Encryption:
_encrypt(howdoyoudo);
Unsecured: howdoyoudo
Secured: lsahscsyhs
Decryption:
_decrypt(lsahscsyhs);
Unsecured: lsahscsyhs
Secured: howdoyoudo
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.