예제 #1
0
        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);
        }
예제 #2
0
        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));
        }
예제 #3
0
        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));
        }