加密文件并发送 [英] Encrypt a file and send it through

查看:212
本文介绍了加密文件并发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用AES 192加密文件,并通过套接字将其发送给客户端。
我正在使用这个代码加密文件:

I need to encrypt a file with AES 192 and send it to a client via socket. I'm using this code to encrypt the file:

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();

我想知道如何可以通过套接字发送此加密的临时文件,以便接收器可以重构文件并进行解密。
有人可以给我一些教程或指南吗?
非常感谢所有提前

I'm wondering how I can now send this encrypted temporary file through the socket, so that the receiver can reconstruct the file and decrypt it. Can someone give me some tutorial or guide ? Thank you all in advance

推荐答案

考虑使用 TcpClient 连接到服务器并发送数据。我不会写出一个完整的答案,因为你表示这是学校的工作,但看看这个例子如何写数据:

Consider using TcpClient to connect to the server and send the data. I'm not going to write a full answer as you've indicated this is school work, but look at how the example writes data:

// 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 将数据直接从加密流中写入网络流。

You might want to avoid tweak it slightly to use CopyTo to write the data directly from the crypto stream onto the network stream.

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

This assumes you don't have to solve the problem of secure key exchange.

这篇关于加密文件并发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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