Encriptación PGP en memoria BouncyCastle en C #
He estado tratando de usar la biblioteca BouncyCastle para hacer cifrado / descifrado PGP. Tengo un código que necesito modificar para usar solo secuencias, sin archivos.
Intenté eliminar PgpUtilities.WriteFileToLiteralData () y luego hacer que devolviera una secuencia, pero no funcionó (la secuencia de salida estaba vacía).
Para ser más claro, aquí está cuál debería ser el método:
public static Stream EncryptFile(MemoryStream inputStream, PgpPublicKey encKey, bool withIntegrityCheck)
Aquí está el código que necesito modificar:
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();
}