/// <summary> /// Decrypts and verifies the data. /// </summary> /// <param name="cipherbuffer">The data to be decrypted.</param> /// <param name="card">The signer's <see cref="VirgilCard"/>.</param> /// <returns>The decrypted data, which is the original plain text before encryption.</returns> /// <exception cref="ArgumentNullException"></exception> public VirgilBuffer DecryptThenVerify(VirgilBuffer cipherbuffer, VirgilCard card) { var plaitext = this.context.Crypto .DecryptThenVerify(cipherbuffer.GetBytes(), this.privateKey, card.PublicKey); return(new VirgilBuffer(plaitext)); }
/// <summary> /// Imports the <see cref="VirgilKey"/> from buffer. /// </summary> /// <param name="keyBuffer">The buffer with Key.</param> /// <param name="keyPassword">The Key password.</param> /// <returns>An instance of <see cref="VirgilKey"/> class.</returns> public VirgilKey Import(VirgilBuffer keyBuffer, string keyPassword = null) { var privateKey = this.context.Crypto.ImportPrivateKey(keyBuffer.GetBytes(), keyPassword); var virgilKey = new VirgilKey(this.context, privateKey); return(virgilKey); }
/// <summary> /// Decrypts and verifies the data. /// </summary> /// <param name="cipherbuffer">The data to be decrypted.</param> /// <param name="card">A list of trusted cards, which can contains the signer's <see cref="VirgilCard"/>.</param> /// <returns>The decrypted data, which is the original plain text before encryption.</returns> /// <exception cref="ArgumentNullException"></exception> /// <example> /// <code> /// var virgil = new VirgilApi("[YOUR_ACCESS_TOKEN_HERE]"); /// // load a Virgil Key from device storage /// var bobKey = virgil.Keys.Load("[KEY_NAME]", "[OPTIONAL_KEY_PASSWORD]") /// /// // get a sender's Virgil Card /// var aliceCard = await virgil.Cards.Get("[ALICE_CARD_ID]") /// /// // decrypt the message /// var originalMessage = bobKey.DecryptThenVerify(ciphertext, aliceCard).ToString(); /// </code> /// </example> public VirgilBuffer DecryptThenVerify(VirgilBuffer cipherbuffer, params VirgilCard[] cards) { var plaitext = this.context.Crypto .DecryptThenVerify(cipherbuffer.GetBytes(), this.privateKey, cards.Select(it => it.PublicKey).ToArray()); return(new VirgilBuffer(plaitext)); }
/// <summary> /// Encrypts the specified data for current <see cref="VirgilCard"/> recipient. /// </summary> /// <param name="buffer">The data to be encrypted.</param> /// <example> /// <code> /// // search for Virgil Cards /// var aliceCards = await virgil.Cards.FindAsync("alice"); /// /// var fileBuf = VirgilBuffer.FromFile("FILE_NAME_HERE"); /// /// // encrypt the buffer using found Virgil Cards /// var cipherFileBuf = aliceCards.Encrypt(fileBuf); /// </code> /// </example> public VirgilBuffer Encrypt(VirgilBuffer buffer) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } var cipherdata = this.context.Crypto.Encrypt(buffer.GetBytes(), this.PublicKey); return(new VirgilBuffer(cipherdata)); }
/// <summary> /// Decrypts the specified cipher data using <see cref="VirgilKey"/>. /// </summary> /// <param name="cipherBuffer">The encrypted data.</param> /// <returns>A byte array containing the result from performing the operation.</returns> /// <exception cref="ArgumentNullException"></exception> public VirgilBuffer Decrypt(VirgilBuffer cipherBuffer) { if (cipherBuffer == null) { throw new ArgumentNullException(nameof(cipherBuffer)); } var data = this.context.Crypto.Decrypt(cipherBuffer.GetBytes(), this.privateKey); return(new VirgilBuffer(data)); }
/// <summary> /// Generates a digital signature for specified data using current <see cref="VirgilKey"/>. /// </summary> /// <param name="data">The data for which the digital signature will be generated.</param> /// <returns>A new buffer that containing the result from performing the operation.</returns> /// <exception cref="ArgumentNullException"></exception> public VirgilBuffer Sign(VirgilBuffer data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } var signature = this.context.Crypto.Sign(data.GetBytes(), this.privateKey); return(new VirgilBuffer(signature)); }
/// <summary> /// Encrypts and signs the data. /// </summary> /// <param name="buffer">The data to be encrypted.</param> /// <param name="recipients">The list of <see cref="VirgilCard"/> recipients.</param> /// <returns>The encrypted data</returns> /// <exception cref="ArgumentNullException"></exception> public VirgilBuffer SignThenEncrypt(VirgilBuffer buffer, IEnumerable <VirgilCard> recipients) { if (recipients == null) { throw new ArgumentNullException(nameof(recipients)); } var publicKeys = recipients.Select(pk => pk.PublicKey).ToArray(); var cipherdata = this.context.Crypto.SignThenEncrypt(buffer.GetBytes(), this.privateKey, publicKeys); return(new VirgilBuffer(cipherdata)); }
/// <summary> /// Encrypts a buffer data for list of <paramref name="recipients"/> Cards. /// </summary> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> internal VirgilBuffer Encrypt(VirgilBuffer buffer, IEnumerable <VirgilCard> recipients) { var publicKeyRecipients = new List <IPublicKey>(); var virgilCards = recipients?.ToList(); if (virgilCards != null) { publicKeyRecipients.AddRange(virgilCards.Select(r => r.PublicKey)); } var cipherdata = this.context.Crypto.Encrypt(buffer.GetBytes(), publicKeyRecipients.ToArray()); return(new VirgilBuffer(cipherdata)); }
/// <summary> /// Verifies the specified buffer and signature with current <see cref="VirgilCard"/> recipient. /// </summary> /// <param name="buffer">The data to be verified.</param> /// <param name="signature">The signature used to verify the data integrity.</param> public bool Verify(VirgilBuffer buffer, VirgilBuffer signature) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (signature == null) { throw new ArgumentNullException(nameof(signature)); } var isValid = this.context.Crypto.Verify( buffer.GetBytes(), signature.GetBytes(), this.PublicKey); return(isValid); }