public PgpCompressedDataGenerator(CompressionAlgorithmTag algorithm, int compression) { switch (algorithm) { default: throw new ArgumentException("unknown compression algorithm", "algorithm"); case CompressionAlgorithmTag.Uncompressed: case CompressionAlgorithmTag.Zip: case CompressionAlgorithmTag.ZLib: case CompressionAlgorithmTag.BZip2: switch (compression) { default: throw new ArgumentException("unknown compression level: " + compression); case -1: case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: this.algorithm = algorithm; this.compression = compression; break; } break; } }
public PgpCompressedDataGenerator( CompressionAlgorithmTag algorithm, int compression) { switch (algorithm) { case CompressionAlgorithmTag.Uncompressed: case CompressionAlgorithmTag.Zip: case CompressionAlgorithmTag.ZLib: case CompressionAlgorithmTag.BZip2: break; default: throw new ArgumentException("unknown compression algorithm", "algorithm"); } if (compression != JZlib.Z_DEFAULT_COMPRESSION) { if ((compression < JZlib.Z_NO_COMPRESSION) || (compression > JZlib.Z_BEST_COMPRESSION)) { throw new ArgumentException("unknown compression level: " + compression); } } this.algorithm = algorithm; this.compression = compression; }
private void doTestCompression( CompressionAlgorithmTag type, bool streamClose) { MemoryStream bOut = new MemoryStream(); PgpCompressedDataGenerator cPacket = new PgpCompressedDataGenerator(type); Stream os = cPacket.Open(new UncloseableStream(bOut), new byte[Data.Length - 1]); os.Write(Data, 0, Data.Length); if (streamClose) { os.Close(); } else { cPacket.Close(); } ValidateData(bOut.ToArray()); try { os.Close(); cPacket.Close(); } catch (Exception) { Fail("Redundant Close() should be ignored"); } }
/// <summary> /// Output encrypted data stream /// </summary> /// <param name="inputFile"> /// The input file. /// </param> /// <param name="outputStream"> /// The output stream. /// </param> /// <param name="encryptionKeys"> /// The encryption keys. /// </param> /// <param name="integrityProtected"> /// Integrity protect? /// </param> /// <param name="symmetricKeyAlgorithm"> /// Symmetric algorithm. /// </param> /// <param name="compressionAlgorithm"> /// Compression algorithm. /// </param> private static void OutputEncrypted( string inputFile, Stream outputStream, PgpKeyContainer encryptionKeys, bool integrityProtected, SymmetricKeyAlgorithmTag symmetricKeyAlgorithm = SymmetricKeyAlgorithmTag.Aes128, CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip) { using ( var encryptedOut = ChainEncryptedOut( outputStream, encryptionKeys, integrityProtected, symmetricKeyAlgorithm)) using (var compressedOut = ChainCompressedOut(encryptedOut, compressionAlgorithm)) { var unencryptedFileInfo = new FileInfo(inputFile); var signatureGenerator = InitSignatureGenerator(compressedOut, encryptionKeys); using (var literalOut = ChainLiteralOut(compressedOut, unencryptedFileInfo)) using (var inputFileStream = unencryptedFileInfo.OpenRead()) { WriteOutputAndSign(compressedOut, literalOut, inputFileStream, signatureGenerator); } } }
public PgpCompressedDataGenerator( CompressionAlgorithmTag algorithm, int compression) { switch (algorithm) { case CompressionAlgorithmTag.Uncompressed: case CompressionAlgorithmTag.Zip: case CompressionAlgorithmTag.ZLib: case CompressionAlgorithmTag.BZip2: break; default: throw new ArgumentException("unknown compression algorithm", "algorithm"); } //if (compression != 0) //{ // if ((compression < 0) || (compression > 0)) // { // throw new ArgumentException("unknown compression level: " + compression); // } //} this.algorithm = algorithm; this.compression = compression; }
private static byte[] Compress(byte[] clearData, string fileName, CompressionAlgorithmTag algorithm) { MemoryStream bOut = new MemoryStream(); PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm); Stream cos = comData.Open(bOut); // open it with the final destination PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); // we want to Generate compressed data. This might be a user option later, // in which case we would pass in bOut. Stream pOut = lData.Open( cos, // the compressed output stream PgpLiteralData.Binary, fileName, // "filename" to store clearData.Length, // length of clear data DateTime.UtcNow // current time ); pOut.Write(clearData, 0, clearData.Length); pOut.Close(); comData.Close(); return(bOut.ToArray()); }
/// <summary> /// Compresses the Byte Array removing un-needed data based on the CompressionAlgorithmTag /// </summary> /// <param name="clearData"></param> /// <param name="fileName"></param> /// <param name="algorithm"></param> /// <returns>Compressed Bytes</returns> private static byte[] CompressBytes(byte[] clearData, string fileName, CompressionAlgorithmTag algorithm) { using (var bytesOut = new MemoryStream()) { var compressedData = new PgpCompressedDataGenerator(algorithm); using (var compressedOutput = compressedData.Open(bytesOut)) { var literalData = new PgpLiteralDataGenerator(); try { using (var pOut = literalData.Open(compressedOutput, PgpLiteralData.Binary, fileName, clearData.Length, DateTime.UtcNow)) { pOut.Write(clearData, 0, clearData.Length); return(bytesOut.ToArray()); } } catch (Exception e) { throw new Exception("Unable to Compress", e); } } } }
internal static byte[] CompressFile(string fileName, CompressionAlgorithmTag algorithm) { MemoryStream bOut = new MemoryStream(); PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm); PgpUtilities.WriteFileToLiteralData(comData.Open(bOut), PgpLiteralData.Binary, new FileInfo(fileName)); comData.Close(); return bOut.ToArray(); }
internal static byte[] CompressFile(string fileName, CompressionAlgorithmTag algorithm) { MemoryStream bOut = new MemoryStream(); PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm); PgpUtilities.WriteFileToLiteralData(comData.Open(bOut), PgpLiteralData.Binary, new FileInfo(fileName)); comData.Close(); return(bOut.ToArray()); }
/// <summary> /// Gets compression stream if compression is needed, otherwise returns original stream /// </summary> /// <param name="stream">Source stream</param> /// <param name="input">Task input</param> /// <returns>Compression chained stream or original source</returns> internal static Stream GetCompressionStream(Stream stream, PgpEncryptInput input) { if (input.UseArmor) { CompressionAlgorithmTag compressionTag = input.CompressionType.ConvertEnum <CompressionAlgorithmTag>(); PgpCompressedDataGenerator compressedDataGenerator = new PgpCompressedDataGenerator(compressionTag); return(compressedDataGenerator.Open(stream)); } return(stream); }
private const int BufferSize = 0x10000; // should always be power of 2 /// <summary> /// Instantiate a new PgpEncrypt class with initialized PgpEncryptionKeys. /// </summary> /// <param name="encryptionKeys"></param> /// <param name="symmetricAlgorithm"></param> /// <param name="compressionAlgorithm"></param> /// <param name="sigHashAlgorithmTag"></param> /// <exception cref="ArgumentNullException">encryptionKeys is null</exception> public PgpEncrypt(PgpEncryptionKeys encryptionKeys, SymmetricKeyAlgorithmTag symmetricAlgorithm, CompressionAlgorithmTag compressionAlgorithm, HashAlgorithmTag sigHashAlgorithmTag) { if (encryptionKeys == null) { throw new ArgumentNullException("encryptionKeys", "encryptionKeys is null."); } _encryptionKeys = encryptionKeys; _symmetricAlgorithm = symmetricAlgorithm; _compressionAlgorithm = compressionAlgorithm; _sigHashAlgorithmTag = sigHashAlgorithmTag; }
public static void PgpEncrypt( this Stream toEncrypt, Stream outStream, PgpPublicKey encryptionKey, bool armor = true, bool verify = false, CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip) { var encryptor = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, verify, new SecureRandom()); var literalizer = new PgpLiteralDataGenerator(); var compressor = new PgpCompressedDataGenerator(compressionAlgorithm); encryptor.AddMethod(encryptionKey); //it would be nice if these streams were read/write, and supported seeking. Since they are not, //we need to shunt the data to a read/write stream so that we can control the flow of data as //we go. using (var stream = new MemoryStream()) // this is the read/write stream using (var armoredStream = armor ? new ArmoredOutputStream(stream) : stream as Stream) using (var compressedStream = compressor.Open(armoredStream)) { //data is encrypted first, then compressed, but because of the one-way nature of these streams, //other "interim" streams are required. The raw data is encapsulated in a "Literal" PGP object. var rawData = toEncrypt.ReadFully(); var buffer = new byte[1024]; using (var literalOut = new MemoryStream()) using (var literalStream = literalizer.Open(literalOut, 'b', "STREAM", DateTime.UtcNow, buffer)) { literalStream.Write(rawData, 0, rawData.Length); literalStream.Close(); var literalData = literalOut.ReadFully(); //The literal data object is then encrypted, which flows into the compressing stream and //(optionally) into the ASCII armoring stream. using (var encryptedStream = encryptor.Open(compressedStream, literalData.Length)) { encryptedStream.Write(literalData, 0, literalData.Length); encryptedStream.Close(); compressedStream.Close(); armoredStream.Close(); //the stream processes are now complete, and our read/write stream is now populated with //encrypted data. Convert the stream to a byte array and write to the out stream. stream.Position = 0; var data = stream.ReadFully(); outStream.Write(data, 0, data.Length); } } } }
// http://stackoverflow.com/questions/20572737/sign-and-verify-xml-file-in-c-sharp public void SignFile(string hashAlgorithm, string fileName, System.IO.Stream privateKeyStream , string privateKeyPassword, System.IO.Stream outStream) { PgpSecretKey pgpSec = ReadSigningSecretKey(privateKeyStream); PgpPrivateKey pgpPrivKey = null; pgpPrivKey = pgpSec.ExtractPrivateKey(privateKeyPassword.ToCharArray()); PgpSignatureGenerator sGen = new PgpSignatureGenerator(pgpSec.PublicKey.Algorithm, ParseHashAlgorithm(hashAlgorithm)); sGen.InitSign(PgpSignature.BinaryDocument, pgpPrivKey); foreach (string userId in pgpSec.PublicKey.GetUserIds()) { PgpSignatureSubpacketGenerator spGen = new PgpSignatureSubpacketGenerator(); spGen.SetSignerUserId(false, userId); sGen.SetHashedSubpackets(spGen.Generate()); } CompressionAlgorithmTag compression = PreferredCompression(pgpSec.PublicKey); PgpCompressedDataGenerator cGen = new PgpCompressedDataGenerator(compression); BcpgOutputStream bOut = new BcpgOutputStream(cGen.Open(outStream)); sGen.GenerateOnePassVersion(false).Encode(bOut); System.IO.FileInfo file = new System.IO.FileInfo(fileName); System.IO.FileStream fIn = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); System.IO.Stream lOut = lGen.Open(bOut, PgpLiteralData.Binary, file); int ch = 0; while ((ch = fIn.ReadByte()) >= 0) { lOut.WriteByte((byte)ch); sGen.Update((byte)ch); } fIn.Close(); sGen.Generate().Encode(bOut); lGen.Close(); cGen.Close(); outStream.Close(); }
/// <summary> /// Compresses a file using the specified Compression Algorithm. /// </summary> /// <param name="fileName"></param> /// <param name="algorithm"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> internal static byte[] CompressFile(string fileName, CompressionAlgorithmTag algorithm) { // Parameter Checks if (String.IsNullOrEmpty(fileName)) { throw new ArgumentException("File Name Parameter is invalid."); } MemoryStream _memoryStream = new MemoryStream(); PgpCompressedDataGenerator _compressedDataGen = new PgpCompressedDataGenerator(algorithm); PgpUtilities.WriteFileToLiteralData(_compressedDataGen.Open(_memoryStream), PgpLiteralData.Binary, new FileInfo(fileName)); _compressedDataGen.Close(); return(_memoryStream.ToArray()); }
internal static byte[] CompressStream(Stream inputStream, string fileName, CompressionAlgorithmTag algorithm) { using (MemoryStream bOut = new MemoryStream()) { //inputStream.CopyTo(bOut); PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm); PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); Stream pOut = lData.Open(comData.Open(bOut), PgpLiteralData.Binary, fileName, inputStream.Length, DateTime.Now); inputStream.CopyTo(pOut); inputStream.Close(); return bOut.ToArray(); } }
internal static byte[] Compress(string input, CompressionAlgorithmTag algorithmTag) { byte[] inputData = Encoding.Default.GetBytes(input); using (MemoryStream memoryStream = new MemoryStream()) { PgpCompressedDataGenerator compressedDataGenerator = new PgpCompressedDataGenerator(algorithmTag); using (Stream compressedStream = compressedDataGenerator.Open(memoryStream)) using (Stream outputStream = new PgpLiteralDataGenerator().Open(compressedStream, PgpLiteralData.Binary, PgpLiteralData.Console, inputData.Length, DateTime.Now)) { outputStream.Write(inputData, 0, inputData.Length); } compressedDataGenerator.Close(); return(memoryStream.ToArray()); } }
/// <summary> /// Compresses a file using the specified Compression Algorithm. /// </summary> /// <param name="fileName"></param> /// <param name="algorithm"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> internal static byte[] CompressFile(string fileName, CompressionAlgorithmTag algorithm) { // Parameter Checks if (String.IsNullOrEmpty(fileName)) { throw new ArgumentException("File Name Parameter is invalid."); } MemoryStream _memoryStream = new MemoryStream(); PgpCompressedDataGenerator _compressedDataGen = new PgpCompressedDataGenerator(algorithm); FileInfo inputFile = new FileInfo(fileName); Stream inputFileStream = File.OpenRead(inputFile.FullName); WriteStreamToLiteralData(_compressedDataGen.Open(_memoryStream), PgpLiteralData.Binary, inputFileStream, inputFile.Name); _compressedDataGen.Close(); inputFileStream.Dispose(); return(_memoryStream.ToArray()); }
/// <summary> /// Compress data object as a PGP message using the specified compression /// format. /// </summary> /// <param name="data"> /// The data to compress. /// </param> /// <param name="format"> /// The format to compress that data as. /// </param> /// <param name="armor"> /// Compress using ASCII armor format? /// </param> /// <returns> /// The compressed <see cref="byte"/> array. /// </returns> public static byte[] CompressData( byte[] data, CompressionAlgorithmTag format = CompressionAlgorithmTag.Zip, bool armor = true) { using (var binaryStream = new MemoryStream()) using (var armorStream = armor ? (Stream) new ArmoredOutputStream(binaryStream) : binaryStream) { var compressedDataGenerator = new PgpCompressedDataGenerator(format); using (var compressedStream = compressedDataGenerator.Open(armorStream, new byte[8])) { compressedStream.Write(data, 0, data.Length); } // .NET standard 1.6 compliant armorStream.Dispose(); return(binaryStream.ToArray()); } }
/// <summary> /// Encrypt and sign the file. /// </summary> /// <param name="inputFile"> /// Input path of the file to encrypt. /// </param> /// <param name="outputFile"> /// Output path of the encrypted file. /// </param> /// <param name="publicKeyFile"> /// Path to the public key file. /// </param> /// <param name="privateKeyFile"> /// Path to the secret key file containing the private key. /// </param> /// <param name="passPhrase"> /// The passphrase protecting the secret file. /// </param> /// <param name="symmetricKeyAlgorithm"> /// Symmetric encryption algorithm. /// </param> /// <param name="armor"> /// Should the encrypted file be written as ASCII armor? /// </param> /// <param name="integrityProtect"> /// Integrity protect? /// </param> /// <param name="compressionAlgorithm"> /// Compression algorithm to use. /// </param> /// <exception cref="FileNotFoundException"> /// Throws a <see cref="FileNotFoundException"/> if the input, public /// key or secret key files do not exist. /// </exception> public static void EncryptAndSignFile( string inputFile, string outputFile, string publicKeyFile, string privateKeyFile, string passPhrase, SymmetricKeyAlgorithmTag symmetricKeyAlgorithm = SymmetricKeyAlgorithmTag.Aes256, bool armor = true, bool integrityProtect = true, CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip) { var encryptionKeys = new PgpKeyContainer(publicKeyFile, privateKeyFile, passPhrase); VerifyEncryptionParameters(inputFile, publicKeyFile, privateKeyFile, passPhrase, encryptionKeys); using (Stream outputStream = File.Create(outputFile)) { if (armor) { using (var armoredOutputStream = new ArmoredOutputStream(outputStream)) { OutputEncrypted( inputFile, armoredOutputStream, encryptionKeys, integrityProtect, symmetricKeyAlgorithm, compressionAlgorithm); } } else { OutputEncrypted( inputFile, outputStream, encryptionKeys, integrityProtect, symmetricKeyAlgorithm, compressionAlgorithm); } } }
public static byte[] PgpEncrypt( Stream toEncrypt, PgpPublicKey encryptionKey, bool armor = true, bool verify = false, CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip) { var encryptor = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Aes256, verify, new SecureRandom()); var literalizer = new PgpLiteralDataGenerator(); var compressor = new PgpCompressedDataGenerator(compressionAlgorithm); encryptor.AddMethod(encryptionKey); using var stream = new MemoryStream(); using var armoredStream = armor ? new ArmoredOutputStream(stream) : stream as Stream; using var compressedStream = compressor.Open(armoredStream); var rawData = toEncrypt.ReadFully(); var buffer = new byte[1024]; using var literalOut = new MemoryStream(); using var literalStream = literalizer.Open(literalOut, 'b', "STREAM", DateTime.UtcNow, buffer); literalStream.Write(rawData, 0, rawData.Length); literalStream.Close(); var literalData = literalOut.ReadFully(); using var encryptedStream = encryptor.Open(compressedStream, literalData.Length); encryptedStream.Write(literalData, 0, literalData.Length); encryptedStream.Close(); compressedStream.Close(); armoredStream.Close(); stream.Position = 0; Stream outStream = new MemoryStream(); var data = stream.ReadFully(); outStream.Write(data, 0, data.Length); return(data); }
public PgpCompressedDataGenerator(CompressionAlgorithmTag algorithm, int compression) { //IL_0028: Unknown result type (might be due to invalid IL or missing references) //IL_004b: Unknown result type (might be due to invalid IL or missing references) switch (algorithm) { default: throw new ArgumentException("unknown compression algorithm", "algorithm"); case CompressionAlgorithmTag.Uncompressed: case CompressionAlgorithmTag.Zip: case CompressionAlgorithmTag.ZLib: case CompressionAlgorithmTag.BZip2: switch (compression) { default: throw new ArgumentException(string.Concat((object)"unknown compression level: ", (object)compression)); case -1: case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: this.algorithm = algorithm; this.compression = compression; break; } break; } }
public PgpCompressedDataGenerator( CompressionAlgorithmTag algorithm) : this(algorithm, JZlib.Z_DEFAULT_COMPRESSION) { }
private static Stream ChainCompressedOut(Stream encryptedOut, CompressionAlgorithmTag compAgreed) { PgpCompressedDataGenerator pgpCompDataGenerator = new PgpCompressedDataGenerator(compAgreed); return(pgpCompDataGenerator.Open(encryptedOut)); }
public PgpCompressedDataGenerator(CompressionAlgorithmTag algorithm) : this(algorithm, -1) { }
private void doTestCompression( CompressionAlgorithmTag type) { doTestCompression(type, true); doTestCompression(type, false); }
internal CompressedDataPacket( BcpgInputStream bcpgIn) : base(bcpgIn) { this.algorithm = (CompressionAlgorithmTag)bcpgIn.ReadByte(); }
internal CompressedDataPacket( BcpgInputStream bcpgIn) : base(bcpgIn) { this.algorithm = (CompressionAlgorithmTag) bcpgIn.ReadByte(); }
//public static string GetPrivateKeyXml(string inputData) //{ // Stream inputStream = StreamHelper.GetStream(inputData); // PgpObjectFactory pgpFactory = new PgpObjectFactory(PgpUtilities.GetDecoderStream(inputStream)); // PgpObject pgp = null; // if (pgpFactory != null) // { // pgp = pgpFactory.NextPgpObject(); // } // PgpEncryptedDataList encryptedData = null; // if (pgp is PgpEncryptedDataList) // { // encryptedData = (PgpEncryptedDataList)pgp; // } // else // { // encryptedData = (PgpEncryptedDataList)pgpFactory.NextPgpObject(); // } // Stream privateKeyStream = File.OpenRead(PrivateKeyOnlyPath); // // find secret key // PgpSecretKeyRingBundle pgpKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); // PgpPrivateKey privateKey = null; // foreach (PgpPublicKeyEncryptedData pked in encryptedData.GetEncryptedDataObjects()) // { // privateKey = FindSecretKey(pgpKeyRing, pked.KeyId, Password.ToCharArray()); // if (privateKey != null) // { // //pubKeyData = pked; // break; // } // } // // get xml: // RsaPrivateCrtKeyParameters rpckp = ((RsaPrivateCrtKeyParameters)privateKey.Key); // RSAParameters dotNetParams = DotNetUtilities.ToRSAParameters(rpckp); // RSA rsa = RSA.Create(); // rsa.ImportParameters(dotNetParams); // string xmlPrivate = rsa.ToXmlString(true); // return xmlPrivate; //} #endregion #region helpers /// <summary> /// /// </summary> /// <param name="clearData"></param> /// <param name="fileName">File name reference for compression to include (not fully qualified).</param> /// <param name="algorithm"></param> /// <returns></returns> private static byte[] Compress(byte[] clearData, string fileName, CompressionAlgorithmTag algorithm) { MemoryStream bOut = new MemoryStream(); PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm); Stream cos = comData.Open(bOut); // open it with the final destination PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); // we want to Generate compressed data. This might be a user option later, // in which case we would pass in bOut. Stream pOut = lData.Open( cos, // the compressed output stream PgpLiteralData.Binary, fileName, // "filename" to store clearData.Length, // length of clear data DateTime.UtcNow // current time ); pOut.Write(clearData, 0, clearData.Length); pOut.Close(); comData.Close(); return bOut.ToArray(); }
/// <summary> /// Encrypt a file as specified by the input file path. /// </summary> /// <param name="inputFile"> /// The file to encrypt. /// </param> /// <param name="outputFile"> /// The file to write the encrypted content to. /// </param> /// <param name="publicKeyFile"> /// The path to the public key file to use for encryption. /// </param> /// <param name="symmetricKeyAlgorithm"> /// Encryption algorithm. /// </param> /// <param name="armor"> /// Should the encrypted file be written using ASCII armor? /// </param> /// <param name="withIntegrityCheck"> /// Should the integrity be verified? /// </param> /// <param name="compressionAlgorithm"> /// Compression algorithm to use. /// </param> public static void EncryptFile( string inputFile, string outputFile, string publicKeyFile, SymmetricKeyAlgorithmTag symmetricKeyAlgorithm = SymmetricKeyAlgorithmTag.Aes256, bool armor = true, bool withIntegrityCheck = true, CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip) { try { using (Stream publicKeyStream = File.OpenRead(publicKeyFile)) { PgpPublicKey encKey = PgpKeyHelper.ReadPublicKey(publicKeyStream); using (var memoryStream = new MemoryStream()) { var compressedDataGenerator = new PgpCompressedDataGenerator(compressionAlgorithm); WriteFileToLiteralData( compressedDataGenerator.Open(memoryStream), PgpLiteralData.Binary, new FileInfo(inputFile)); compressedDataGenerator.Close(); var encryptedDataGenerator = new PgpEncryptedDataGenerator( symmetricKeyAlgorithm, withIntegrityCheck, new SecureRandom()); encryptedDataGenerator.AddMethod(encKey); var bytes = memoryStream.ToArray(); using (Stream outputStream = File.Create(outputFile)) { if (armor) { using (var armoredStream = new ArmoredOutputStream(outputStream)) using (var encryptedStream = encryptedDataGenerator.Open(armoredStream, bytes.Length)) { encryptedStream.Write(bytes, 0, bytes.Length); } } else { using ( Stream encryptedOutputStream = encryptedDataGenerator.Open( outputStream, bytes.Length)) { encryptedOutputStream.Write(bytes, 0, bytes.Length); } } } } } } catch (PgpException exception) { PgpCommon.DumpException(exception); throw; } }