我必须选择文件,将其分割为10kb,加密并通过网络流(tcp)发送。
在服务器端,我必须接收它,解密它,并将文件保存在其他位置。代码是工作的,它用byte[]传输所有字节,但如果我选择图像或word文档,您无法打开它,但它们都有相同的大小。我使用的是DiffieHellman和Aes的组合。
以下是服务器代码:
using (FileStream fs = new FileStream("D:\\" + name + "." + suffix, FileMode.Create))
{
for (int i = 0; i < Int32.Parse(howManyTimes - 1); i++)
{
byte[] data = Receive(ns);
byte[] decrypted = Decrypt(data, key, iv);
fs.Write(decrypted, 0, decrypted.Length);
Array.Clear(data, 0, decrypted.Length);
Send(ns, ok);
}
if (Int32.Parse(rest) > 0)
{
byte[] data = Receive(ns);
byte[] decrypted = Decrypt(data, key, iv);
fs.Write(decrypted, 0, Int32.Parse(rest));
Array.Clear(podatki, 0, Int32.Parse(rest));
Send(ns, ok);
}
fs.Close();
}
以下是客户端代码:
byte[] data= new byte[10000];
var fileStream = file.OpenFile();
using (Stream bufStream = new BufferedStream(fileStream, STD_MSG_SIZE))
{
for(int i = 0; i < Int32.Parse(howManyTimes); i++)
{
bufStream.Read(data, 0, STD_MSG_SIZE);
Send(ns, Encrypt(data, key, iv));
Array.Clear(data, 0, data.Length);
Receive(ns);
}
if (Int32.Parse(rest) > 0)
{
bufStream.Read(data, 0, Int32.Parse(ostanek));
Send(ns, Encrypt(data, key, iv));
Array.Clear(data, 0, data.Length);
Receive(ns);
}
}
转载请注明出处:http://www.songfuwangmfj.com/article/20230526/2001023.html