Bouncycastle PGP 解密期间的 PartialInputStream
我正在尝试解密刚刚使用 bouncycastle 加密的文件,但出现此异常:
Premature end of stream in PartialInputStream
我正在使用 bouncycastle 中的示例代码,并且没有更改任何内容。
当我使用这段代码进行加密时,我得到了这个:
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();
}
我认为它与 PgpLiteralDataGenerator
有关。 但我需要使用它,因为我想加密字节数组中的数据,而不是文件中的数据。还有其他方法可以做到这一点吗?
I am tryng to decrypt a file I just encrypted using bouncycastle, but im getting this exception:
Premature end of stream in PartialInputStream
I am using the example code from bouncycastle and haven't changed anything.
I'm getting this when I use this code for encryption:
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();
}
I think it has something to do with the PgpLiteralDataGenerator
.
But I need to use it because I want to encrypt data from a byte array, not from a file. Is there some other way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何正在努力的人都会收到一个工作代码:
Anybody who is struggeling heres a working code:
当我尝试从“流”中读取时,我遇到了同样的错误。
我的解决方案是将流复制到新的 .net Stream 对象
就像
MemoryStream
。然后从内存流对象中读取。因此,我的基本流类型与 Org.BouncyCastle.Bcpg 流类型无关。
I got the same error, when I tried to read from the 'stream'.
A solution for me was to copy the stream to a new .net Stream object
like
MemoryStream
. And after that read from the memoryStream object.So, my base stream type has nothing to do with
Org.BouncyCastle.Bcpg
stream types.