mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-08 23:41:57 +02:00
Move AES GCM crypto and PBKDF2 key derivation to Cryptography folder
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
using Org.BouncyCastle.Crypto.Engines;
|
||||||
|
using Org.BouncyCastle.Crypto.Modes;
|
||||||
|
using Org.BouncyCastle.Crypto.Parameters;
|
||||||
|
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace RemSox.Cryptography
|
||||||
|
{
|
||||||
|
public static class AesGcmCrypto
|
||||||
|
{
|
||||||
|
private const int NonceSize = 12;
|
||||||
|
private const int TagSize = 128;
|
||||||
|
|
||||||
|
public static byte[] Encrypt(byte[] plainData, byte[] key)
|
||||||
|
{
|
||||||
|
byte[] nonce = new byte[NonceSize];
|
||||||
|
RandomNumberGenerator.Fill(nonce);
|
||||||
|
|
||||||
|
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
|
||||||
|
cipher.Init(true, new AeadParameters(new KeyParameter(key), TagSize, nonce));
|
||||||
|
|
||||||
|
byte[] output = new byte[cipher.GetOutputSize(plainData.Length)];
|
||||||
|
|
||||||
|
int len = cipher.ProcessBytes(plainData, 0, plainData.Length, output, 0);
|
||||||
|
_ = cipher.DoFinal(output, len);
|
||||||
|
|
||||||
|
byte[] result = new byte[nonce.Length + output.Length];
|
||||||
|
|
||||||
|
Buffer.BlockCopy(nonce, 0, result, 0, nonce.Length);
|
||||||
|
Buffer.BlockCopy(output, 0, result, nonce.Length, output.Length);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Encrypt(string text, byte[] key)
|
||||||
|
{
|
||||||
|
byte[] data = Encoding.UTF8.GetBytes(text);
|
||||||
|
return Convert.ToBase64String(Encrypt(data, key));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] Decrypt(byte[] encryptedData, byte[] key)
|
||||||
|
{
|
||||||
|
byte[] nonce = new byte[NonceSize];
|
||||||
|
Buffer.BlockCopy(encryptedData, 0, nonce, 0, NonceSize);
|
||||||
|
|
||||||
|
int cipherLength = encryptedData.Length - NonceSize;
|
||||||
|
byte[] cipherBytes = new byte[cipherLength];
|
||||||
|
|
||||||
|
Buffer.BlockCopy(encryptedData, NonceSize, cipherBytes, 0, cipherLength);
|
||||||
|
|
||||||
|
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
|
||||||
|
cipher.Init(false, new AeadParameters(new KeyParameter(key), TagSize, nonce));
|
||||||
|
|
||||||
|
byte[] plain = new byte[cipher.GetOutputSize(cipherBytes.Length)];
|
||||||
|
|
||||||
|
int len = cipher.ProcessBytes(cipherBytes, 0, cipherBytes.Length, plain, 0);
|
||||||
|
_ = cipher.DoFinal(plain, len);
|
||||||
|
|
||||||
|
return plain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Decrypt(string cipherText, byte[] key)
|
||||||
|
{
|
||||||
|
byte[] data = Convert.FromBase64String(cipherText);
|
||||||
|
byte[] plain = Decrypt(data, key);
|
||||||
|
return Encoding.UTF8.GetString(plain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Cosmos.Kernel.System.Keyboard;
|
||||||
|
using Cosmos.Kernel.System.Mouse;
|
||||||
|
using RemSox.Plugs;
|
||||||
|
|
||||||
|
namespace RemSox.Cryptography;
|
||||||
|
|
||||||
|
public static class CryptoManager
|
||||||
|
{
|
||||||
|
private static Point lastMousePosition = new Point(0, 0);
|
||||||
|
|
||||||
|
public static void Update()
|
||||||
|
{
|
||||||
|
int x = MouseManager.X;
|
||||||
|
int y = MouseManager.Y;
|
||||||
|
int dx = x - lastMousePosition.X;
|
||||||
|
int dy = y - lastMousePosition.Y;
|
||||||
|
int dz = MouseManager.ScrollDelta;
|
||||||
|
|
||||||
|
lastMousePosition = new Point(x, y);
|
||||||
|
|
||||||
|
RandomNumberGeneratorImplementationImpl.AddMouseEntropy(dx, dy, dz, x, y);
|
||||||
|
|
||||||
|
if (!KeyboardManager.KeyAvailable) return;
|
||||||
|
|
||||||
|
KeyEvent keyEvent = KeyboardManager.Peek();
|
||||||
|
|
||||||
|
RandomNumberGeneratorImplementationImpl.AddKeyboardEntropy((uint)keyEvent.Key, (uint)keyEvent.Modifiers, keyEvent.KeyChar);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using Org.BouncyCastle.Crypto;
|
||||||
|
using Org.BouncyCastle.Crypto.Digests;
|
||||||
|
using Org.BouncyCastle.Crypto.Generators;
|
||||||
|
using Org.BouncyCastle.Crypto.Parameters;
|
||||||
|
|
||||||
|
namespace RemSox.Cryptography
|
||||||
|
{
|
||||||
|
public static class Pkcs5S2PasswordKeyDeriver
|
||||||
|
{
|
||||||
|
private const int KeySize = 32; // 256-bit key
|
||||||
|
private const int Iterations = 150_000; // PBKDF2 cost factor
|
||||||
|
|
||||||
|
public static byte[] DeriveKey(string password, byte[] salt)
|
||||||
|
{
|
||||||
|
Pkcs5S2ParametersGenerator generator = new Pkcs5S2ParametersGenerator(new Sha256Digest());
|
||||||
|
|
||||||
|
generator.Init(
|
||||||
|
PbeParametersGenerator.Pkcs5PasswordToBytes(password.ToCharArray()),
|
||||||
|
salt,
|
||||||
|
Iterations
|
||||||
|
);
|
||||||
|
|
||||||
|
return ((KeyParameter)generator.GenerateDerivedMacParameters(KeySize * 8))
|
||||||
|
.GetKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
global using Sys = Cosmos.Kernel.System;
|
global using Sys = Cosmos.Kernel.System;
|
||||||
|
using RemSox.Cryptography;
|
||||||
using RemSox.Processes;
|
using RemSox.Processes;
|
||||||
using RemSox.Processing;
|
using RemSox.Processing;
|
||||||
using RemSox.UI.CLI;
|
using RemSox.UI.CLI;
|
||||||
@@ -55,6 +55,7 @@ public class Kernel : Sys.Kernel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CryptoManager.Update();
|
||||||
ProcessManager.TickAllProcesses();
|
ProcessManager.TickAllProcesses();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using RemSox.Utils;
|
using RemSox.Cryptography;
|
||||||
|
|
||||||
namespace RemSox.Networking;
|
namespace RemSox.Networking;
|
||||||
|
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
using Org.BouncyCastle.Crypto.Engines;
|
|
||||||
using Org.BouncyCastle.Crypto.Modes;
|
|
||||||
using Org.BouncyCastle.Crypto.Parameters;
|
|
||||||
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace RemSox.Utils;
|
|
||||||
|
|
||||||
public static class AesGcmCrypto
|
|
||||||
{
|
|
||||||
private const int NonceSize = 12;
|
|
||||||
private const int TagSize = 128;
|
|
||||||
|
|
||||||
public static byte[] Encrypt(byte[] plainData, byte[] key)
|
|
||||||
{
|
|
||||||
byte[] nonce = new byte[NonceSize];
|
|
||||||
RandomNumberGenerator.Fill(nonce);
|
|
||||||
|
|
||||||
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
|
|
||||||
cipher.Init(true, new AeadParameters(new KeyParameter(key), TagSize, nonce));
|
|
||||||
|
|
||||||
byte[] output = new byte[cipher.GetOutputSize(plainData.Length)];
|
|
||||||
|
|
||||||
int len = cipher.ProcessBytes(plainData, 0, plainData.Length, output, 0);
|
|
||||||
_ = cipher.DoFinal(output, len);
|
|
||||||
|
|
||||||
byte[] result = new byte[nonce.Length + output.Length];
|
|
||||||
|
|
||||||
Buffer.BlockCopy(nonce, 0, result, 0, nonce.Length);
|
|
||||||
Buffer.BlockCopy(output, 0, result, nonce.Length, output.Length);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Encrypt(string text, byte[] key)
|
|
||||||
{
|
|
||||||
byte[] data = Encoding.UTF8.GetBytes(text);
|
|
||||||
return Convert.ToBase64String(Encrypt(data, key));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] Decrypt(byte[] encryptedData, byte[] key)
|
|
||||||
{
|
|
||||||
byte[] nonce = new byte[NonceSize];
|
|
||||||
Buffer.BlockCopy(encryptedData, 0, nonce, 0, NonceSize);
|
|
||||||
|
|
||||||
int cipherLength = encryptedData.Length - NonceSize;
|
|
||||||
byte[] cipherBytes = new byte[cipherLength];
|
|
||||||
|
|
||||||
Buffer.BlockCopy(encryptedData, NonceSize, cipherBytes, 0, cipherLength);
|
|
||||||
|
|
||||||
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
|
|
||||||
cipher.Init(false, new AeadParameters(new KeyParameter(key), TagSize, nonce));
|
|
||||||
|
|
||||||
byte[] plain = new byte[cipher.GetOutputSize(cipherBytes.Length)];
|
|
||||||
|
|
||||||
int len = cipher.ProcessBytes(cipherBytes, 0, cipherBytes.Length, plain, 0);
|
|
||||||
_ = cipher.DoFinal(plain, len);
|
|
||||||
|
|
||||||
return plain;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Decrypt(string cipherText, byte[] key)
|
|
||||||
{
|
|
||||||
byte[] data = Convert.FromBase64String(cipherText);
|
|
||||||
byte[] plain = Decrypt(data, key);
|
|
||||||
return Encoding.UTF8.GetString(plain);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
using Org.BouncyCastle.Crypto;
|
|
||||||
using Org.BouncyCastle.Crypto.Digests;
|
|
||||||
using Org.BouncyCastle.Crypto.Generators;
|
|
||||||
using Org.BouncyCastle.Crypto.Parameters;
|
|
||||||
|
|
||||||
namespace RemSox.Utils;
|
|
||||||
|
|
||||||
public static class PasswordKeyDeriver
|
|
||||||
{
|
|
||||||
private const int KeySize = 32; // 256-bit key
|
|
||||||
private const int Iterations = 150_000; // PBKDF2 cost factor
|
|
||||||
|
|
||||||
public static byte[] DeriveKey(string password, byte[] salt)
|
|
||||||
{
|
|
||||||
Pkcs5S2ParametersGenerator generator = new Pkcs5S2ParametersGenerator(new Sha256Digest());
|
|
||||||
|
|
||||||
generator.Init(
|
|
||||||
PbeParametersGenerator.Pkcs5PasswordToBytes(password.ToCharArray()),
|
|
||||||
salt,
|
|
||||||
Iterations
|
|
||||||
);
|
|
||||||
|
|
||||||
return ((KeyParameter)generator.GenerateDerivedMacParameters(KeySize * 8))
|
|
||||||
.GetKey();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user