/// <summary>
        /// Exports the Private key into material representation.
        /// </summary>
        public override byte[] ExportPrivateKey(IPrivateKey privateKey, string password = null)
        {
            try
            {
                if (string.IsNullOrEmpty(password))
                {
                    return(VirgilKeyPair.PrivateKeyToDER(privateKey.Get().Value));
                }

                var passwordBytes = Encoding.UTF8.GetBytes(password);
                var encryptedKey  = VirgilKeyPair.EncryptPrivateKey(privateKey.Get().Value, passwordBytes);

                return(VirgilKeyPair.PrivateKeyToDER(encryptedKey, passwordBytes));
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Exports the Private key into material representation.
        /// </summary>
        /// <param name="privateKey">private key for export.</param>
        /// <param name="password">password that is used for encryption of private key raw bytes.</param>
        /// <returns>Private key material representation bytes.</returns>
        /// <example>
        ///     <code>
        ///         var crypto = new VirgilCrypto();
        ///         var keyPair = crypto.GenerateKeys();
        ///         var crypto = new VirgilCrypto();
        ///         var exportedPrivateKey = crypto.ExportPrivateKey(keyPair.PrivateKey, "my_password");
        ///     </code>
        /// </example>
        /// <remarks>How to import private key <see cref="ImportPrivateKey(byte[], string)"/>.</remarks>
        /// <remarks>How to get generate keys <see cref="VirgilCrypto.GenerateKeys()"/>.</remarks>
        public byte[] ExportPrivateKey(IPrivateKey privateKey, string password)
        {
            if (privateKey == null)
            {
                throw new ArgumentNullException("privateKey");
            }
            try
            {
                if (string.IsNullOrEmpty(password))
                {
                    return(VirgilKeyPair.PrivateKeyToDER(VirgilCryptoExtentions.Get(privateKey).RawKey));
                }

                byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
                byte[] encryptedKey  = VirgilKeyPair.EncryptPrivateKey(VirgilCryptoExtentions.Get(privateKey).RawKey,
                                                                       passwordBytes);

                return(VirgilKeyPair.PrivateKeyToDER(encryptedKey, passwordBytes));
            }
            catch (Exception ex)
            {
                throw new VirgilCryptoException(ex.Message);
            }
        }