using System.Security.Cryptography; namespace RsaKeyLoader; public class KeyLoader { public static string PathString = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "auth-key"); public static RSA GetGeneratedKey() { if (File.Exists(PathString)) { return LoadKey(PathString); } else { Generate(PathString); return LoadKey(PathString); } } public static void Generate(string path) { var rsaKey = RSA.Create(); var privateKey = rsaKey.ExportRSAPrivateKey(); File.WriteAllBytes(path, privateKey); } public static RSA LoadKey(string path) { var rsaKey = RSA.Create(); rsaKey.ImportRSAPrivateKey(File.ReadAllBytes(path), out _); return rsaKey; } }