/// <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(); } } }
/// <summary> /// Encrypt the stream. /// </summary> /// <param name="encrypted">The encrypted data stream.</param> /// <param name="input">The data 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, System.IO.Stream input, 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. byte[] buffer = Compress(input, Key.Bcpg.CompressionAlgorithmTag.Uncompressed); // Write the encrypted data. encOutput = encryptedDataGenerator.Open(encrypted, (long)buffer.Length); encOutput.Write(buffer, 0, buffer.Length); encOutput.Close(); // If file is protected. if (protectedKeys) { encrypted.Close(); } } catch (Exception) { throw; } finally { if (encOutput != null) { encOutput.Close(); } } }