Luski.Net/Luski.net/ServerEncryption.cs

56 lines
1.9 KiB
C#

using System;
using System.Security.Cryptography;
using JacobTechEncryption;
namespace Luski.net;
public class ServerEncryption
{
internal bool Generating, Generated;
internal string ServerPublicKey = "", MyPublicKey = "", myPrivateKey = "", OfflinePrivateKey = "", OfflinePublicKey = "";
internal byte[] Hash = default!;
internal ServerEncryption(string Domain)
{
//TODO Get server p key
}
internal int PasswordVersion = 0;
internal byte[] LocalPasswordEncrypt(byte[] Password) => LocalPasswordEncrypt(Password, PasswordVersion);
internal string RemotePasswordEncrypt(byte[] Password) => RemotePasswordEncrypt(Password, PasswordVersion);
internal byte[] LocalPasswordEncrypt(byte[] Password, int PasswordVersion)
{
return PasswordVersion switch
{
0 => SHA256.Create().ComputeHash(Password),
_ => throw new ArgumentException("The value provided was not accepted", nameof(PasswordVersion)),
};
}
internal string RemotePasswordEncrypt(byte[] Password, int PasswordVersion)
{
return PasswordVersion switch
{
0 => Convert.ToBase64String(Encryption.RSA.Encrypt(LocalPasswordEncrypt(Password, PasswordVersion), ServerPublicKey)),
_ => throw new ArgumentException("The value provided was not accepted", nameof(PasswordVersion)),
};
}
public void GenerateKeys()
{
if (!Generating)
{
Generating = true;
GenerateNewKeys(out MyPublicKey, out myPrivateKey);
GenerateNewKeys(out OfflinePublicKey, out OfflinePrivateKey);
Generated = true;
}
}
public static void GenerateNewKeys(out string Public, out string Private, int KeySize = 4096)
{
using RSACryptoServiceProvider r = new(KeySize);
Private = r.ToXmlString(true);
Public = r.ToXmlString(false);
}
}