Library Move

This push is to move the encryption code to a new library to help manage encryption across projects easier.
This commit is contained in:
JacobTech 2023-05-13 11:45:54 -04:00
parent 01e42af1f8
commit a27144d54a
10 changed files with 16 additions and 246 deletions

View File

@ -114,87 +114,13 @@ public static class Luski
public static class Encryption
{
public class AES
{
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 byte[] CalculateHash(string text, byte[] salt, int iteration)
{
Rfc2898DeriveBytes? pbkdf2 = new(text, salt, iteration);
return pbkdf2.GetBytes(64);
}
public static byte[] Hash(byte[] data, byte[]? salt = null)
{
using SHA256 sha = SHA256.Create();
if (salt is null) return sha.ComputeHash(data);
else return sha.ComputeHash(Combine(data, salt));
}
internal const int PasswordVersion = 0;
internal static byte[] RemotePasswordEncrypt(string Base64Password, byte[] salt, int PasswordVersion = PasswordVersion)
{
return PasswordVersion switch
{
0 => Hash(Decrypt(Convert.FromBase64String(Base64Password), Keys.PrivateKey), salt),
0 => JacobTechEncryption.Encryption.Hashing.SHA256(JacobTechEncryption.Encryption.RSA.Decrypt(Convert.FromBase64String(Base64Password), Keys.PrivateKey), salt),
_ => throw new ArgumentException("The value provided was not accepted", nameof(PasswordVersion)),
};
}
@ -225,6 +151,7 @@ public static class Luski
{
get
{
//This will not stay like this for long so it's not an exploit, lol
return "<RSAKeyValue><Modulus>2pR655ycDWZuQiPWlcpvloFo2lmcxZJ88nlwNANOri8y+jCrqYvJLLjHW/PHBzDSx/wHEL6GYm/FIPdywgcHzi1a5tuDcdRBrKKqQU/mTW3z2d38H+BJlUCQT/Dai5SXX52p6ZSQBj2Tb4cHZSv3/6vLh1dtJdHYW+RlnadGCKoLJNBF/FQFPBgRkuP69XfAnyM8RTLzzQAcwdmw00pHOdoYV0Ue70MOF3Vv1InnxmB4uOPW6MK45NPH/1u1NkVcp3OZWRAv3xJHfS5CAwtDddnxe4a9f/grvV0lxx4ZCYRLgoe5Lv+0NVIcL+oXdJUI9geyZpfxDpdjdPRc8IA+t+0yyUHTEjyQ3focmFc8fNkmiaxji5sNvG1pWq8Q8MaqgIk1gjRSKJTnWU9N54BMB4pYgRX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ii7DGbYTrvzup9HzPCyVbBwrslvhHDnrrBXs+EjSVcRigSc9t5Q3TKtN4WsUsZef70JcqOOj9DmQUTsg2YXp7X23y9wKHX9jgbYNBIMSjoU5z1mvKfOOXjZeGefFhlCTG5ykirHJ342eUe4jw9pWJ/8heEuonU4heJQTY1M417f4NlzeYiwyZDygGJi/7QgC1DFrxOrxS9H2GYW3pHSQrCvvOPkBgTSy42/hrOG0DmrBKRlW7wbKbw4aEiLpIPKDGS07X9Udzgc3SLA28/DB9AdX6qZSKEmf33LwK47wLEZkiLbU/IO/kb0MpSpl77TrMDrl1J+3Kgcmz7q9eF83W9519/uZgZpOSO/a/97oMKZSfLeyASVP7K1EcqK23K/wjvaP3nvSMGsiQmqCn4jItgfH5N/tcnkNz2DKz3BPcfgAClVH1MFsjDlBqYSHXIqwAFQ8SDBixrPoLoJDkygFoNIrrXqbArVYqXOAgODo2t0foENa/msPJyZ6m6UPi3bMY3gji5qjaSEcvbBmhlSVyHmumgOJ5Rl+L5dS5jAyPgybcVYKab71XQf5sm1UhA8iFWqYgyglmR/b6F9UTFi1QwZA6rgvELFv7DvqjLbmM=</D></RSAKeyValue>";
}
}
@ -245,148 +172,5 @@ public static class Luski
}
}
}
public static readonly UnicodeEncoding _encoder = new();
public static byte[] Decrypt(byte[] data, bool multithread = false)
{
return Decrypt(data, Keys.PrivateKey, multithread);
}
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 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 = Combine(datasplitout, data);
}
}
else
{
for (int i = 0; i < bbb; i++)
{
datasplitout = Combine(datasplitout, rsa.Decrypt(EncryptedText.Skip(i * size).Take(size).ToArray(), false));
}
}
return datasplitout;
/*
using (RSACryptoServiceProvider? rsa = new())
{
rsa.ImportParameters(Key);
double x = ((double)EncryptedText.Length / (double)512);
int bbb = int.Parse(x.ToString().Split('.')[0]);
if (x.ToString().Contains('.')) bbb++;
byte[][] datasplit = Array.Empty<byte[]>();
byte[] datasplitout = Array.Empty<byte>();
Array.Resize(ref datasplit, bbb);
for (int i = 0; i < bbb; i++)
{
byte[] fff = EncryptedText.Skip(i * 512).Take(512).ToArray();
datasplit[i] = fff;
datasplitout = Combine(datasplitout, rsa.Decrypt(datasplit[i], false));
}
return datasplitout;
}*/
}
public static byte[] Encrypt(string Text, bool multithread = false)
{
using RSACryptoServiceProvider rsa = new();
rsa.FromXmlString(Keys.PublicKey);
return Encrypt(_encoder.GetBytes(Text), rsa.ExportParameters(false), multithread);
}
public static byte[] Encrypt(string Text, string key, bool multithread = false)
{
using RSACryptoServiceProvider rsa = new();
rsa.FromXmlString(key);
return Encrypt(_encoder.GetBytes(Text), rsa.ExportParameters(false), 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);
}
private 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 byte[] Encrypt(string Text, RSAParameters Key, bool multithread = false)
{
return Encrypt(_encoder.GetBytes(Text), Key, 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 = Combine(datasplitout, dataa);
}
}
else
{
for (int i = 0; i < bbb; i++)
{
datasplitout = Combine(datasplitout, rsa.Encrypt(data.Skip(i * size).Take(size).ToArray(), false));
}
}
return datasplitout;
}
}
}

