private byte[] EncryptData(byte[] data)
{
if(_useDPAPI) {
try {
return ProtectedData.Protect(data, null, DataProtectionScope.CurrentUser);
}
catch(Exception e) {
Debug.ReportError("Failed to encrypt data using DPAPI. Exception: {0}", e.Message);
return new byte[0];
}
}
else {
InitEncryptionAlgorithm();
RijndaelManaged aes = null;
ICryptoTransform encryptor = null;
CryptoStream encStream = null;
MemoryStream inputStream = null;
try {
inputStream = new MemoryStream();
aes = new RijndaelManaged();
aes.IV = EncryptionIV;
aes.Key = _encryptionKey;
encryptor = aes.CreateEncryptor(_encryptionKey, EncryptionIV);
// encrypt
encStream = new CryptoStream(inputStream, encryptor, CryptoStreamMode.Write);
encStream.Write(data, 0, data.Length);
encStream.FlushFinalBlock();
encStream.Close();
return inputStream.ToArray();
}
catch(Exception e) {
Debug.ReportError("Failed to encrypt data using AES. Exception: {0}", e.Message);
return new byte[0];
}
finally {
if(aes != null) {
aes.Clear();
}
if(encStream != null) {
encStream.Close();
}
if(inputStream != null) {
inputStream.Close();
}
}
}
}