public IContentResponse HandlePasswordSet(NameValueCollection headers, Stream inputStream) { INameValueStore store = new RegistryStorage(Settings.RegistryPath); int contentLen = int.Parse(headers["Content-Length"]); if (contentLen == 0) { using (RSAPrivateKey _temporaryKey = new RSAPrivateKey(2048)) { store.Write("Transfers", "temp-key", Convert.ToBase64String(_temporaryKey.ToArray())); return(new DynamicResponse("application/public-key", _temporaryKey.PublicKey.ToArray())); } } string tempkey; if (contentLen <= 2048 && store.Read("Transfers", "temp-key", out tempkey)) { byte[] bytes = IOStream.Read(inputStream, contentLen); using (RSAPrivateKey _temporaryKey = RSAPrivateKey.FromBytes(Convert.FromBase64String(tempkey))) bytes = _temporaryKey.Decrypt(bytes); _content.KeyPair.SetServerPassword(bytes); } return(DynamicResponse.Empty); }
public void SetPassword(byte[] passbytes) { Check.ArraySize(passbytes, 1, 2048); Check.Assert <InvalidOperationException>(_passwordRequired && _privateBits != null && _privateKey == null); try { byte[] keybytes; using (Password pwd = new Password(false, passbytes)) { pwd.IV = IV.ToByteArray(); keybytes = pwd.Decrypt(_privateBits, Salt.Size.b256); } Check.Assert <InvalidDataException>(_privateHash.Equals(Hash.SHA256(keybytes))); _privateKey = RSAPrivateKey.FromBytes(keybytes); Check.Assert <InvalidDataException>(_publicHash.Equals(Hash.SHA256(_privateKey.PublicKey.ToArray()))); passbytes = Encryption.CurrentUser.Encrypt(passbytes); RegistryStorage store = new RegistryStorage(Settings.RegistryPath); store.Write("CryptoKey", _privateHash.ToString(), Convert.ToBase64String(passbytes)); } catch (Exception err) { Log.Error(err); throw new InvalidDataException(); } }
public void TestPrivateKeyExport() { RSAPrivateKey pk = new RSAPrivateKey(); string xml = pk.ToXml(); RSAPrivateKey copy = RSAPrivateKey.FromXml(xml); Assert.AreEqual(xml, copy.ToXml()); byte[] bytes = pk.ToArray(); Assert.AreEqual(596, bytes.Length); copy = RSAPrivateKey.FromBytes(bytes); Assert.AreEqual(bytes, copy.ToArray()); copy = RSAPrivateKey.FromParameters(pk.ExportParameters()); Assert.AreEqual(bytes, copy.ToArray()); }