/// <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); } }
/// <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); } }
/// <summary> /// Extracts the Public key from the specified <see cref="IPrivateKey"/> /// </summary> /// <param name="privateKey"> The private key.</param> /// <returns>The instance of <see cref="IPublicKey"/></returns> public IPublicKey ExtractPublicKey(IPrivateKey privateKey) { try { byte[] publicKeyData = VirgilKeyPair.ExtractPublicKey( VirgilCryptoExtentions.Get(privateKey).RawKey, new byte[] { }); PublicKey publicKey = new PublicKey(); publicKey.Id = VirgilCryptoExtentions.Get(privateKey).Id; publicKey.RawKey = VirgilKeyPair.PublicKeyToDER(publicKeyData); return(publicKey); } catch (Exception ex) { throw new VirgilCryptoException(ex.Message); } }
/// <summary> /// Extracts the Public key from Private key. /// </summary> public override IPublicKey ExtractPublicKey(IPrivateKey privateKey) { try { var publicKeyData = VirgilKeyPair.ExtractPublicKey(privateKey.Get().Value, new byte[] { }); var publicKey = new PublicKey { ReceiverId = privateKey.Get().ReceiverId, Value = VirgilKeyPair.PublicKeyToDER(publicKeyData) }; return(publicKey); } catch (Exception ex) { throw new CryptoException(ex.Message); } }