[英]Sending File Over LAN
我想使用對稱和非對稱密鑰方法對文件進行加密並通過lan發送,首先使用AES加密/解密,然后使用RSA加密/解密對稱密鑰
johan要安全地向Alice發送文件,johan必須使用其用戶名和密碼(如果已注冊)登錄應用程序。 如果不是,則johan必須創建用戶名和密碼如果是首次注冊,則johan必須生成密鑰對(公共密鑰和.Private密鑰)。 發布公共密鑰並將私有密鑰保密要將文件發送給Alice,johan會從應用程序生成一個隨機密鑰,以用於使用AES加密文件的過程。 文件加密完成后,johan使用Alice的Published public密鑰使用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);
}
}
}
}
我現在不在電腦旁,但是完成方案的最簡單方法是在兩端使用SslStream。
一旦有傳入連接,就在服務器(Johan)上,從TcpStream創建一個SslStream。 在服務器上,您將使用SslStream.AuthenticateAsServer(),此方法接受X509Certificate(這將是您的私鑰)。
在客戶端(愛麗絲)上,您將改為使用SslStream.AuthenticateAsClient。
對於這兩種方法,您都可以傳遞證書驗證回調。 在您的情況下,您只能在客戶端執行此操作,並檢查給定的證書是否是Johan提供的證書。
編輯:我已經重讀了您的問題,並且按照您的要求,我創建了一個示例 ,該示例首先發送使用RSA加密的密鑰,然后使用該密鑰對以下數據進行加密
收到一些數據后,首先將密鑰解密,然后再用於AES解密。 這是一個非常粗糙的示例,我沒有注冊和登錄,並且存儲了證書,因此它們都有私鑰和公鑰,但是應該足以滿足您的目標
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.