public LocalPlayerProfile(string filePath, PlayerDatabase playerDatabase) { this.filePath = filePath; this.playerDatabase = playerDatabase; innerState = LinkState.Uninitialized; try { if (File.Exists(filePath)) { using (var rsa = new RSACryptoServiceProvider()) { using (var data = File.OpenRead(filePath)) { var keyData = Convert.FromBase64String(data.ReadAllText()); rsa.FromXmlString(new string(Encoding.ASCII.GetChars(keyData))); } parameters = rsa.ExportParameters(true); innerPublicKey = CryptoUtil.EncodePEMPublicKey(parameters); innerFingerprint = CryptoUtil.PublicKeyFingerprint(parameters); innerState = LinkState.Unlinked; } } } catch (Exception e) { Console.WriteLine("Failed to load keys: {0}", e); Log.Write("debug", "Failed to load player keypair from `{0}` with exception: {1}", filePath, e); } }
public void GenerateKeypair() { if (State != LinkState.Uninitialized) { return; } innerState = LinkState.GeneratingKeys; new Task(() => { try { var rsa = new RSACryptoServiceProvider(AuthKeySize); parameters = rsa.ExportParameters(true); innerPublicKey = CryptoUtil.EncodePEMPublicKey(parameters); innerFingerprint = CryptoUtil.PublicKeyFingerprint(parameters); var data = Convert.ToBase64String(Encoding.ASCII.GetBytes(rsa.ToXmlString(true))); File.WriteAllText(filePath, data); innerState = LinkState.Unlinked; } catch (Exception e) { Log.Write("debug", "Failed to generate keypair with exception: {1}", e); Console.WriteLine("Key generation failed: {0}", e); innerState = LinkState.Uninitialized; } }).Start(); }