我需要使用AES 192加密文件并通过套接字将其发送到客户端。 我正在使用此代码加密文件:
string outputFile = "crypted";
//Confidentiality
RijndaelManaged AES192Confidentiality = new RijndaelManaged();
AES192Confidentiality.KeySize = 192;
AES192Confidentiality.BlockSize = 192;
AES192Confidentiality.IV = ConfIV;
AES192Confidentiality.Key = ConfKey;
AES192Confidentiality.Mode = CipherMode.CBC;
FileStream inputFileStream = new FileStream(par.GetFilePath(), FileMode.Open, FileAccess.Read);
FileStream outputFileStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
byte[] inputFileData = new byte[(int)inputFileStream.Length];
inputFileStream.Read(inputFileData, 0, (int)inputFileStream.Length);
CryptoStream encryptStream = new CryptoStream(outputFileStream, AES192Confidentiality.CreateEncryptor(), CryptoStreamMode.Write);
encryptStream.Write(inputFileData, 0, (int)inputFileStream.Length);
encryptStream.FlushFinalBlock();
encryptStream.Close();
我想知道我现在如何通过套接字发送这个加密的临时文件,以便接收者可以重建文件并解密它。 有人可以给我一些教程或指南吗? 提前谢谢大家
答案 0 :(得分:1)
您可以为套接字创建NetworkStream的实例,然后调用encryptStream.CopyTo(myNetworkStream);
答案 1 :(得分:1)
考虑使用TcpClient连接到服务器并发送数据。我不打算写一个完整的答案,因为你已经指出这是学校的工作,但看看这个例子如何写数据:
// Get a client stream for reading and writing.
NetworkStream networkStream = client.GetStream();
// Send the message to the connected TcpServer.
networkStream.Write(data, 0, data.Length);
您可能希望避免稍微调整一下,以便使用CopyTo将数据直接从加密流写入网络流。
这假设您不必解决secure key exchange的问题。