From b5a5e0eb8a29e7eb858b6db6f8f7a60a2b2ae90c Mon Sep 17 00:00:00 2001 From: MichaelTheShifter Date: Tue, 5 Jul 2016 08:35:02 -0400 Subject: [PATCH] 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. --- source/WindowsFormsApplication1/API.cs | 52 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/source/WindowsFormsApplication1/API.cs b/source/WindowsFormsApplication1/API.cs index 3ccab3c..9c8c69c 100644 --- a/source/WindowsFormsApplication1/API.cs +++ b/source/WindowsFormsApplication1/API.cs @@ -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) /// The encrypted string. /// The decrypted string. 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))