public void EncryptFileAndSign(string inputFilePath, string outputFilePath, string publicKeyFilePath, string privateKeyFilePath, string passPhrase, bool armor = true, bool withIntegrityCheck = true) { if (String.IsNullOrEmpty(inputFilePath)) { throw new ArgumentException(nameof(inputFilePath)); } if (String.IsNullOrEmpty(outputFilePath)) { throw new ArgumentException(nameof(outputFilePath)); } if (String.IsNullOrEmpty(publicKeyFilePath)) { throw new ArgumentException(nameof(publicKeyFilePath)); } if (String.IsNullOrEmpty(privateKeyFilePath)) { throw new ArgumentException(nameof(privateKeyFilePath)); } if (passPhrase == null) { passPhrase = String.Empty; } if (!File.Exists(inputFilePath)) { throw new FileNotFoundException(String.Format("Input file [{0}] does not exist.", inputFilePath)); } if (!File.Exists(publicKeyFilePath)) { throw new FileNotFoundException(String.Format("Public Key file [{0}] does not exist.", publicKeyFilePath)); } if (!File.Exists(privateKeyFilePath)) { throw new FileNotFoundException(String.Format("Private Key file [{0}] does not exist.", privateKeyFilePath)); } ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyFilePath, privateKeyFilePath, passPhrase); if (encryptionKeys == null) { throw new ArgumentNullException("Encryption Key not found."); } using (Stream outputStream = File.Create(outputFilePath)) { if (armor) { using (ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream)) { OutputEncrypted(inputFilePath, armoredOutputStream, encryptionKeys, withIntegrityCheck); } } else { OutputEncrypted(inputFilePath, outputStream, encryptionKeys, withIntegrityCheck); } } }
private Stream ChainEncryptedOut(Stream outputStream, ChoPGPEncryptionKeys encryptionKeys, bool withIntegrityCheck) { PgpEncryptedDataGenerator encryptedDataGenerator; encryptedDataGenerator = new PgpEncryptedDataGenerator((SymmetricKeyAlgorithmTag)(int)SymmetricKeyAlgorithm, withIntegrityCheck, new SecureRandom()); encryptedDataGenerator.AddMethod(encryptionKeys.PublicKey); return(encryptedDataGenerator.Open(outputStream, new byte[BufferSize])); }
private void OutputEncrypted(Stream inputStream, Stream outputStream, ChoPGPEncryptionKeys encryptionKeys, bool withIntegrityCheck, string name) { using (Stream encryptedOut = ChainEncryptedOut(outputStream, encryptionKeys, withIntegrityCheck)) { using (Stream compressedOut = ChainCompressedOut(encryptedOut)) { PgpSignatureGenerator signatureGenerator = InitSignatureGenerator(compressedOut, encryptionKeys); using (Stream literalOut = ChainLiteralStreamOut(compressedOut, inputStream, name)) { WriteOutputAndSign(compressedOut, literalOut, inputStream, signatureGenerator); inputStream.Dispose(); } } } }
private PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut, ChoPGPEncryptionKeys encryptionKeys) { PublicKeyAlgorithmTag tag = encryptionKeys.SecretKey.PublicKey.Algorithm; PgpSignatureGenerator pgpSignatureGenerator = new PgpSignatureGenerator(tag, HashAlgorithmTag.Sha1); pgpSignatureGenerator.InitSign(PgpSignature.BinaryDocument, encryptionKeys.PrivateKey); foreach (string userId in encryptionKeys.SecretKey.PublicKey.GetUserIds()) { PgpSignatureSubpacketGenerator subPacketGenerator = new PgpSignatureSubpacketGenerator(); subPacketGenerator.SetSignerUserId(false, userId); pgpSignatureGenerator.SetHashedSubpackets(subPacketGenerator.Generate()); // Just the first one! break; } pgpSignatureGenerator.GenerateOnePassVersion(false).Encode(compressedOut); return(pgpSignatureGenerator); }
/// <summary> /// Encrypt and sign the file pointed to by unencryptedFileInfo and /// </summary> /// <param name="inputFilePath"></param> /// <param name="outputFilePath"></param> /// <param name="publicKeyFilePath"></param> /// <param name="privateKeyFilePath"></param> /// <param name="passPhrase"></param> /// <param name="armor"></param> public void EncryptAndSign(Stream inputStream, Stream outputStream, Stream publicKeyStream, Stream privateKeyStream, string passPhrase, bool armor = true, bool withIntegrityCheck = true) { if (inputStream == null) { throw new ArgumentException(nameof(inputStream)); } if (outputStream == null) { throw new ArgumentException(nameof(outputStream)); } if (publicKeyStream == null) { throw new ArgumentException(nameof(publicKeyStream)); } if (privateKeyStream == null) { throw new ArgumentException(nameof(privateKeyStream)); } if (passPhrase == null) { passPhrase = String.Empty; } ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyStream, privateKeyStream, passPhrase); if (encryptionKeys == null) { throw new ArgumentNullException("Encryption Key not found."); } if (armor) { using (ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream)) { OutputEncrypted(inputStream, armoredOutputStream, encryptionKeys, withIntegrityCheck, GetFileName(inputStream)); } } else { OutputEncrypted(inputStream, outputStream, encryptionKeys, withIntegrityCheck, GetFileName(inputStream)); } }
private void OutputEncrypted(string inputFilePath, Stream outputStream, ChoPGPEncryptionKeys encryptionKeys, bool withIntegrityCheck) { using (Stream encryptedOut = ChainEncryptedOut(outputStream, encryptionKeys, withIntegrityCheck)) { FileInfo unencryptedFileInfo = new FileInfo(inputFilePath); using (Stream compressedOut = ChainCompressedOut(encryptedOut)) { PgpSignatureGenerator signatureGenerator = InitSignatureGenerator(compressedOut, encryptionKeys); using (Stream literalOut = ChainLiteralOut(compressedOut, unencryptedFileInfo)) { using (FileStream inputFileStream = unencryptedFileInfo.OpenRead()) { WriteOutputAndSign(compressedOut, literalOut, inputFileStream, signatureGenerator); inputFileStream.Dispose(); } } } } }
public void DecryptAndVerify(Stream inputStream, Stream outputStream, Stream publicKeyStream, Stream privateKeyStream, string passPhrase) { if (inputStream == null) { throw new ArgumentException(nameof(inputStream)); } if (outputStream == null) { throw new ArgumentException(nameof(outputStream)); } if (publicKeyStream == null) { throw new ArgumentException(nameof(publicKeyStream)); } if (privateKeyStream == null) { throw new ArgumentException(nameof(privateKeyStream)); } if (passPhrase == null) { passPhrase = String.Empty; } ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyStream, privateKeyStream, passPhrase); if (encryptionKeys == null) { throw new ArgumentNullException("Encryption Key not found."); } PgpPublicKeyEncryptedData publicKeyED = ExtractPublicKeyEncryptedData(inputStream); if (publicKeyED.KeyId != encryptionKeys.PublicKey.KeyId) { throw new PgpException(String.Format("Failed to verify file.")); } PgpObject message = GetClearCompressedMessage(publicKeyED, encryptionKeys); if (message is PgpCompressedData) { message = ProcessCompressedMessage(message); PgpLiteralData literalData = (PgpLiteralData)message; using (Stream literalDataStream = literalData.GetInputStream()) { Streams.PipeAll(literalDataStream, outputStream); } } else if (message is PgpLiteralData) { PgpLiteralData literalData = (PgpLiteralData)message; using (Stream literalDataStream = literalData.GetInputStream()) { Streams.PipeAll(literalDataStream, outputStream); } } else { throw new PgpException("Message is not a simple encrypted file."); } return; }
public void DecryptFileAndVerify(string inputFilePath, string outputFilePath, string publicKeyFilePath, string privateKeyFilePath, string passPhrase) { if (String.IsNullOrEmpty(inputFilePath)) { throw new ArgumentException(nameof(inputFilePath)); } if (String.IsNullOrEmpty(outputFilePath)) { throw new ArgumentException(nameof(outputFilePath)); } if (String.IsNullOrEmpty(publicKeyFilePath)) { throw new ArgumentException(nameof(publicKeyFilePath)); } if (String.IsNullOrEmpty(privateKeyFilePath)) { throw new ArgumentException(nameof(privateKeyFilePath)); } if (passPhrase == null) { passPhrase = String.Empty; } if (!File.Exists(inputFilePath)) { throw new FileNotFoundException(String.Format("Encrypted File [{0}] not found.", inputFilePath)); } if (!File.Exists(publicKeyFilePath)) { throw new FileNotFoundException(String.Format("Public Key File [{0}] not found.", publicKeyFilePath)); } if (!File.Exists(privateKeyFilePath)) { throw new FileNotFoundException(String.Format("Private Key File [{0}] not found.", privateKeyFilePath)); } ChoPGPEncryptionKeys encryptionKeys = new ChoPGPEncryptionKeys(publicKeyFilePath, privateKeyFilePath, passPhrase); if (encryptionKeys == null) { throw new ArgumentNullException("Encryption Key not found."); } using (Stream inputStream = File.OpenRead(inputFilePath)) { PgpPublicKeyEncryptedData publicKeyED = ExtractPublicKeyEncryptedData(inputStream); if (publicKeyED.KeyId != encryptionKeys.PublicKey.KeyId) { throw new PgpException(String.Format("Failed to verify file.")); } PgpObject message = GetClearCompressedMessage(publicKeyED, encryptionKeys); if (message is PgpCompressedData) { message = ProcessCompressedMessage(message); PgpLiteralData literalData = (PgpLiteralData)message; using (Stream outputFile = File.Create(outputFilePath)) { using (Stream literalDataStream = literalData.GetInputStream()) { Streams.PipeAll(literalDataStream, outputFile); } } } else if (message is PgpLiteralData) { PgpLiteralData literalData = (PgpLiteralData)message; using (Stream outputFile = File.Create(outputFilePath)) { using (Stream literalDataStream = literalData.GetInputStream()) { Streams.PipeAll(literalDataStream, outputFile); } } } else { throw new PgpException("Message is not a simple encrypted file."); } } return; }
private PgpObject GetClearCompressedMessage(PgpPublicKeyEncryptedData publicKeyED, ChoPGPEncryptionKeys encryptionKeys) { PgpObjectFactory clearFactory = GetClearDataStream(encryptionKeys.PrivateKey, publicKeyED); PgpObject message = clearFactory.NextPgpObject(); if (message is PgpOnePassSignatureList) { message = clearFactory.NextPgpObject(); } return(message); }