From f953fd4bb28c12fe16da56087be935db70a82abd Mon Sep 17 00:00:00 2001 From: JacobTech Date: Sat, 13 May 2023 11:41:09 -0400 Subject: [PATCH] Main This push contains the default code for encryption. --- JacobTechEncryption/Class1.cs | 5 - JacobTechEncryption/Encryption.cs | 301 ++++++++++++++++++ JacobTechEncryption/Enums/EncoderType.cs | 11 + JacobTechEncryption/Enums/EncryptionType.cs | 8 + .../JacobTechEncryption.csproj | 9 + 5 files changed, 329 insertions(+), 5 deletions(-) delete mode 100644 JacobTechEncryption/Class1.cs create mode 100644 JacobTechEncryption/Encryption.cs create mode 100644 JacobTechEncryption/Enums/EncoderType.cs create mode 100644 JacobTechEncryption/Enums/EncryptionType.cs diff --git a/JacobTechEncryption/Class1.cs b/JacobTechEncryption/Class1.cs deleted file mode 100644 index 0f2a761..0000000 --- a/JacobTechEncryption/Class1.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace JacobTechEncryption; - -public class Class1 -{ -} \ No newline at end of file diff --git a/JacobTechEncryption/Encryption.cs b/JacobTechEncryption/Encryption.cs new file mode 100644 index 0000000..ce02cfa --- /dev/null +++ b/JacobTechEncryption/Encryption.cs @@ -0,0 +1,301 @@ +using System.Security.Cryptography; +using System.Text; +using JacobTechEncryption.Enums; + +namespace JacobTechEncryption; + +public class Encryption +{ + public static class Generic + { + public static List Encoders = new() + { + Encoding.UTF8, + Encoding.Unicode, + Encoding.UTF32, + Encoding.ASCII, + Encoding.Latin1, + Encoding.BigEndianUnicode + }; + + internal static byte[] Combine(byte[] first, byte[] second) + { + byte[] bytes = new byte[first.Length + second.Length]; + Buffer.BlockCopy(first, 0, bytes, 0, first.Length); + Buffer.BlockCopy(second, 0, bytes, first.Length, second.Length); + return bytes; + } + } + + public static class AES + { + public static string EncryptFile(string path, string Password) + { + string p = Path.GetTempFileName(); + byte[] salt = RandomNumberGenerator.GetBytes(100); + byte[] passwordBytes = Encoding.UTF8.GetBytes(Password); + Rfc2898DeriveBytes key = new(passwordBytes, salt, 50000); + byte[] data = System.IO.File.ReadAllBytes(path); + + using Aes aesAlg = Aes.Create(); + aesAlg.KeySize = 256; + aesAlg.BlockSize = 128; + aesAlg.Padding = PaddingMode.PKCS7; + aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); + aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); + + ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); + + using FileStream msEncrypt = new(p, FileMode.Open); + msEncrypt.Write(salt, 0, salt.Length); + using CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write); + csEncrypt.Write(data, 0, data.Length); + csEncrypt.Dispose(); + msEncrypt.Dispose(); + return p; + } + + public static void DecryptToFile(Stream fsCrypt, string Password, string File) + { + byte[] salt = new byte[100]; + fsCrypt.Read(salt, 0, salt.Length); + byte[] passwordBytes = Encoding.UTF8.GetBytes(Password); + Rfc2898DeriveBytes key = new(passwordBytes, salt, 50000); + byte[] decrypted = new byte[fsCrypt.Length - salt.Length]; + + using Aes aesAlg = Aes.Create(); + aesAlg.KeySize = 256; + aesAlg.BlockSize = 128; + aesAlg.Padding = PaddingMode.PKCS7; + aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); + aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); + + ICryptoTransform encryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); + + using CryptoStream csEncrypt = new(fsCrypt, encryptor, CryptoStreamMode.Read); + FileStream fsOut = new(File, FileMode.Create); + int read; + byte[] buffer = new byte[fsCrypt.Length]; + while ((read = csEncrypt.Read(buffer, 0, buffer.Length)) > 0) + { + fsOut.Write(buffer, 0, read); + } + csEncrypt.Dispose(); + fsCrypt.Dispose(); + fsOut.Dispose(); + } + + public static void DecryptToFile(byte[] data, string Password, string File) + { + byte[] salt = new byte[100]; + using MemoryStream fsCrypt = new(data); + fsCrypt.Read(salt, 0, salt.Length); + byte[] passwordBytes = Encoding.UTF8.GetBytes(Password); + Rfc2898DeriveBytes key = new(passwordBytes, salt, 50000); + byte[] decrypted = new byte[data.Length - salt.Length]; + + using Aes aesAlg = Aes.Create(); + aesAlg.KeySize = 256; + aesAlg.BlockSize = 128; + aesAlg.Padding = PaddingMode.PKCS7; + aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); + aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); + + ICryptoTransform encryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); + + using CryptoStream csEncrypt = new(fsCrypt, encryptor, CryptoStreamMode.Read); + FileStream fsOut = new(File, FileMode.Create); + int read; + byte[] buffer = new byte[data.Length]; + while ((read = csEncrypt.Read(buffer, 0, buffer.Length)) > 0) + { + fsOut.Write(buffer, 0, read); + } + csEncrypt.Dispose(); + fsCrypt.Dispose(); + fsOut.Dispose(); + } + + public static byte[] Encrypt(byte[] data, string Password) + { + byte[] salt = RandomNumberGenerator.GetBytes(100); + byte[] passwordBytes = Encoding.UTF8.GetBytes(Password); + Rfc2898DeriveBytes key = new(passwordBytes, salt, 50000); + byte[] encrypted; + + using Aes aesAlg = Aes.Create(); + aesAlg.KeySize = 256; + aesAlg.BlockSize = 128; + aesAlg.Padding = PaddingMode.PKCS7; + aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); + aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); + + ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); + + using MemoryStream msEncrypt = new(); + msEncrypt.Write(salt, 0, salt.Length); + using CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write); + csEncrypt.Write(data, 0, data.Length); + csEncrypt.Dispose(); + encrypted = msEncrypt.ToArray(); + return encrypted; + } + + public static byte[] Decrypt(byte[] data, string Password) + { + byte[] salt = new byte[100]; + using MemoryStream fsCrypt = new(data); + fsCrypt.Read(salt, 0, salt.Length); + byte[] passwordBytes = Encoding.UTF8.GetBytes(Password); + Rfc2898DeriveBytes key = new(passwordBytes, salt, 50000); + byte[] decrypted = new byte[data.Length - salt.Length]; + + using Aes aesAlg = Aes.Create(); + aesAlg.KeySize = 256; + aesAlg.BlockSize = 128; + aesAlg.Padding = PaddingMode.PKCS7; + aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); + aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); + + ICryptoTransform encryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); + + using CryptoStream csEncrypt = new(fsCrypt, encryptor, CryptoStreamMode.Read); + MemoryStream fsOut = new(); + int read; + byte[] buffer = new byte[data.Length]; + while ((read = csEncrypt.Read(buffer, 0, buffer.Length)) > 0) + { + fsOut.Write(buffer, 0, read); + } + csEncrypt.Dispose(); + fsCrypt.Dispose(); + decrypted = fsOut.ToArray(); + fsOut.Dispose(); + return decrypted; + } + } + + public static class Hashing + { + public static byte[] SHA256(byte[] data, byte[]? salt = null) + { + using SHA256 sha = System.Security.Cryptography.SHA256.Create(); + if (salt is null) return sha.ComputeHash(data); + else return sha.ComputeHash(Generic.Combine(data, salt)); + } + } + + public static class RSA + { + public static byte[] Encrypt(string Text, RSAParameters Key, EncoderType EncoderType, bool multithread = false) + { + return Encrypt(Generic.Encoders[(int)EncoderType].GetBytes(Text), Key, multithread); + } + + public static byte[] Encrypt(string Text, string Key, EncoderType EncoderType, bool multithread = false) + { + return Encrypt(Generic.Encoders[(int)EncoderType].GetBytes(Text), Key, multithread); + } + + public static byte[] Encrypt(byte[] data, string key, bool multithread = false) + { + using RSACryptoServiceProvider rsa = new(); + rsa.FromXmlString(key); + return Encrypt(data, rsa.ExportParameters(false), multithread); + } + + public static byte[] Encrypt(byte[] data, RSAParameters Key, bool multithread = false) + { + using RSACryptoServiceProvider rsa = new(); + rsa.ImportParameters(Key); + int size = rsa.KeySize / 8; + double x = data.Length / (double)size; + int bbb = int.Parse(x.ToString().Split('.')[0]); + if (x.ToString().Contains('.')) bbb++; + byte[]? datasplitout = Array.Empty(); + if (multithread) + { + byte[][]? decccc = Array.Empty(); + Array.Resize(ref decccc, bbb); + int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 25) * 2.0)); + if (num == 0) num = 1; + Parallel.For(0, bbb, new ParallelOptions() + { + MaxDegreeOfParallelism = num + }, i => + { + decccc[i] = rsa.Encrypt(data.Skip(i * size).Take(size).ToArray(), false); + }); + foreach (byte[] dataa in decccc) + { + datasplitout = Generic.Combine(datasplitout, dataa); + } + } + else + { + for (int i = 0; i < bbb; i++) + { + datasplitout = Generic.Combine(datasplitout, rsa.Encrypt(data.Skip(i * size).Take(size).ToArray(), false)); + } + } + return datasplitout; + } + + public static byte[] Decrypt(byte[] EncryptedText, RSAParameters Key, bool multithread = false) + { + if (EncryptedText is null) throw new ArgumentNullException(nameof(EncryptedText)); + using RSACryptoServiceProvider rsa = new(); + rsa.ImportParameters(Key); + int size = rsa.KeySize / 8; + double x = EncryptedText.Length / (double)size; + int bbb = int.Parse(x.ToString().Split('.')[0]); + if (x.ToString().Contains('.')) bbb++; + byte[]? datasplitout = Array.Empty(); + if (multithread) + { + byte[][]? decccc = Array.Empty(); + Array.Resize(ref decccc, bbb); + int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 25) * 2.0)); + if (num == 0) num = 1; + Parallel.For(0, bbb, new ParallelOptions() + { + MaxDegreeOfParallelism = num + }, i => + { + decccc[i] = rsa.Decrypt(EncryptedText.Skip(i * size).Take(size).ToArray(), false); + }); + foreach (byte[] data in decccc) + { + datasplitout = Generic.Combine(datasplitout, data); + } + } + else + { + for (int i = 0; i < bbb; i++) + { + datasplitout = Generic.Combine(datasplitout, rsa.Decrypt(EncryptedText.Skip(i * size).Take(size).ToArray(), false)); + } + } + return datasplitout; + } + + public static byte[] Decrypt(byte[] data, string Key, bool multithread = false) + { + using RSACryptoServiceProvider rsa = new(); + rsa.FromXmlString(Key); + return Decrypt(data, rsa.ExportParameters(true), multithread); + } + + public static string Decrypt(byte[] data, string Key, EncoderType EncoderType, bool multithread = false) + { + using RSACryptoServiceProvider rsa = new(); + rsa.FromXmlString(Key); + return Generic.Encoders[(int)EncoderType].GetString(Decrypt(data, rsa.ExportParameters(true), multithread)); + } + + public static string Decrypt(byte[] data, RSAParameters Key, EncoderType EncoderType, bool multithread = false) + { + return Generic.Encoders[(int)EncoderType].GetString(Decrypt(data, Key, multithread)); + } + } +} \ No newline at end of file diff --git a/JacobTechEncryption/Enums/EncoderType.cs b/JacobTechEncryption/Enums/EncoderType.cs new file mode 100644 index 0000000..73bc131 --- /dev/null +++ b/JacobTechEncryption/Enums/EncoderType.cs @@ -0,0 +1,11 @@ +namespace JacobTechEncryption.Enums; + +public enum EncoderType +{ + UTF8 = 0, + UTF16 = 1, + UTF32 = 2, + ASCII = 3, + Latin1 = 4, + BigEndianUnicode = 5 +} \ No newline at end of file diff --git a/JacobTechEncryption/Enums/EncryptionType.cs b/JacobTechEncryption/Enums/EncryptionType.cs new file mode 100644 index 0000000..badf3ea --- /dev/null +++ b/JacobTechEncryption/Enums/EncryptionType.cs @@ -0,0 +1,8 @@ +namespace JacobTechEncryption.Enums; + +public enum EncryptionType : short +{ + None = 0, + RSA = 1, + AES = 2, +} \ No newline at end of file diff --git a/JacobTechEncryption/JacobTechEncryption.csproj b/JacobTechEncryption/JacobTechEncryption.csproj index eb2460e..5631130 100644 --- a/JacobTechEncryption/JacobTechEncryption.csproj +++ b/JacobTechEncryption/JacobTechEncryption.csproj @@ -4,6 +4,15 @@ net6.0 enable enable + JacobTechEncryption + JacobTech + https://git.jacobtech.com/JacobTech.com/JacobTechEncryption + git + + + + +