在分析结束之前已达到流的末尾. [英] End of stream reached before the end of analysis.

查看:75
本文介绍了在分析结束之前已达到流的末尾.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我必须将(已写入的)可序列化类加密到流(将成为ftp站点中的文件或文件)中,并将其加密(从流到类).

这是我写的代码:

加密代码:

Hi,
i have to encrypt a Serializable Class (that i have written) into a Stream (that will become a file or a file on a ftp site) and the opposite (from a stream to a class).

this is the code i have written:

Code to encrypt:

Out = new System.IO.FileStream("[Removed path]", System.IO.FileMode.Create);
Rijndael Alg = Rijndael.Create(); 
Alg.IV = ASCIIEncoding.ASCII.GetBytes(K);
Alg.Key = ASCIIEncoding.ASCII.GetBytes(K);
Alg.Padding = PaddingMode.Zeros;
CryptoStream streamC = new CryptoStream(Out, Alg.CreateEncryptor(), CryptoStreamMode.Write);
Form.Serialize(streamC, InstanceOfMyClass);
streamC.FlushFinalBlock();
streamC.Close();
Out.Close();



解密代码:



Code to decrypt:

In = new System.IO.FileStream("[Removed path]", System.IO.FileMode.Open, FileAccess.ReadWrite);
Rijndael Alg = Rijndael.Create();
Alg.IV = ASCIIEncoding.ASCII.GetBytes(K); 
Alg.Key = ASCIIEncoding.ASCII.GetBytes(K); 
Alg.Padding = PaddingMode.Zeros;
byte[] sorg = new byte[(int)In.Length];
In.Read(sorg, 0, (int)In.Length);
MemoryStream tmp = new MemoryStream();
CryptoStream streamC = new CryptoStream(tmp, Alg.CreateDecryptor(), CryptoStreamMode.Write);
streamC.Write(sorg, 0, sorg.Length);
streamC.FlushFinalBlock(); 
MyClass d = (MyClass)Form.Deserialize(tmp);
tmp.Close(); In.Close();
return d;



加密进行得很好,但是在反序列化(最后一行的第三行)上的解密操作给了我错误在分析结束之前到达流的末尾".我一直在努力寻找解决方案,但是一无所获.我该怎么办?

在此先感谢!



The encryption goes well, but the decription, at the Deserialization (at the third last line) gives me the error "End of stream reached before the end of analysis.". I have struggled to find a solution, but nothing. What can i do??

Thanks in advance!

推荐答案

我回答自己:
我不需要将字节从解密流传输到新流,然后反序列化新流.足以在读取模式下初始化密码流并从中反序列化:

I answer myself:
I don''t need to transfer bytes from the decrypted stream to a new stream, and then deserialize the new stream. It''s enough to initialize the cryptostream in read mode and to deserialize from it:

In = new System.IO.FileStream("[Removed path]", System.IO.FileMode.Open, FileAccess.ReadWrite);
Rijndael Alg = Rijndael.Create();
Alg.IV = ASCIIEncoding.ASCII.GetBytes(K); 
Alg.Key = ASCIIEncoding.ASCII.GetBytes(K); 
Alg.Padding = PaddingMode.Zeros;
Alg.Mode = CipherMode.CBC;

CryptoStream streamC = new CryptoStream(In, Alg.CreateDecryptor(), CryptoStreamMode.Read);
MyClass d = (MyClass)Form.Deserialize(streamCifrato);
In.Close();
return d;


这篇关于在分析结束之前已达到流的末尾.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