2003-09-21 23:24:53 +02:00
|
|
|
// last modified Friday, May 09, 2003
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2003-09-21 23:24:53 +02:00
|
|
|
#include "Encryption.h"
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// EncryptData - Takes A pointer to data in memory, the length of that data,
|
|
|
|
// Along with a key in memory and its length. It will allocated space
|
|
|
|
// for the encrypted copy of the data via new[], It is the responsiblity of the user
|
|
|
|
// to call delete[].
|
|
|
|
//--------------------------------------------------------------------
|
2004-06-01 18:51:37 +02:00
|
|
|
char *EncryptData(char *Data, long DataLength, char *Key, long KeyLength)
|
2003-09-21 23:24:53 +02:00
|
|
|
{
|
|
|
|
// Allocate space for new Encrypted data
|
2004-06-01 18:51:37 +02:00
|
|
|
char *NewData = new char[DataLength];
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// A counter to hold our absolute position in data
|
2004-06-01 18:51:37 +02:00
|
|
|
long DataOffset = 0;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// Loop through Data until end is reached
|
|
|
|
while (DataOffset < DataLength)
|
|
|
|
{
|
|
|
|
// Loop through the key
|
2004-06-01 18:51:37 +02:00
|
|
|
for (long KeyOffset = 0; KeyOffset < KeyLength; KeyOffset++)
|
2003-09-21 23:24:53 +02:00
|
|
|
{
|
|
|
|
// If were at end of data break and the the while loop should end as well.
|
|
|
|
if (DataOffset >= DataLength)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Otherwise Add the previous element of the key from the newdata
|
|
|
|
// (just something a little extra to confuse the hackers)
|
|
|
|
if (KeyOffset > 0) // Don't mess with the first byte
|
|
|
|
NewData[DataOffset] += Key[KeyOffset - 1];
|
|
|
|
|
|
|
|
// Xor the Data byte with the key byte and get new data
|
|
|
|
NewData[DataOffset] = Data[DataOffset] ^ Key[KeyOffset];
|
|
|
|
|
|
|
|
// Increase position in data
|
|
|
|
DataOffset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the new data
|
|
|
|
return NewData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// DecryptData - Takes A pointer to data in memory, the length of that data,
|
|
|
|
// Along with a key in memory and its length. It will allocated space
|
|
|
|
// for the decrypted copy of the data via new[], It is the responsiblity of the user
|
|
|
|
// to call delete[].
|
|
|
|
//--------------------------------------------------------------------
|
2004-06-01 18:51:37 +02:00
|
|
|
char *DecryptData(char *Data, long DataLength, char *Key, long KeyLength)
|
2003-09-21 23:24:53 +02:00
|
|
|
{
|
|
|
|
// Allocate space for new Decrypted data
|
2004-06-01 18:51:37 +02:00
|
|
|
char *NewData = new char[DataLength];
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// A counter to hold our absolute position in data
|
2004-06-01 18:51:37 +02:00
|
|
|
long DataOffset = 0;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// Loop through Data until end is reached
|
|
|
|
while (DataOffset < DataLength)
|
|
|
|
{
|
|
|
|
// Loop through the key
|
2004-06-01 18:51:37 +02:00
|
|
|
for (long KeyOffset = 0; KeyOffset < KeyLength; KeyOffset++)
|
2003-09-21 23:24:53 +02:00
|
|
|
{
|
|
|
|
// If were at end of data break and the the while loop should end as well.
|
|
|
|
if (DataOffset >= DataLength)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Otherwise Xor the Data byte with the key byte and get new data
|
|
|
|
NewData[DataOffset] = Data[DataOffset] ^ Key[KeyOffset];
|
|
|
|
|
|
|
|
// Subtract the previous element of the key from the newdata
|
|
|
|
// (just something a little extra to confuse the hackers)
|
|
|
|
if (KeyOffset > 0) // Don't mess with the first byte
|
|
|
|
NewData[DataOffset] -= Key[KeyOffset - 1];
|
|
|
|
|
|
|
|
// Increase position in data
|
|
|
|
DataOffset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the new data
|
|
|
|
return NewData;
|
|
|
|
}
|