public override int Run(string[] remainingArguments) { var ret = 0; Crypter crypter = null; IKeySet ks = new KeySet(_location); Func<string> prompt = CachedPrompt.Password(Util.PromptForPassword).Prompt; IDisposable dks = null; if (!String.IsNullOrWhiteSpace(_crypterLocation)) { if (_password) { var cks = new PbeKeySet(_crypterLocation, prompt); crypter = new Crypter(cks); dks = cks; } else { crypter = new Crypter(_crypterLocation); } ks = new EncryptedKeySet(ks, crypter); } else if (_password) { ks = new PbeKeySet(ks, prompt); } var d2ks = ks as IDisposable; using (crypter) using (dks) using (d2ks) using (var keySet = new MutableKeySet(ks)) { var pubKeySet = keySet.PublicKey(); if (pubKeySet != null) { using (pubKeySet) { IKeySetWriter writer = new KeySetWriter(_destination, overwrite: false); if (pubKeySet.Save(writer)) { Console.WriteLine(Localized.MsgNewPublicKeySet); ret = 0; } else { ret = -1; } } } else { ret = -1; } } return ret; }
public void TestOperateOnPbeCryptKeys() { string result; var path = Util.TestDataPath(TEST_DATA, "rsa-pbe2"); var pathc = Util.TestDataPath(TEST_DATA, "rsa-crypted2"); if (Directory.Exists(path)) Directory.Delete(path, true); if (Directory.Exists(pathc)) Directory.Delete(pathc, true); result = Util.KeyczarTool(create: null, location: path, purpose: "crypt", unofficial: null); Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgCreatedKeySet)); result = Util.KeyczarTool("cartman", "cartman", addkey: null, location: path, password: null, status: "primary"); Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgCreatedKey)); result = Util.KeyczarTool(create: null, location: pathc, purpose: "crypt", asymmetric: null); Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgCreatedKeySet)); result = Util.KeyczarTool("cartman", addkey: null, location: pathc, crypter: path, password: null, status: "primary"); Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgCreatedKey)); var pathi = Path.Combine(pathc, "out.pem"); result = Util.KeyczarTool( "cartman", "pass", "pass", export: null, location: pathc, password: null, crypter: path, destination: pathi); Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgExportedPem)); result = Util.KeyczarTool("cartman", "pass", importkey: null, location: pathc, status: "primary", crypter: path, password: null, importlocation: pathi); Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgImportedNewKey)); var pathp = Path.Combine(pathc, "export"); result = Util.KeyczarTool( "cartman", pubkey: null, location: pathc, crypter: path, password: null, destination: pathp ); Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgNewPublicKeySet)); var patho = Path.Combine(pathc, "1.out"); result = Util.KeyczarTool( "cartman", usekey: null, location: pathc, crypter: path, password: null, destination: patho, additionalArgs: new[] {input} ); using (var pks = new PbeKeySet(path, () => "cartman" /*hardcoding because this is a test*/)) using (var kcrypter = new Crypter(pks)) { var eks = new EncryptedKeySet(pathc, kcrypter); using (var crypter = new Crypter(eks)) { Expect(pks.Metadata.Encrypted, Is.True); Expect(eks.Metadata.Encrypted, Is.True); result = crypter.Decrypt((WebBase64) File.ReadAllText(patho)); Expect(result, Is.EqualTo(input)); } } Directory.Delete(pathc, true); }