This push contains the default code for encryption.
This commit is contained in:
JacobTech 2023-05-13 11:41:09 -04:00
parent d160e3d93d
commit f953fd4bb2
5 changed files with 329 additions and 5 deletions

View File

@ -1,5 +0,0 @@
namespace JacobTechEncryption;
public class Class1
{
}

View File

@ -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<Encoding> 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<byte>();
if (multithread)
{
byte[][]? decccc = Array.Empty<byte[]>();
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<byte>();
if (multithread)
{
byte[][]? decccc = Array.Empty<byte[]>();
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));
}
}
}

View File

@ -0,0 +1,11 @@
namespace JacobTechEncryption.Enums;
public enum EncoderType
{
UTF8 = 0,
UTF16 = 1,
UTF32 = 2,
ASCII = 3,
Latin1 = 4,
BigEndianUnicode = 5
}

View File

@ -0,0 +1,8 @@
namespace JacobTechEncryption.Enums;
public enum EncryptionType : short
{
None = 0,
RSA = 1,
AES = 2,
}

View File

@ -4,6 +4,15 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Title>JacobTechEncryption</Title>
<Authors>JacobTech</Authors>
<RepositoryUrl>https://git.jacobtech.com/JacobTech.com/JacobTechEncryption</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<Target Name="CustomActionsAfterPublish" AfterTargets="Pack">
<Message Text="Actions AfterPublish: $(PackageId).$(PackageVersion).nupkg" Importance="high" />
<Exec Command="nuget push -Source https://nuget.jacobtech.com/v3/index.json bin/Release/$(PackageId).$(PackageVersion).nupkg" />
</Target>
</Project>