public static ArchiveKeyResult Archive(string ArchiveFile, string Password, FileInfo SecuredKey) { ArchiveKeyResult result = new ArchiveKeyResult { IsInError = false, ArchiveKey = ArchiveFile, SecuredKey = SecuredKey.FullName, Password = Password }; ProtectedKey _securedKey; try { using (FileStream rfs = File.OpenRead(SecuredKey.FullName)) { _securedKey = KeyManager.Read(rfs, DataProtectionScope.LocalMachine); } using (FileStream ofs = File.OpenWrite(ArchiveFile)) { KeyManager.ArchiveKey(ofs, _securedKey, Password); } } catch (Exception ex) { result.IsInError = true; result.ErrorString = ex.Message; result.Exception = ex; } return(result); }
public void SymmetricKeysCanBeRoundTrippedToAndFromArchivalStream() { ProtectedKey keyToArchive = ProtectedKey.CreateFromPlaintextKey(generatedKey, DataProtectionScope.CurrentUser); KeyManager.ArchiveKey(stream, keyToArchive, "passphrase"); stream.Seek(0, SeekOrigin.Begin); ProtectedKey restoredKey = KeyManager.RestoreKey(stream, "passphrase", DataProtectionScope.CurrentUser); AssertHelpers.AssertArraysEqual(keyToArchive.DecryptedKey, restoredKey.DecryptedKey); }
/// <summary> /// Generates a new Cryptographic key from Security.Cryptography. Use to initiate new basis for encrypting objects. /// </summary> /// <param name="NewKeyFile">Name of new keyfile to be generated</param> /// <param name="Password">Password used to secure the new keyfile</param> /// <returns></returns> public static bool Generate(FileInfo NewKeyFile, string Password) { KeyManager.ClearCache(); byte[] managerByteKey = KeyManager.GenerateSymmetricKey("Rijndael"); ProtectedKey newProtectedKey = ProtectedKey.CreateFromPlaintextKey(managerByteKey, DataProtectionScope.LocalMachine); using (FileStream ofs = File.OpenWrite(NewKeyFile.FullName)) { KeyManager.ArchiveKey(ofs, newProtectedKey, Password); } return(true); }
private void btnOk_Click(object sender, EventArgs e) { if (exportKeyControl1.ValidateControl()) { try { using (Stream fileOut = File.OpenWrite(exportKeyControl1.FileName)) { KeyManager.ArchiveKey(fileOut, key, exportKeyControl1.Password); } } catch (Exception) { MessageBox.Show(KeyManagerResources.ErrorExportingKey, KeyManagerResources.ExportDialogErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } } else { this.DialogResult = DialogResult.None; } }