2

我需要使用 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();

我想知道我现在如何通过套接字发送这个加密的临时文件,以便接收者可以重建文件并解密它。有人可以给我一些教程或指南吗?谢谢大家

4

2 回答 2

1

您可以为套接字创建NetworkStream实例,然后调用encryptStream.CopyTo(myNetworkStream);

于 2013-05-18T14:31:52.373 回答
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将数据直接从加密流写入网络流。

这假设您不必解决安全密钥交换问题。

于 2013-05-18T14:32:39.653 回答