Exemplo n.º 1
0
        /// <summary>
        /// Compress the data.
        /// </summary>
        /// <param name="input">The input data.</param>
        /// <param name="algorithm">The compression alogrithm.</param>
        /// <returns>The compressed data.</returns>
        private byte[] Compress(Stream input, Key.Bcpg.CompressionAlgorithmTag algorithm)
        {
            MemoryStream output = null;

            try
            {
                output = new MemoryStream();

                // Compress the data.
                Key.Bcpg.OpenPgp.PgpCompressedDataGenerator compressedData = new Key.Bcpg.OpenPgp.PgpCompressedDataGenerator(algorithm);
                Key.Bcpg.OpenPgp.PgpUtilities.WriteDataToLiteralData(compressedData.Open(output),
                                                                     input, Key.Bcpg.OpenPgp.PgpLiteralData.Binary, Guid.NewGuid().ToString(), input.Length, DateTime.UtcNow);

                // Return the compressed data.
                compressedData.Close();
                return(output.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (output != null)
                {
                    output.Close();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encrypt the file.
        /// </summary>
        /// <param name="encrypted">The encrypted data stream.</param>
        /// <param name="filename">The path and file name to encrypt.</param>
        /// <param name="publicKey">The public key used for encryption.</param>
        /// <param name="protectedKeys">Should the public and secret key data be protected.</param>
        /// <param name="integrityCheck">Should the cipher stream have an integrity packet associated with it.</param>
        /// <param name="symmetricKeyAlgorithm">The symmetric key algorithm used for cryptography.</param>
        public void Encrypt(System.IO.Stream encrypted, string filename, System.IO.Stream publicKey, bool protectedKeys = false, bool integrityCheck = false,
                            Nequeo.Cryptography.SymmetricKeyAlgorithmType symmetricKeyAlgorithm = Nequeo.Cryptography.SymmetricKeyAlgorithmType.Aes256)
        {
            // Read the public key data.
            Key.Bcpg.OpenPgp.PgpPublicKey pgpPublicKey = ReadPublicKey(publicKey);

            // If file is protected.
            if (protectedKeys)
            {
                encrypted = new Key.Bcpg.ArmoredOutputStream(encrypted);
            }

            System.IO.Stream encOutput = null;

            try
            {
                // Create the encypted data generator.
                Key.Bcpg.OpenPgp.PgpEncryptedDataGenerator encryptedDataGenerator = new Key.Bcpg.OpenPgp.PgpEncryptedDataGenerator(
                    GetSymmetricKeyAlgorithm(symmetricKeyAlgorithm), integrityCheck, new Key.Security.SecureRandom());
                encryptedDataGenerator.AddMethod(pgpPublicKey);

                // The input data buffer.
                Key.Bcpg.OpenPgp.PgpCompressedDataGenerator compressedData =
                    new Key.Bcpg.OpenPgp.PgpCompressedDataGenerator(Key.Bcpg.CompressionAlgorithmTag.Uncompressed);

                // Write the encrypted data.
                encOutput = encryptedDataGenerator.Open(encrypted, new byte[1 << 16]);
                Key.Bcpg.OpenPgp.PgpUtilities.WriteFileToLiteralData(
                    compressedData.Open(encOutput),
                    Key.Bcpg.OpenPgp.PgpLiteralData.Binary,
                    new FileInfo(filename),
                    new byte[1 << 16]);

                // Close the streams.
                compressedData.Close();
                encOutput.Close();

                // If file is protected.
                if (protectedKeys)
                {
                    encrypted.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (encOutput != null)
                {
                    encOutput.Close();
                }
            }
        }