Ejemplo n.º 1
0
 /// <summary>
 /// Decrypts the specified stream <see cref="cipherStream"/> using the specified Private key
 /// and writes to specified output stream <see cref="outputStream"/>
 /// <param name="cipherStream">readable stream containing encrypted data.</param>
 /// <param name="outputStream">writable stream for output.</param>
 /// <param name="privateKey">private key for decryption.</param>
 /// </summary>
 /// <example>
 ///     <code>
 ///         var crypto = new VirgilCrypto();
 ///         var alicePrivateKey = crypto.ImportPrivateKey(exportedPrivateKey);
 ///         using (var encryptedStream = new FileStream("[YOUR_CIPHER_FILE_PATH_HERE]",
 ///                 FileMode.Open, FileAccess.Read))
 ///         {
 ///             using (var decryptedStream = new FileStream("[YOUR_DECRYPTED_FILE_PATH_HERE]",
 ///                     FileMode.Create, FileAccess.Write))
 ///             {
 ///                 crypto.Decrypt(encryptedStream, decryptedStream, alicePrivateKey);
 ///             }
 ///          }
 ///     </code>
 /// </example>
 /// <remarks>How to get encryptedStream <see cref="Encrypt(Stream, Stream, IPublicKey[])"/></remarks>
 /// <remarks>How to get exportedPrivateKey <see cref="ExportPrivateKey(IPrivateKey, string)"/></remarks>
 public void Decrypt(Stream cipherStream, Stream outputStream, IPrivateKey privateKey)
 {
     try
     {
         using (VirgilChunkCipher cipher = new VirgilChunkCipher())
             using (VirgilStreamDataSource source = new VirgilStreamDataSource(cipherStream))
                 using (VirgilStreamDataSink sink = new VirgilStreamDataSink(outputStream))
                 {
                     cipher.DecryptWithKey(source, sink, VirgilCryptoExtentions.Get(privateKey).Id,
                                           VirgilCryptoExtentions.Get(privateKey).RawKey);
                 }
     }
     catch (Exception ex)
     {
         throw new VirgilCryptoException(ex.Message);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Encrypts the specified stream <see cref="inputStream"/> using the specified recipients Public keys
        /// and writes to specified output stream <see cref="cipherStream"/>.
        /// </summary>
        /// <param name="inputStream">readable stream containing input bytes.</param>
        /// <param name="cipherStream">writable stream for output.</param>
        /// <param name="recipients"> list of recipients' public keys.</param>
        /// <example>
        ///     <code>
        ///         var crypto = new VirgilCrypto();
        ///         var aliceKeyPair = crypto.GenerateKeys();
        ///         var bobKeyPair = crypto.GenerateKeys();
        ///         using (var inputStream = new FileStream("[YOUR_FILE_PATH_HERE]",
        ///         FileMode.Open, FileAccess.Read))
        ///         {
        ///             using (var cipherStream = new FileStream("[YOUR_CIPHER_FILE_PATH_HERE]",
        ///             FileMode.Create, FileAccess.Write))
        ///             {
        ///                crypto.Encrypt(inputStream, cipherStream, aliceKeyPair.PublicKey, bobKeyPair.PublicKey);
        ///             }
        ///          }
        ///     </code>
        /// </example>
        public void Encrypt(Stream inputStream, Stream cipherStream, params IPublicKey[] recipients)
        {
            try
            {
                using (VirgilChunkCipher cipher = new VirgilChunkCipher())
                    using (VirgilStreamDataSource source = new VirgilStreamDataSource(inputStream))
                        using (VirgilStreamDataSink sink = new VirgilStreamDataSink(cipherStream))
                        {
                            foreach (IPublicKey publicKey in recipients)
                            {
                                cipher.AddKeyRecipient(VirgilCryptoExtentions.Get(publicKey).Id,
                                                       VirgilCryptoExtentions.Get(publicKey).RawKey);
                            }

                            cipher.Encrypt(source, sink);
                        }
            }
            catch (Exception ex)
            {
                throw new VirgilCryptoException(ex.Message);
            }
        }