public static Task<IAsymmetricKey> LoadKey(string path) { if (path == null) throw new ArgumentNullException ("path"); return Task<IAsymmetricKey>.Factory.StartNew (() => { if (!File.Exists (path) || new FileInfo (path).Length == 0) { var crypto = new RSACrypto(); var key = crypto.ExportKey (includePrivate: true); Directory.CreateDirectory (Path.GetDirectoryName (path)); using (var fstream = File.OpenWrite (path)) { var writer = new StreamValueWriter (fstream); key.Serialize (null, writer); } return key; } var reader = new BufferValueReader (File.ReadAllBytes (path)); return new RSAAsymmetricKey (null, reader); }); }
public void Serialize(IValueWriter writer, RSACrypto crypto) { if (!writer.WriteBool (this.publicKey != null)) return; writer.WriteBytes (crypto.Encrypt (this.Exponent)); int first = this.Modulus.Length / 2; writer.WriteBytes (crypto.Encrypt (this.Modulus.Copy (0, first))); writer.WriteBytes (crypto.Encrypt (this.Modulus.Copy (first, this.Modulus.Length - first))); }
public void Deserialize(IValueReader reader, RSACrypto crypto) { if (reader == null) throw new ArgumentNullException ("reader"); if (crypto == null) throw new ArgumentNullException ("crypto"); if (reader.ReadBool()) { byte[] exponent = crypto.Decrypt (reader.ReadBytes()); byte[] modulus1 = crypto.Decrypt (reader.ReadBytes()); byte[] modulus2 = crypto.Decrypt (reader.ReadBytes()); byte[] modulus = modulus1.Concat (modulus2).ToArray(); this.exponentOffset = modulus.Length; this.publicKey = new byte[exponent.Length + modulus.Length]; Buffer.BlockCopy (modulus, 0, this.publicKey, 0, modulus.Length); Buffer.BlockCopy (exponent, 0, this.publicKey, exponentOffset, exponent.Length); } SetupSignature(); }
public static Task StartAsync() { return Task.Run (() => { var crypto = new RSACrypto(); return crypto.ExportKey (true); }).ContinueWith (t => StartAsync (t.Result), TaskScheduler.Default).Unwrap(); }