Files
RAGECOOP-V/Client/Scripts/Security.cs
2023-02-13 20:44:50 +08:00

53 lines
1.5 KiB
C#

using System.IO;
using System.Security.Cryptography;
using RageCoop.Core;
namespace RageCoop.Client
{
internal class Security
{
public Security()
{
ClientAes.GenerateKey();
ClientAes.GenerateIV();
}
public RSA ServerRSA { get; set; }
public Aes ClientAes { get; set; } = Aes.Create();
public void GetSymmetricKeysCrypted(out byte[] cryptedKey, out byte[] cryptedIV)
{
// Logger?.Debug($"Aes.Key:{ClientAes.Key.Dump()}, Aes.IV:{ClientAes.IV.Dump()}");
cryptedKey = ServerRSA.Encrypt(ClientAes.Key, RSAEncryptionPadding.Pkcs1);
cryptedIV = ServerRSA.Encrypt(ClientAes.IV, RSAEncryptionPadding.Pkcs1);
}
public byte[] Encrypt(byte[] data)
{
return new CryptoStream(new MemoryStream(data), ClientAes.CreateEncryptor(), CryptoStreamMode.Read)
.ReadToEnd();
}
public byte[] Decrypt(byte[] data)
{
return new CryptoStream(new MemoryStream(data), ClientAes.CreateDecryptor(), CryptoStreamMode.Read)
.ReadToEnd();
}
public void SetServerPublicKey(byte[] modulus, byte[] exponent)
{
var para = new RSAParameters();
para.Modulus = modulus;
para.Exponent = exponent;
ServerRSA = RSA.Create(para);
}
public void Regen()
{
ClientAes = Aes.Create();
ClientAes.GenerateKey();
ClientAes.GenerateIV();
}
}
}