diff options
| author | MichaelTheShifter <[email protected]> | 2016-07-05 08:35:02 -0400 |
|---|---|---|
| committer | MichaelTheShifter <[email protected]> | 2016-07-05 08:35:02 -0400 |
| commit | b5a5e0eb8a29e7eb858b6db6f8f7a60a2b2ae90c (patch) | |
| tree | 3eee7061d3c3f5bf57793ef84f92b4a1e0211046 | |
| parent | 6e40ae80f39c766d611b4ca29aea0f6685db80dc (diff) | |
| download | shiftos-c-_theultimatehacker-b5a5e0eb8a29e7eb858b6db6f8f7a60a2b2ae90c.tar.gz shiftos-c-_theultimatehacker-b5a5e0eb8a29e7eb858b6db6f8f7a60a2b2ae90c.tar.bz2 shiftos-c-_theultimatehacker-b5a5e0eb8a29e7eb858b6db6f8f7a60a2b2ae90c.zip | |
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.
| -rw-r--r-- | source/WindowsFormsApplication1/API.cs | 52 |
1 files changed, 51 insertions, 1 deletions
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.Security.Cryptography; using System.Diagnostics; using System.Net; using System.ComponentModel; +using System.Net.NetworkInformation; namespace ShiftOS { @@ -417,6 +418,23 @@ namespace ShiftOS { 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 @@ namespace ShiftOS 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()) @@ -463,6 +481,38 @@ namespace ShiftOS /// <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)) { |
