Пример #1
0
        /// <summary>
        /// Imports the Private key from material representation.
        /// </summary>
        public override IPrivateKey ImportPrivateKey(byte[] keyData, string password = null)
        {
            if (keyData == null)
            {
                throw new ArgumentNullException(nameof(keyData));
            }

            try
            {
                var privateKeyBytes = string.IsNullOrEmpty(password)
                    ? keyData
                    : VirgilKeyPair.DecryptPrivateKey(keyData, Encoding.UTF8.GetBytes(password));

                var publicKey  = VirgilKeyPair.ExtractPublicKey(privateKeyBytes, new byte[] { });
                var privateKey = new PrivateKey
                {
                    ReceiverId = this.ComputePublicKeyHash(publicKey),
                    Value      = VirgilKeyPair.PrivateKeyToDER(privateKeyBytes)
                };

                return(privateKey);
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// Imports the Private key from material representation.
        /// </summary>
        /// <param name="keyBytes">private key material representation bytes.</param>
        /// <param name="password">the password that was used during
        /// <see cref="ExportPrivateKey(IPrivateKey, string)"/>.</param>
        /// <returns>Imported private key.</returns>
        /// <example>
        ///     <code>
        ///         var crypto = new VirgilCardCrypto();
        ///         var privateKey = crypto.ImportPrivateKey(exportedPrivateKey, "my_password");
        ///     </code>
        /// </example>
        /// <remarks>How to get exportedPrivateKey <see cref="ExportPrivateKey(IPrivateKey, string)"/></remarks>
        public IPrivateKey ImportPrivateKey(byte[] keyBytes, string password)
        {
            if (keyBytes == null)
            {
                throw new ArgumentNullException("keyBytes");
            }

            try
            {
                byte[] privateKeyBytes = string.IsNullOrEmpty(password)
                    ? keyBytes
                    : VirgilKeyPair.DecryptPrivateKey(keyBytes, Encoding.UTF8.GetBytes(password));

                byte[]     publicKey  = VirgilKeyPair.ExtractPublicKey(privateKeyBytes, new byte[] { });
                PrivateKey privateKey = new PrivateKey();
                privateKey.Id     = this.ComputePublicKeyHash(publicKey);
                privateKey.RawKey = VirgilKeyPair.PrivateKeyToDER(privateKeyBytes);

                return(privateKey);
            }
            catch (Exception ex)
            {
                throw new VirgilCryptoException(ex.Message);
            }
        }