BouncyCastle PGP-шифрование в памяти в C #
Я пытался использовать библиотеку BouncyCastle для шифрования / дешифрования PGP. У меня есть код, который мне нужно изменить, чтобы использовать только потоки - без файлов.
Я попытался удалить PgpUtilities.WriteFileToLiteralData () и затем заставить его возвращать поток, но он не работал (выходной поток был пустым).
Чтобы быть более понятным, вот какой метод должен быть:
public static Stream EncryptFile(MemoryStream inputStream, PgpPublicKey encKey, bool withIntegrityCheck)
Вот код, который мне нужно изменить:
private static void EncryptFile(Stream outputStream, string fileName, PgpPublicKey encKey, bool armor, bool withIntegrityCheck)
{
if (armor)
outputStream = new ArmoredOutputStream(outputStream);
try
{
MemoryStream bOut = new MemoryStream();
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(
CompressionAlgorithmTag.Zip);
PgpUtilities.WriteFileToLiteralData(
comData.Open(bOut),
PgpLiteralData.Binary,
new FileInfo(fileName));
comData.Close();
PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator(
SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());
cPk.AddMethod(encKey);
byte[] bytes = bOut.ToArray();
Stream cOut = cPk.Open(outputStream, bytes.Length);
cOut.Write(bytes, 0, bytes.Length);
cOut.Close();
if (armor)
outputStream.Close();
}
catch (PgpException e)
{
Console.Error.WriteLine(e);
Exception underlyingException = e.InnerException;
if (underlyingException != null)
{
Console.Error.WriteLine(underlyingException.Message);
Console.Error.WriteLine(underlyingException.StackTrace);
}
}
}
public void EncryptFile(string filePath, string publicKeyFile, string pathToSaveFile)
{
Stream keyIn, fos;
keyIn = File.OpenRead(publicKeyFile);
string[] fileSplit = filePath.Split('\\');
string fileName = fileSplit[fileSplit.Length - 1];
fos = File.Create(pathToSaveFile + fileName + ".asc");
EncryptFile(fos, filePath, ReadPublicKey(keyIn), true, true);
keyIn.Close();
fos.Close();
}