View File

@ -1,3 +1,4 @@
using JacobTechEncryption.Enums;
using LuskiServer.Enums;
using ServerDatabase;

View File

@ -1,4 +1,4 @@
using LuskiServer.Enums;
using JacobTechEncryption.Enums;
using ServerDatabase;
namespace LuskiServer.Classes.TableDef;

View File

@ -1,4 +1,4 @@
using LuskiServer.Enums;
using JacobTechEncryption.Enums;
using ServerDatabase;
namespace LuskiServer.Classes.TableDef;

View File

@ -2,6 +2,8 @@ using System.Net.Mime;
using System.Security.Cryptography;
using System.Text;
using Asp.Versioning;
using JacobTechEncryption;
using JacobTechEncryption.Enums;
using LuskiServer.Classes;
using LuskiServer.Classes.TableDef;
using LuskiServer.Enums;
@ -44,12 +46,12 @@ public class CreateAccountController : ControllerBase
}
try
{
PasBytes = Luski.Encryption.RemotePasswordEncrypt(Luski.Encryption._encoder.GetString(Luski.Encryption.Decrypt(Convert.FromBase64String(PasRaw))), salt);
Username = Luski.Encryption.Decrypt(Convert.FromBase64String(UsernameRaw));
PasBytes = Luski.Encryption.RemotePasswordEncrypt(Encryption.Generic.Encoders[(int)EncoderType.UTF16].GetString(Encryption.RSA.Decrypt(Convert.FromBase64String(PasRaw), Luski.Encryption.Keys.PrivateKey)), salt);
Username = Encryption.RSA.Decrypt(Convert.FromBase64String(UsernameRaw), Luski.Encryption.Keys.PrivateKey);
try
{
byte[] g = Luski.Encryption.Encrypt("Test data to send to client", KeyRaw);
byte[] g = Encryption.RSA.Encrypt("Test data to send to client", KeyRaw, EncoderType.UTF8);
}
catch
{

View File

@ -1,6 +1,8 @@
using System.Net.Mime;
using System.Text;
using Asp.Versioning;
using JacobTechEncryption;
using JacobTechEncryption.Enums;
using LuskiServer.Classes;
using LuskiServer.Classes.TableDef;
using LuskiServer.Enums;
@ -48,7 +50,7 @@ public class KeysController : ControllerBase
try
{
byte[] g = Luski.Encryption.Encrypt("Test data to send to client", key);
byte[] g = Encryption.RSA.Encrypt("Test data to send to client", key, EncoderType.UTF8);
}
catch
{

View File

@ -1,12 +0,0 @@
namespace LuskiServer.Enums;
public enum EncoderType : short
{
UTF8 = 0,
UTF16 = 1,
UTF32 = 2,
ASCII = 3,
Latin1 = 4,
Unicode = 5,
BigEndianUnicode = 6
}

View File

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

View File

@ -19,6 +19,7 @@
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0" />
<PackageReference Include="JacobTechEncryption" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="ServerDatabase" Version="2.5.4" />

View File

@ -1,6 +1,7 @@
using System.Reflection;
using System.Text;
using Asp.Versioning.ApiExplorer;
using JacobTechEncryption.Enums;
using LuskiServer;
using LuskiServer.Classes;
using LuskiServer.Classes.TableDef;
@ -114,8 +115,7 @@ if (!Tables.Categories.TryRead(Categories.ID, out _, Categories.ID.CreateParamet
{
EncoderType.UTF8, EncoderType.UTF16,
EncoderType.UTF32, EncoderType.ASCII,
EncoderType.Latin1, EncoderType.Unicode,
EncoderType.BigEndianUnicode
EncoderType.Latin1, EncoderType.BigEndianUnicode
}));
}