嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 5 元微信扫码支付:5 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
实践的加解密文件例子,附带传输,可签名验证
很有借鉴作用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using DESHelp;
using TransferFilesHelper;
using System.Threading;
using System.IO;
namespace ServerSide
{
public partial class ServerMain : Form
{
string strMD5 = string.Empty; //文件的MD5值,用于检验客户端发过来的文件
Thread threadListener;
delegate void SetTextBoxValue(string strPath); //设置richTextbox值的委托
public ServerMain()
{
InitializeComponent();
this.toolStripStatusLabelServerMain.Text = "加载完毕";
}
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
private int GetConnectedState()
{
int INTERNET_CONNECTION_MODEM = 1;
int INTERNET_CONNECTION_LAN = 2;
int INTERNET_CONNECTION_PROXY = 4;
int INTERNET_CONNECTION_MODEM_BUSY = 8;
int flags = 0;
/* connectionType变量内容
* 11(在线:拨号上网)
* 12(在线:通过局域网)
* 13(在线:代理)
* 14(MODEM被其他非INTERNET连接占用)
*/
int connectionType=-1;
#region 获取网络连接状态
bool state = InternetGetConnectedState(out flags, 0);
if (state)
{
if ((flags & INTERNET_CONNECTION_MODEM) == INTERNET_CONNECTION_MODEM)
{
connectionType = 11;
}
if ((flags & INTERNET_CONNECTION_LAN) == INTERNET_CONNECTION_LAN)
{
connectionType = 12;
}
if ((flags & INTERNET_CONNECTION_PROXY) == INTERNET_CONNECTION_PROXY)
{
connectionType = 13;
}
if ((flags & INTERNET_CONNECTION_MODEM_BUSY) == INTERNET_CONNECTION_MODEM_BUSY)
{
connectionType = 14;
}
}
#endregion
return connectionType;
}
private void SetRichTextBoxValue(string strPath)
{
if (this.richTextBoxValue.InvokeRequired) //控件是否跨线程
{
SetTextBoxValue setValue = new SetTextBoxValue(SetRichTextBoxValue);
this.richTextBoxValue.Invoke(setValue, strPath);
}
else
{
this.richTextBoxValue.LoadFile(strPath, RichTextBoxStreamType.PlainText);
this.richTextBoxValue.Update();
}
}
private void buttonMonitor_Click(object sender, EventArgs e)
{
threadListener = new Thread(new ThreadStart(this.StartingReceive));
threadListener.Start(); //启动监听线程
}
private void StartingReceive()
{
try
{
int intPort = Convert.ToInt32(this.textBoxPort.Text); //本机开设的端口号
string strIPAddress = string.Empty; //本机IP地址
/* 网络连接标志解释
* 11(在线:拨号上网)
* 12(在线:通过局域网)
* 13(在线:代理)
* 14(MODEM被其他非INTERNET连接占用)
*/
int connectionState = this.GetConnectedState();
#region 判断网络连接的类型
if (11 == connectionState)
{
IPAddress[] ipAddresses = this.GetLocalIPAddresses();
int countIPAddresses = ipAddresses.Length;
strIPAddress = ipAddresses[countIPAddresses / 2].ToString();
}
else if (12 == connectionState)
{
IPAddress[] ipAddresses = this.GetLocalIPAddresses();
int countIPAddresses = ipAddresses.Length;
strIPAddress = ipAddresses[countIPAddresses / 2].ToString();
}
else
{
MessageBox.Show("该模块暂时未处理,请联系开发人", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
#endregion
if (strIPAddress.Length > 0)
{
IPAddress ipAddress = IPAddress.Parse(strIPAddress);
IPEndPoint ipep = new IPEndPoint(ipAddress, intPort);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(ipep);
server.Listen(10);
this.toolStripStatusLabelServerMain.Text = "正在监听...";
Socket client = server.Accept();
this.toolStripStatusLabelServerMain.Text = "建立连接成功";
//IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
//string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
//string BagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
//int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));
//string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
string fileName = "接收的文件(加密)";
string fileaddr = "D:\\" fileName ".txt";
FileStream MyFileStream = new FileStream(fileaddr, FileMode.Create, FileAccess.Write);
//int SendedCount = 0;
while (true)
{
byte[] data = TransferFiles.ReceiveVarData(client);
if (data.Length < 1024)
{
MyFileStream.Write(data, 0, data.Length);
break;
}
else
{
// SendedCount ;
MyFileStream.Write(data, 0, data.Length);
}
}
MyFileStream.Close();
this.toolStripStatusLabelServerMain.Text = "接收完毕";
//给客户端返回信息
string strSendToClient = "OK";
byte[] bytesSendToClient = Encoding.GetEncoding("GB2312").GetBytes(strSendToClient);
client.Send(bytesSendToClient, bytesSendToClient.Length, 0); //向客户端发送信息
//接收客户端发过来的文件MD5值
byte[] bytesReceive = new byte[1024];
int intReceive = client.Receive(bytesReceive, bytesReceive.Length, 0);
strMD5=Encoding.GetEncoding("GB2312").GetString(bytesReceive, 0, intReceive);
client.Close();
MessageBox.Show("文件信息成功接收\n提示:加密文件保存在D:\\接收的文件(加密).txt", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.SetRichTextBoxValue("D:\\接收的文件(加密).txt");
this.toolStripStatusLabelServerMain.Text = "结束监听";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
threadListener.Abort();
return;
}
threadListener.Abort();
}
//private void StartingMonitor()
//{
// try
// {
// int intPort = Convert.ToInt32(this.textBoxPort.Text); //本机开设的端口号
// string strIPAddress = string.Empty; //本机IP地址
// /* 网络连接标志解释
// * 11(在线:拨号上网)
// * 12(在线:通过局域网)
// * 13(在线:代理)
// * 14(MODEM被其他非INTERNET连接占用)
// */
// int connectionState = this.GetConnectedState();
// #region 判断网络连接的类型
// if (11 == connectionState)
// {
// IPAddress[] ipAddresses = this.GetLocalIPAddresses();
// int countIPAddresses = ipAddresses.Length;
// strIPAddress = ipAddresses[countIPAddresses / 2].ToString();
// }
// else if (12 == connectionState)
// {
// IPAddress[] ipAddresses = this.GetLocalIPAddresses();
// int countIPAddresses = ipAddresses.Length;
// strIPAddress = ipAddresses[countIPAddresses / 2].ToString();
// }
// else
// {
// MessageBox.Show("该模块暂时未处理,请联系开发人", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
// return ;
// }
// #endregion
// if (strIPAddress.Length > 0)
// {
// IPAddress ipAddress = IPAddress.Parse(strIPAddress);
// IPEndPoint ipep = new IPEndPoint(ipAddress, intPort);
// mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// mySocket.Bind(ipep); //绑定套接字
// mySocket.Listen(0); //开始监听
// this.SetButtonEnable(true);
// //this.buttonStopMonitor.Enabled = true; //启用停止监听按钮
// this.toolStripStatusLabelServerMain.Text = "正在监听...";
// //接收来自客户端发回的信息
// Socket clientSocket = mySocket.Accept(); //接收客户端的信息
// string strReceive = "";
// byte[] bytesReceive = new byte[1024];
// int lengthBytesReceive = clientSocket.Receive(bytesReceive, bytesReceive.Length, 0); //接收来自客户端的信息
// strReceive = Encoding.GetEncoding("GB2312").GetString(bytesReceive, 0, lengthBytesReceive);
// this.SetRichTextBoxValue(strReceive);
// //this.richTextBoxValue.Text = strReceive;
// this.toolStripStatusLabelServerMain.Text = "已接收客户端信息";
// //给客户端返回信息
// string strSendToClient = "服务器已接收信息";
// byte[] bytesSendToClient = Encoding.GetEncoding("GB2312").GetBytes(strSendToClient);
// clientSocket.Send(bytesReceive, bytesReceive.Length, 0); //向客户端发送信息
// clientSocket.Close();
// mySocket.Close(); //关闭监听
// this.toolStripStatusLabelServerMain.Text = "结束监听";
// }
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
// return ;
// }
//}
private IPAddress[] GetLocalIPAddresses()
{
IPAddress[] ipAddresses=new IPAddress[]{};
try
{
string strHostName = Dns.GetHostName(); //获取本地计算机主机名
IPHostEntry localhost = Dns.GetHostEntry(strHostName);
ipAddresses = localhost.AddressList;
}
catch (SocketException se)
{
MessageBox.Show(se.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return ipAddresses;
}
private void buttonEncrypt_Click(object sender, EventArgs e)
{
try
{
string fileName = "接收的文件(加密)";
string strPath = "D:\\" fileName ".txt";
DES_ des = new DES_("江西财经大学");
string encryptedFilePath = "D:\\" fileName "(加密).txt";
des.Encrypt(strPath, encryptedFilePath);
MessageBox.Show("文件加密成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.richTextBoxValue.LoadFile(encryptedFilePath, RichTextBoxStreamType.PlainText);
this.richTextBoxValue.Update();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void buttonDecrypt_Click(object sender, EventArgs e)
{
try
{
string fileName = "接收的文件(加密)";
string strPath = "D:\\" fileName ".txt";
DES_ des = new DES_("江西财经大学");
des.Decrypt(strPath, "D:\\" fileName "(解密).txt");
MessageBox.Show("文件解密成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.richTextBoxValue.LoadFile("D:\\" fileName "(解密).txt", RichTextBoxStreamType.PlainText);
this.richTextBoxValue.Update();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
///
private void buttonCheckingFile_Click(object sender, EventArgs e)
{
string fileName = "接收的文件(加密)";
string fileaddr = "D:\\" fileName ".txt";
try
{
string strMD5=MD5Help.MD5_.GetFileMD5(fileaddr);
if (this.strMD5 == strMD5)
{
MessageBox.Show("文件验证成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//this.richTextBoxValue.LoadFile(fileaddr, RichTextBoxStreamType.PlainText);
//this.richTextBoxValue.Update();
return;
}
else
{
MessageBox.Show("文件验证失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.richTextBoxValue.Clear();
return;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
}