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)) { PgpSignatureGenerator signatureGenerator = InitSignatureGenerator(compressedOut, encryptionKeys); using (Stream literalOut = ChainLiteralOut(compressedOut, unencryptedFileInfo)) { using (FileStream inputFileStream = unencryptedFileInfo.OpenRead()) { WriteOutputAndSign(compressedOut, literalOut, inputFileStream, signatureGenerator); inputFileStream.Close(); } } } } }
/// <summary> /// ArmoredOutputStream, Base64'e benzer bir kodlama kullanır, /// böylece ikili yazdırılamaz baytlar metin dostu bir şeye dönüştürülür. /// Verileri e-postayla göndermek, bir siteye veya başka bir metin ortamına göndermek istiyorsanız bunu yaparsınız. /// 'Güvenlik açısından bir fark yaratmaz. /// </summary> /// <param name="armor"></param> public static void EncryptAndSign(string inputFile, string outputFile, string publicKeyFile, string privateKeyFile, string passPhrase, bool armor) { PgpEncryptionKeys encryptionKeys = new PgpEncryptionKeys(publicKeyFile, privateKeyFile, passPhrase); if (!File.Exists(inputFile)) { throw new FileNotFoundException(String.Format("Şifrelenecek dosya [{0}] bulunamadı.", inputFile)); } if (!File.Exists(publicKeyFile)) { throw new FileNotFoundException(String.Format("Public Key dosyası [{0}] bulunamadı.", publicKeyFile)); } if (!File.Exists(privateKeyFile)) { throw new FileNotFoundException(String.Format("Private Key dosyası [{0}] bulunamadı.", privateKeyFile)); } if (String.IsNullOrEmpty(passPhrase)) { throw new ArgumentNullException("Geçersiz şifre."); } if (encryptionKeys == null) { throw new ArgumentNullException("Şifreleme için anahtarlar bulunamadı."); } using (Stream outputStream = File.Create(outputFile)) { if (armor) { using (ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream)) { OutputEncrypted(inputFile, armoredOutputStream, encryptionKeys); } } else { OutputEncrypted(inputFile, outputStream, encryptionKeys); } } }
private static PgpSignatureGenerator InitSignatureGenerator(Stream compressedOut, PgpEncryptionKeys m_encryptionKeys) { const bool IsCritical = false; const bool IsNested = false; PublicKeyAlgorithmTag tag = m_encryptionKeys.SecretKey.PublicKey.Algorithm; PgpSignatureGenerator pgpSignatureGenerator = new PgpSignatureGenerator(tag, HashAlgorithmTag.Sha1); pgpSignatureGenerator.InitSign(PgpSignature.BinaryDocument, m_encryptionKeys.PrivateKey); foreach (string userId in m_encryptionKeys.SecretKey.PublicKey.GetUserIds()) { PgpSignatureSubpacketGenerator subPacketGenerator = new PgpSignatureSubpacketGenerator(); subPacketGenerator.SetSignerUserId(IsCritical, userId); pgpSignatureGenerator.SetHashedSubpackets(subPacketGenerator.Generate()); // Just the first one! break; } pgpSignatureGenerator.GenerateOnePassVersion(IsNested).Encode(compressedOut); return(pgpSignatureGenerator); }