you are to write a program that will encode and decode an input string using a k
ID: 2250154 • Letter: Y
Question
you are to write a program that will encode and decode an input string using a key string. As each letter of the input string is encoded or decoded, the next character in the key string is to be used. When the end of the key string is found, the program needs to start back at the beginning of the key string as needed.
#include <iostream>
#include <cstring>
using namespace std;
//xor operator ^: Hash code applied twice regenerates the original value:
m is message character and h is hash
// x=m^h and y=x^h then y (decoded message) is the same as m
void coder(char *x, char key,int length);
char *getMessage(char *request);
int main()
{
char *m = getMessage("Enter message to encode: ");
int length = strlen(m);
cout << length << endl;
//char *k = getMessage("Enter key ");//read in a key of arbitrary
length
coder(m, 'A', length);//A is the key. Encode message with key string
cout << m << endl;
coder(m, 'A', length);//Decode message. Decode message with key string
cout << m << endl;
delete(m); //no leaks
// delete(k);
return 0;
}
void coder(char *message, char key, int length)//make key an array
{
for (int i = 0; i < length; i++)
message[i] = message[i]^key;//cycle through the key
}
char *getMessage(char *request)
{
char temp[100], *p;
cout << request << " ";
cin >> temp;
p = new char(strlen(temp)+1);//+1 for terminating null
strcpy(p, temp);
return p;
}
Explanation / Answer
Code:
#include <iostream>
#include <cstring>
using namespace std;
//xor operator ^: Hash code applied twice regenerates the original value:m is message character and h is hash
// x=m^h and y=x^h then y (decoded message) is the same as m
void coder(char *x, char key,int length);
char *getMessage(char *request);
int main()
{
while(1)
{
char *m = getMessage("Enter message to encode: ");
int length = strlen(m);
cout << length << endl;
char k = m[1];//read in a key of arbitrary length
coder(m, k, length);//A is the key. Encode message with key string
cout << m << endl;
coder(m, k, length);//Decode message. Decode message with key string
cout << m << endl;
delete(m); //no leaks
}
// delete(k);
return 0;
}
void coder(char *message, char key, int length)//make key an array
{
for (int i = 0; i < length; i++)
message[i] = message[i]^key;//cycle through the key
}
char *getMessage(char *request)
{
char temp[100], *p;
cout << request << " ";
cin >> temp;
p = new char(strlen(temp)+1);//+1 for terminating null
strcpy(p, temp);
return p;
}
Output:
Enter message to encode: This is test msg.
4
<
This
Enter message to encode: 2
is
Enter message to encode: 4
test
Enter message to encode: 4
msg.
Enter message to encode: It is test msg two.
2
=
It
Enter message to encode: 2
is
Enter message to encode: 4
test
Enter message to encode: 3
msg
Enter message to encode: 4
two.
Enter message to encode:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.