Encryption keys for saves are now unique to the user's PC.

This mitigates the risk of being able to encrypt and decrypt the save
files as the Lua encrypt() and decrypt() methods will use a different
key.
This commit is contained in:
MichaelTheShifter 2016-07-05 08:35:02 -04:00
parent 6e40ae80f3
commit b5a5e0eb8a

View file

@ -13,6 +13,7 @@
using System.Diagnostics;
using System.Net;
using System.ComponentModel;
using System.Net.NetworkInformation;
namespace ShiftOS
{
@ -417,6 +418,23 @@ public static class Encryption
{
private static readonly string passPhrase = "h8gf9dh790df87h9";
private static string GetMacAddress()
{
string macAddresses = string.Empty;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return macAddresses;
}
// This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.
// This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be
// 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array.
@ -433,7 +451,7 @@ public static class Encryption
public static string Encrypt(string plainText)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
using (PasswordDeriveBytes password = new PasswordDeriveBytes(GetMacAddress(), null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
@ -462,6 +480,38 @@ public static string Encrypt(string plainText)
/// <param name="cipherText">The encrypted string.</param>
/// <returns>The decrypted string.</returns>
public static string Decrypt(string cipherText)
{
try
{
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(GetMacAddress(), null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
}
}
catch
{
return Decrypt_old(cipherText);
}
}
public static string Decrypt_old(string cipherText)
{
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))