bool ImportKey (Config cfg) { string containerName = cfg.ContainerName; string fileName = cfg.FileName; Console.WriteLine ("Importing an RSA key from file '{0}' into the container '{1}'...", fileName, containerName); if (String.IsNullOrEmpty (containerName)) { Failure (cfg, "Unspecified container name."); return true; } if (String.IsNullOrEmpty (fileName)) { Failure (cfg, "Unspecified file name."); return true; } if (!File.Exists (fileName)) { Failure (cfg, "Key file '{0}' does not exist.", fileName); return true; } KeyContainerCollection kcc; Key key; KeyContainer kc; try { kcc = new KeyContainerCollection (cfg.UseMachinePath); kc = kcc [containerName]; if (kc != null) key = kc [0]; else key = null; // No validation is performed on the key - this is left for the // encryption algorithm implementation to do. string keyvalue = File.ReadAllText (fileName); if (key == null) key = new Key (containerName, keyvalue, cfg.UseMachinePath); else { key.KeyValue = keyvalue; key.ContainerName = containerName; } key.Save (); Console.WriteLine ("Success."); } catch (Exception ex) { Failure (ex, cfg); return true; } return false; }
bool CreateKey (Config cfg) { string name = cfg.ContainerName; KeyContainerCollection kc; Console.WriteLine ("Creating RSA key container '{0}'...", name); try { kc = new KeyContainerCollection (cfg.UseMachinePath); if (kc.Contains (name)) { Failure (cfg, "The RSA container already exists."); return true; } var k = new Key (name, cfg.KeySize, cfg.UseMachinePath); if (!k.IsValid) { Failure (cfg, "Failed to generate RSA key pair."); return true; } k.Save (); Success (); } catch (Exception ex) { Failure (ex, cfg); return true; } return false; }