【发布时间】:2017-12-19 23:22:39
【问题描述】:
使用 Tcp 通过 LAN 发送和接收文件的简单程序
我想加密文件并使用对称和非对称密钥方法通过局域网发送它,首先是 AES 加密/解密,然后使用 RSA 加密/解密对称密钥
场景示例
johan 想要安全地向 Alice 发送一个文件,johan 必须使用他的用户名和密码登录到应用程序(如果已注册)。 如果 .not,johan 必须创建用户名和密码 如果是第一次注册, johan 必须生成一对密钥(公钥和私钥)。 公开公钥并保密私钥 要将文件发送给 Alice, johan 从应用程序生成一个随机密钥,用于使用 AES 对文件进行加密过程。 文件加密完成后, johan 使用 Alice 的 Published 公钥使用 RSA 加密密钥并发送文件。 用加密的密钥当 Alice 收到文件时, 她必须登录应用程序并使用她存储的私钥来解密 RSA 加密密钥。 之后她使用解密的密钥解密文件
这是我的代码
但是我很难理解 c# 中的加密库我不知道从哪里开始请任何人帮助我
图形界面
Groupbox called "Send" contain 2 textbox and 2 buttons
1 - type : textbox name : SrcFilePathTextBox for Path
2 - type : textbox name : DstAddressTextBox for Target IP
3 - type : Button name : SrcFilePathBrowseButton for open file dialog
4 - type : Button name : SendButton for start sending Process
groupbox called "receive" contain textbox and 2 buttons
1 - type : textbox name : LocalhostInfoTextBox for Show PC LAN INFO
2 - type : Button name : LocalhostInfoLoadButton for Put Info In textbox
3 - type : Button name : ReceiveWaitButton for start receiving Process
and at the end progress bar
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;
namespace JustSendItPrototype
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SrcFilePathBrowseButton_Click(object sender, EventArgs e)
{
if (SrcOpenFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
SrcFilePathTextBox.Text = SrcOpenFileDialog.FileName;
}
const int PORT = 32665;
const int BUF_SIZE = 65536;
private void ReceiveWaitButton_Click(object sender, EventArgs e)
{
try
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, 32665);
tcpListener.Start();
using (TcpClient tcpClient = tcpListener.AcceptTcpClient())
{
using (NetworkStream networkStream = tcpClient.GetStream())
{
using (BinaryReader reader = new BinaryReader(networkStream))
{
using (BinaryWriter writer = new BinaryWriter(networkStream))
{
string fileName = reader.ReadString();
long fileLength = reader.ReadInt64();
Debug.Print("FileName={0}, FileLength={1}", fileName, fileLength);
DstSaveFileDialog.FileName = fileName;
if (DstSaveFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
using (FileStream fileStream = new FileStream(DstSaveFileDialog.FileName, FileMode.Create))
{
if (fileLength > 0)
{
byte[] buf = new byte[BUF_SIZE];
long bytesLeft = fileLength;
while (bytesLeft > 0)
{
int bytesToTransfer = (int)Math.Min(bytesLeft, (long)BUF_SIZE);
Debug.Print("Reading {0} B", bytesToTransfer);
int bytesRead = reader.Read(buf, 0, bytesToTransfer);
Debug.Print("Read {0} B", bytesRead);
if (bytesRead > 0)
{
fileStream.Write(buf, 0, bytesRead);
bytesLeft -= bytesRead;
ProgressBar1.Value = 1000 - (int)(bytesLeft * 1000 / fileLength);
}
else
System.Threading.Thread.Sleep(30);
}
}
Debug.Print("Sending confirmation");
writer.Write((byte)1);
MessageBox.Show(this, "File received successfully.", "Receive File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Receive File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SendButton_Click(object sender, EventArgs e)
{
try
{
string srcFilePath = SrcFilePathTextBox.Text;
FileInfo fileInfo = new FileInfo(srcFilePath);
long fileLength = fileInfo.Length;
using (FileStream fileStream = new FileStream(srcFilePath, FileMode.Open))
{
using (TcpClient sendingClient = new TcpClient(DstAddressTextBox.Text, PORT))
{
using (NetworkStream sendingStream = sendingClient.GetStream())
{
using (BinaryWriter binaryWriter = new BinaryWriter(sendingStream))
{
using (BinaryReader binaryReader = new BinaryReader(sendingStream))
{
string fileName = Path.GetFileName(srcFilePath);
binaryWriter.Write(fileName);
binaryWriter.Write(fileLength);
Debug.Print("FileName={0}, FileLength={1}", fileName, fileLength);
if (fileLength > 0)
{
byte[] buf = new byte[BUF_SIZE];
long bytesLeft = fileLength;
while (bytesLeft > 0)
{
int bytesToTransfer = (int)Math.Min(bytesLeft, (long)BUF_SIZE);
fileStream.Read(buf, 0, bytesToTransfer);
Debug.Print("Sending {0} B", bytesToTransfer);
binaryWriter.Write(buf, 0, bytesToTransfer);
bytesLeft -= bytesToTransfer;
ProgressBar1.Value = 1000 - (int)(bytesLeft * 1000 / fileLength);
}
}
Debug.Print("Reading confirmation...");
byte answer = binaryReader.ReadByte();
if (answer == 1)
MessageBox.Show(this, "File sent successfully.", "Send File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Send File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LocalhostInfoLoadButton_Click(object sender, EventArgs e)
{
try
{
System.Text.StringBuilder sb = new StringBuilder();
string hostname = Dns.GetHostName();
sb.Append("Hostname: ");
sb.Append(hostname);
sb.Append("\r\n");
IPAddress[] addresses = Dns.GetHostAddresses(hostname);
foreach (IPAddress address in addresses)
{
sb.Append("IP: ");
sb.Append(address.ToString());
sb.Append("\r\n");
}
LocalhostInfoTextBox.Text = sb.ToString();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Send File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
【问题讨论】:
-
这里有问题吗?
-
@JamesKPolk 是什么?
标签: c# encryption public-key-encryption