private static Stream ChainEncryptedOut(Stream outputStream, PgpEncryptionKeys m_encryptionKeys) { PgpEncryptedDataGenerator encryptedDataGenerator; encryptedDataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.TripleDes, new SecureRandom()); encryptedDataGenerator.AddMethod(m_encryptionKeys.PublicKey); return(encryptedDataGenerator.Open(outputStream, new byte[BufferSize])); }
private static void OutputEncrypted(string inputFile, Stream outputStream, PgpEncryptionKeys encryptionKeys) { using (Stream encryptedOut = ChainEncryptedOut(outputStream, encryptionKeys)) { FileInfo unencryptedFileInfo = new FileInfo(inputFile); using (Stream compressedOut = ChainCompressedOut(encryptedOut)) { using (Stream literalOut = ChainLiteralOut(compressedOut, unencryptedFileInfo)) { using (FileStream inputFileStream = unencryptedFileInfo.OpenRead()) { WriteOutputAndSign(compressedOut, literalOut, inputFileStream); inputFileStream.Close(); } } } } }
/* * Encrypt and sign the file pointed to by unencryptedFileInfo and */ public static void EncryptAndSign(string inputFile, string outputFile, string publicKeyFile, string privateKeyFile, string passPhrase, bool armor) { PgpEncryptionKeys encryptionKeys = new PgpEncryptionKeys(publicKeyFile); if (!File.Exists(inputFile)) { throw new FileNotFoundException(String.Format("Input file [{0}] does not exist.", inputFile)); } if (!File.Exists(publicKeyFile)) { throw new FileNotFoundException(String.Format("Public Key file [{0}] does not exist.", publicKeyFile)); } if (!File.Exists(privateKeyFile)) { throw new FileNotFoundException(String.Format("Private Key file [{0}] does not exist.", privateKeyFile)); } if (String.IsNullOrEmpty(passPhrase)) { throw new ArgumentNullException("Invalid Pass Phrase."); } if (encryptionKeys == null) { throw new ArgumentNullException("Encryption Key not found."); } using (Stream outputStream = File.Create(outputFile)) { if (armor) { using (ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream)) { OutputEncrypted(inputFile, armoredOutputStream, encryptionKeys); } } else { OutputEncrypted(inputFile, outputStream, encryptionKeys); } } }