BouncyCastle In-Memory-PGP-Verschlüsselung in C #

Ich habe versucht, die BouncyCastle-Bibliothek für die PGP-Verschlüsselung / -Entschlüsselung zu verwenden. Ich habe Code, den ich ändern muss, um nur Streams zu verwenden - keine Dateien.

Ich habe versucht, PgpUtilities.WriteFileToLiteralData () zu entfernen und dann einen Stream zurückzugeben, aber es hat nicht funktioniert (der Ausgabestream war leer).

Um es klarer zu machen:

public static Stream EncryptFile(MemoryStream inputStream, PgpPublicKey encKey, bool withIntegrityCheck)

Hier ist der Code, den ich ändern muss:

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();
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage