Ejemplo n.º 1
0
 /// <summary>
 /// Signs the specified stream using the specified Private key.
 /// </summary>
 /// <param name="inputStream">readable stream containing input data.</param>
 /// <param name="privateKey">private key for signing.</param>
 /// <returns>Signature data.</returns>
 /// <example>
 ///     <code>
 ///         var crypto = new VirgilCrypto();
 ///         var keyPair = crypto.GenerateKeys();
 ///         using (var inputStream = new FileStream("[YOUR_FILE_PATH_HERE]", FileMode.Open, FileAccess.Read))
 ///         {
 ///             signature = crypto.GenerateSignature(inputStream, keyPair.PrivateKey);
 ///         }
 ///     </code>
 /// </example>
 /// <remarks>How to verify signature <see cref="VerifySignature(byte[], Stream, IPublicKey)"/>.</remarks>
 public byte[] GenerateSignature(Stream inputStream, IPrivateKey privateKey)
 {
     try
     {
         using (VirgilStreamSigner signer = new VirgilStreamSigner(VirgilHash.Algorithm.SHA512))
             using (VirgilStreamDataSource source = new VirgilStreamDataSource(inputStream))
             {
                 byte[] signature = signer.Sign(source, VirgilCryptoExtentions.Get(privateKey).RawKey);
                 return(signature);
             }
     }
     catch (Exception ex)
     {
         throw new VirgilCryptoException(ex.Message);
     }
 }
Ejemplo n.º 2
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.º 3
0
        /// <summary>
        /// Verifies the specified signature using original stream and signer's Public key.
        /// </summary>
        /// <param name="inputStream">readable stream containing input data.</param>
        /// <param name="publicKey">signer public key for verification.</param>
        /// <param name="signature">signature bytes for verification.</param>
        /// <returns>True if signature is valid, False otherwise.</returns>
        /// <example>
        /// <code>
        ///    var publicKey = crypto.ImportPublicKey(exportedPublicKey);
        ///    using (var inputStream = new FileStream("[YOUR_FILE_PATH_HERE]", FileMode.Open, FileAccess.Read))
        ///    {
        ///       crypto.Verify(inputStream, signature, publicKey);
        ///    }
        /// </code>
        /// </example>
        /// <remarks>How to get exportedPublicKey <see cref="ExportPublicKey(IPublicKey)"/>.</remarks>
        /// <remarks>How to genrate signature <see cref="GenerateSignature(Stream, IPrivateKey)"/>.</remarks>
        public bool VerifySignature(byte[] signature, Stream inputStream, IPublicKey publicKey)
        {
            if (signature == null)
            {
                throw new ArgumentNullException("signature");
            }

            try
            {
                using (VirgilStreamSigner streamSigner = new VirgilStreamSigner(VirgilHash.Algorithm.SHA512))
                {
                    VirgilStreamDataSource source = new VirgilStreamDataSource(inputStream);
                    bool isValid = streamSigner.Verify(source, signature, VirgilCryptoExtentions.Get(publicKey).RawKey);
                    return(isValid);
                }
            }
            catch (Exception ex)
            {
                throw new VirgilCryptoException(ex.Message);
            }
        }
Ejemplo n.º 4
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);
            }
        }