public void Execute(T command) { decoratedHandler.Execute(command); if (command.Result != null) { return; } command.OriginalContentType = ContentType.Der; if (command.IsPrivateKey) { try { command.Result = asymmetricKeyProvider.GetPrivateKey(command.FileContent); } catch (CryptographicException) { command.Result = asymmetricKeyProvider.GetEncryptedPrivateKey(command.FileContent); } return; } command.Result = asymmetricKeyProvider.GetPublicKey(command.FileContent); }
public IAsymmetricKey DecryptPrivateKey(IAsymmetricKey key, string password) { AsymmetricKeyParameter asymmetricKey; try { asymmetricKey = PrivateKeyFactory.DecryptKey(password.ToCharArray(), key.Content); } catch (InvalidCipherTextException) { throw new ArgumentException("Incorrect password was provided or the key is corrupt."); } var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(asymmetricKey); byte[] privateKey = privateKeyInfo .ToAsn1Object() .GetDerEncoded(); return(keyProvider.GetPrivateKey(privateKey)); }
public IAsymmetricKey GetAsDer(string key) { var pemReader = new PemReader(new StringReader(key)); var pemObject = pemReader.ReadPemObject(); if (pemObject.Type.Contains("PUBLIC")) { return(keyProvider.GetPublicKey(pemObject.Content)); } return(pemObject.Type.Contains("ENCRYPTED") ? keyProvider.GetEncryptedPrivateKey(pemObject.Content) : keyProvider.GetPrivateKey(pemObject.Content)); }