This is an encryption program. Modify the functions with an algorithm of a diffe
ID: 3536080 • Letter: T
Question
This is an encryption program. Modify the functions with an algorithm of a different design to encrypt/decrypt. The functions should follow the same format as below. Just edit the contents of the encrypt/decrypt functions and jumble/unjumble the data as you see fit.They should accept a character array, combine it with the 16 byte key and manipulate it to some degree. Thank you
#include <iostream>
using namespace std;
#define BLOCK_SIZE 16
// Initialize key array, same size as data array
// The key is just an arbitary collection of bytes, it can be anything as long as they same key's used for encryption/decryption
const char key[] = "a@SDF23hds234u(D";
// Combines data bytes with key bytes then uses arbitary algorithm to jumble the data
void encrypt(char data[]) {
// Add the value of every index in the key to every index in the data array
for (int i = 0; i < BLOCK_SIZE; i++) {
data[i] = data[i] + key[i];
}
// Shift every member in the array left by 1
// So 0123456789012345 -> 1234567890123450
char first = data[0];
for (int i = 0; i < BLOCK_SIZE - 1; i++) {
data[i] = data[i + 1];
}
data[BLOCK_SIZE - 1] = first;
}
// Uses reverse of encryption algorithm to un-jumble data then seperates data bytes from key bytes
void decrypt(char data[]) {
// Shift every member in the array right by 1
// So 1234567890123450 -> 0123456789012345
char last = data[BLOCK_SIZE - 1];
for (int i = BLOCK_SIZE - 1; i >= 0; i--) {
data[i] = data[i - 1];
}
data[0] = last;
// Subtract the values of they key from the values of the data array
for (int i = 0; i < BLOCK_SIZE; i++) {
data[i] = data[i] - key[i];
}
}
int main() {
// Initialize data array
char data[] = "0123456789012345";
cout << "Encyption" << endl << endl << "[Data In]" << endl;
cout << data;
cout << endl << endl;
encrypt(data);
cout << "[Data Out]" << endl;
cout << data;
cout << endl << endl << endl;
cout << "Decryption" << endl << endl << "[Data In]" << endl;
cout << data;
cout << endl << endl;
decrypt(data);
cout << "[Data Out]" << endl;
cout << data;
cin.get();
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
#define BLOCK_SIZE 16
// Initialize key array, same size as data array
// The key is just an arbitary collection of bytes, it can be anything as long as they same key's used for encryption/decryption
const char key[] = "a@SDF23hds234u(D";
// Combines data bytes with key bytes then uses arbitary algorithm to jumble the data
void encrypt(char data[]) {
// Add the value of every index in the key to every index in the data array
for (int i = 0; i < BLOCK_SIZE; i++) {
data[i] = data[i] + key[i];
}
for (int i = 0; i < BLOCK_SIZE; i++) {
data[i] = data[i] + 5;
}
}
// Uses reverse of encryption algorithm to un-jumble data then seperates data bytes from key bytes
void decrypt(char data[]) {
for (int i = 0; i < BLOCK_SIZE; i++) {
data[i] = data[i] - 5;
}
// Subtract the values of they key from the values of the data array
for (int i = 0; i < BLOCK_SIZE; i++) {
data[i] = data[i] - key[i];
}
}
int main() {
// Initialize data array
char data[] = "0123456789012345";
cout << "Encyption" << endl << endl << "[Data In]" << endl;
cout << data;
cout << endl << endl;
encrypt(data);
cout << "[Data Out]" << endl;
cout << data;
cout << endl << endl << endl;
cout << "Decryption" << endl << endl << "[Data In]" << endl;
cout << data;
cout << endl << endl;
decrypt(data);
cout << "[Data Out]" << endl;
cout << data;
cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.