From 1ad691ad99d1e98cd781f8e2a36062459ed490bd Mon Sep 17 00:00:00 2001 From: JFronny <33260128+JFronny@users.noreply.github.com> Date: Fri, 3 Apr 2020 17:32:22 +0200 Subject: [PATCH] Add crypto class --- Misc/Crypto.cs | 81 +++++++++++++++++++++++++++++++++++++++ Misc/GenericExtensions.cs | 2 + Misc/HID.cs | 4 +- 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 Misc/Crypto.cs diff --git a/Misc/Crypto.cs b/Misc/Crypto.cs new file mode 100644 index 0000000..95c3395 --- /dev/null +++ b/Misc/Crypto.cs @@ -0,0 +1,81 @@ +using System; +using System.IO; +using System.Security.Cryptography; + +namespace CC_Functions.Misc +{ + public static class Crypto + { + public static byte[] Encrypt(byte[] data, byte[] key) + { + if (key is null) + throw new ArgumentException("Key must have valid value.", nameof(key)); + if (data is null) + throw new ArgumentException("The text must have valid value.", nameof(data)); + + byte[] buffer = data; + SHA512CryptoServiceProvider hash = new SHA512CryptoServiceProvider(); + byte[] aesKey = new byte[24]; + Buffer.BlockCopy(hash.ComputeHash(key), 0, aesKey, 0, 24); + + using Aes aes = Aes.Create(); + if (aes == null) + throw new ArgumentException("Parameter must not be null.", nameof(aes)); + + aes.Key = aesKey; + + using ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); + using MemoryStream resultStream = new MemoryStream(); + using (CryptoStream aesStream = new CryptoStream(resultStream, encryptor, CryptoStreamMode.Write)) + { + using MemoryStream plainStream = new MemoryStream(buffer); + plainStream.CopyTo(aesStream); + } + + byte[] result = resultStream.ToArray(); + byte[] combined = new byte[aes.IV.Length + result.Length]; + Array.ConstrainedCopy(aes.IV, 0, combined, 0, aes.IV.Length); + Array.ConstrainedCopy(result, 0, combined, aes.IV.Length, result.Length); + + return combined; + } + + public static byte[] Decrypt(byte[] encrypted, byte[] key) + { + if (key is null) + throw new ArgumentException("Key must have valid value.", nameof(key)); + if (encrypted is null) + throw new ArgumentException("The encrypted text must have valid value.", nameof(encrypted)); + + byte[] combined = encrypted; + byte[] buffer = new byte[combined.Length]; + SHA512CryptoServiceProvider hash = new SHA512CryptoServiceProvider(); + byte[] aesKey = new byte[24]; + Buffer.BlockCopy(hash.ComputeHash(key), 0, aesKey, 0, 24); + + using Aes aes = Aes.Create(); + if (aes == null) + throw new ArgumentException("Parameter must not be null.", nameof(aes)); + + aes.Key = aesKey; + + byte[] iv = new byte[aes.IV.Length]; + byte[] ciphertext = new byte[buffer.Length - iv.Length]; + + Array.ConstrainedCopy(combined, 0, iv, 0, iv.Length); + Array.ConstrainedCopy(combined, iv.Length, ciphertext, 0, ciphertext.Length); + + aes.IV = iv; + + using ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); + using MemoryStream resultStream = new MemoryStream(); + using (CryptoStream aesStream = new CryptoStream(resultStream, decryptor, CryptoStreamMode.Write)) + { + using MemoryStream plainStream = new MemoryStream(ciphertext); + plainStream.CopyTo(aesStream); + } + + return resultStream.ToArray(); + } + } +} \ No newline at end of file diff --git a/Misc/GenericExtensions.cs b/Misc/GenericExtensions.cs index 7024a98..ebb226f 100644 --- a/Misc/GenericExtensions.cs +++ b/Misc/GenericExtensions.cs @@ -115,5 +115,7 @@ namespace CC_Functions.Misc public static Rectangle Round(this RectangleF self) => Rectangle.Round(self); public static Rectangle Ceiling(this RectangleF self) => Rectangle.Ceiling(self); + public static byte[] Encrypt(this byte[] self, byte[] key) => Crypto.Encrypt(self, key); + public static byte[] Decrypt(this byte[] self, byte[] key) => Crypto.Decrypt(self, key); } } \ No newline at end of file diff --git a/Misc/HID.cs b/Misc/HID.cs index 26be71d..34cb9fe 100644 --- a/Misc/HID.cs +++ b/Misc/HID.cs @@ -93,9 +93,9 @@ Win32_NetworkAdapterConfiguration:MACAddress"; } public static byte[] EncryptLocal(byte[] unencrypted) => - ProtectedData.Protect(unencrypted, Value, DataProtectionScope.CurrentUser); + Crypto.Encrypt(unencrypted, Value); public static byte[] DecryptLocal(byte[] encrypted) => - ProtectedData.Unprotect(encrypted, Value, DataProtectionScope.CurrentUser); + Crypto.Decrypt(encrypted, Value); } } \ No newline at end of file