void Encrypt(string data, string* key, string* crypted_data)
{
*crypted_data = "";
int current_run = 1; // keep track of whether this is an odd or even run
// 0 = even, 1 = odd
// Set up a loop to deal with 8 bytes (64 bits) of data at a time
for(string::size_type i = 0; i < data.length(); i += 8)
{
char* block_temp = new char[8];
data.substr(i,8).copy(block_temp, 8);
unsigned char* block = new unsigned char[8];
memset(block, 0, 8);
memcpy(block_temp, block, 8);
delete block_temp;
unsigned char* curr_key = new unsigned char[8];
char* trans_map = new char;
/*
Note to anyone reading this:
The if structure below has a lot of duplicated code, but this is
only an example so I can't be bothered to make it more efficient!
Its only a matter of moving a lot of it out of the if, else structure
but I'm trying to get this finished!!
*/
if(current_run)
{
// An odd run, use first half of key
char* key_half = new char[8];
(key->substr(0,8)).copy(key_half, 8);
memcpy(key_half, curr_key, 8);
delete key_half;
(key->substr(9,1)).copy(trans_map, 1);
// curr_key now contains the right bit of the key
// and trans_map holds the byte used to map the transposition
}
else
{
// an even run, use second half of key
char* key_half = new char[8];
(key->substr(8,8)).copy(key_half, 8);
memcpy(key_half, curr_key, 8);
delete key_half;
(key->substr(0,1)).copy(trans_map, 1);
// curr_key now contains the right bit of the key
// and trans_map holds the byte used to map the transposition
}
// Now we can XOR the bytes of block against curr_key
unsigned char* ciphertext = new unsigned char[8];
for(int i = 0; i < 8; i++)
{
ciphertext[i] = block[i] ^ curr_key[i];
}
delete curr_key;
// Next, apply the transposition
// This bit could get confusing!
char tr_0 = 0x01;
char tr_1 = 0x02;
char tr_2 = 0x04;
char tr_3 = 0x08;
char tr_4 = 0x10;
char tr_5 = 0x20;
char tr_6 = 0x40;
char tr_7 = 0x80;
unsigned char temp;
if(*trans_map & tr_0)
{
fw_swap(ciphertext, 0, 1);
}
if(*trans_map & tr_1)
{
fw_swap(ciphertext, 1, 2);
}
if(*trans_map & tr_2)
{
fw_swap(ciphertext, 2, 3);
}
if(*trans_map & tr_3)
{
fw_swap(ciphertext, 3, 4);
}
if(*trans_map & tr_4)
{
fw_swap(ciphertext, 4, 5);
}
if(*trans_map & tr_5)
{
fw_swap(ciphertext, 5, 6);
}
if(*trans_map & tr_6)
{
fw_swap(ciphertext, 6, 7);
}
if(*trans_map & tr_7)
{
fw_swap(ciphertext, 7, 0);
}
char* tmp = new char[8];
memcpy(ciphertext, tmp, 8);
string st_tmp = tmp;
delete tmp;
*crypted_data += st_tmp;
if(current_run)
{
current_run = 0;
}
else
{
current_run = 1;
}
}
}
void Decrypt(string data, string* key, string* decrypted_data)
{
*decrypted_data = "";
int current_run = 1; // keep track of whether this is an odd or even run
// 0 = even, 1 = odd
// Set up a loop to deal with 8 bytes (64 bits) of data at a time
for(string::size_type i = 0; i < data.length(); i += 8)
{
char* block_temp = new char[8];
data.substr(i,8).copy(block_temp, 8);
unsigned char* block = new unsigned char[8];
memset(block, 0, 8);
memcpy(block_temp, block, 8);
delete block_temp;
unsigned char* curr_key = new unsigned char[8];
char* trans_map = new char;
/*
Note to anyone reading this:
The if structure below has a lot of duplicated code, but this is
only an example so I can't be bothered to make it more efficient!
Its only a matter of moving a lot of it out of the if, else structure
but I'm trying to get this finished!!
*/
if(current_run)
{
// An odd run, use first half of key
char* key_half = new char[8];
(key->substr(0,8)).copy(key_half, 8);
memcpy(key_half, curr_key, 8);
delete key_half;
(key->substr(9,1)).copy(trans_map, 1);
// curr_key now contains the right bit of the key
// and trans_map holds the byte used to map the transposition
}
else
{
// an even run, use second half of key
char* key_half = new char[8];
(key->substr(8,8)).copy(key_half, 8);
memcpy(key_half, curr_key, 8);
delete key_half;
(key->substr(0,1)).copy(trans_map, 1);
// curr_key now contains the right bit of the key
// and trans_map holds the byte used to map the transposition
}
// First reverse the transposition:
char tr_0 = 0x01;
char tr_1 = 0x02;
char tr_2 = 0x04;
char tr_3 = 0x08;
char tr_4 = 0x10;
char tr_5 = 0x20;
char tr_6 = 0x40;
char tr_7 = 0x80;
unsigned char temp;
if(*trans_map & tr_0)
{
rev_swap(block, 0, 1);
}
if(*trans_map & tr_1)
{
rev_swap(block, 1, 2);
}
if(*trans_map & tr_2)
{
rev_swap(block, 2, 3);
}
if(*trans_map & tr_3)
{
rev_swap(block, 3, 4);
}
if(*trans_map & tr_4)
{
rev_swap(block, 4, 5);
}
if(*trans_map & tr_5)
{
rev_swap(block, 5, 6);
}
if(*trans_map & tr_6)
{
rev_swap(block, 6, 7);
}
if(*trans_map & tr_7)
{
rev_swap(block, 7, 0);
}
// Then XOR again to decrypt:
unsigned char* plaintext = new unsigned char[8];
for(int i = 0; i < 8; i++)
{
plaintext[i] = block[i] ^ curr_key[i];
}
delete curr_key;
char* tmp = new char[8];
memcpy(plaintext, tmp, 8);
string st_tmp = tmp;
delete tmp;
*decrypted_data += st_tmp;
if(current_run)
{
current_run = 0;
}
else
{
current_run = 1;
}
}
}