PartialInputStream durante el descifrado de Bouncycastle PGP
ntento descifrar un archivo que acabo de cifrar usando bouncycastle, pero obtengo esta excepción:
Premature end of stream in PartialInputStream
Estoy usando el código de ejemplo de bouncycastle y no he cambiado nada.
Recibo esto cuando uso este código para el cifrado:
private static byte[] EncryptFile(byte[] clearData, string fileName, PgpPublicKey encKey, bool withIntegrityCheck)
{
MemoryStream encOut = new MemoryStream();
try
{
MemoryStream bOut = new MemoryStream();
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator( CompressionAlgorithmTag.Zip );
//PgpUtilities.WriteFileToLiteralData(
// comData.Open(bOut),
// PgpLiteralData.Binary,
// new FileInfo(fileName));
Stream cos = comData.Open(bOut);
PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator();
Stream pOut = lData.Open(
cos,
PgpLiteralData.Binary,
fileName,
clearData.Length,
DateTime.UtcNow
);
lData.Close();
comData.Close();
PgpEncryptedDataGenerator cPk = new PgpEncryptedDataGenerator( SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom() );
cPk.AddMethod(encKey);
byte[] bytes = bOut.ToArray();
Stream os = encOut;
Stream cOut = cPk.Open(os, bytes.Length);
cOut.Write(bytes, 0, bytes.Length);
cOut.Close();
encOut.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);
}
}
return encOut.ToArray();
}
Creo que tiene algo que ver con laPgpLiteralDataGenerator
. Pero necesito usarlo porque quiero cifrar datos de una matriz de bytes, no de un archivo. ¿Hay alguna otra forma de hacer esto?