#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

void Encrypt(String* strKey, String* strClear, String** strEncrypted)
{
// simple XOR each char
int posKey = 0;

Byte bClear[] = Encoding::ASCII->GetBytes(strClear);
Byte bKey[] = Encoding::ASCII->GetBytes(strKey);
Byte data[] = new Byte[bClear->Length];

for (int pos = 0; pos < bClear->Length; pos++)
{
   data[pos] = bClear[pos] ^ bKey[posKey];
   posKey++;
   if (posKey == bKey->Length) posKey = 0;
}

// convert the chars to printable characters with base64 encoding
*strEncrypted = Convert::ToBase64String(data);
return;
}

void Decrypt(String* strKey, String* strData, String** strClear)
{
// convert the base64 to a string and XOR the data
Byte b[] = Convert::FromBase64String(strData);
Byte bKey[] = Encoding::ASCII->GetBytes(strKey);

int posKey = 0;
Byte data[] = new Byte[b->Length];

for (int pos = 0; pos < b->Length; pos++)
{
   data[pos] = b[pos] ^ bKey[posKey];
   posKey++;
   if (posKey == bKey->Length) posKey = 0;
}

*strClear = Encoding::ASCII->GetString(data);
return;
}

public __gc class Data
{
public:
   String* GetData(String* strData, String* strKey)
   {
      String* str = S"";
      Encrypt(strKey, strData, &str);
      return str;
   }
   String* GetString(String* strData, String* strKey)
   {
      String* str = S"";
      Decrypt(strKey, strData, &str);
      return str;
   }
};
