基本信息
源码名称:混合加密网络聊天传输程序(聊天消息加密解密)
源码大小:0.13M
文件格式:.zip
开发语言:C#
更新时间:2018-12-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在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.IO; using System.Security.Cryptography; using System.Threading; namespace 加密程序__服务器端 { public partial class Form1 : Form { private Socket socket;//监听套接字 private Socket clientSocket;//通信套接字 private Thread thread; private string SAKey; private RSACryptoServiceProvider rsa; private byte[] SAIV = { 0xFE, 0xA1, 0x23, 0x4E, 0xBC, 0x1B, 0x32, 0xEF }; public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; this.listBoxState.Items.Clear();//状态栏清空 this.richTextBoxAccept.Text = "";//接收栏清空 this.txtSend.Text = "";//发送栏清空 } private void btnStart_Click(object sender, EventArgs e)//开始监听(建立连接-->获得客户端公钥-->加密对称密钥并发送) { try { SAKey = this.txtpwd.Text;//输入对称加密密钥 if (SAKey.Length != 8) { MessageBox.Show("请输入8位密码"); this.Close(); } IPEndPoint server = new IPEndPoint(IPAddress.Parse(this.textBoxIP.Text), Int32.Parse(this.textBoxPort.Text));//端点 this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.socket.Bind(server); this.socket.Listen(10); this.clientSocket = socket.Accept(); this.listBoxState.Items.Add("与客户" clientSocket.RemoteEndPoint.ToString() "建立连接");//Tcp建立连接 this.btnStart.Enabled = false; getClientPublicKey();//接收客户端发来RSA公钥 encryptAndSendSymmetricKey();//加密DES对称密钥 并发送 } catch { MessageBox.Show(""); } thread = new Thread(new ThreadStart(AcceptMessage));//接收信息 thread.Start(); } private void btnStop_Click(object sender, EventArgs e)//停止监听 { try { this.socket.Shutdown(SocketShutdown.Both);//在套接字上停止发送和接收 this.socket.Close();//关闭监听套接字 this.clientSocket.Close();//关闭通信套接字 this.thread.Abort();//终止线程 this.btnStart.Enabled = true; this.btnStop.Enabled = false; } catch (Exception ee) { MessageBox.Show(ee.Message); } } private void btnSend_Click(object sender, EventArgs e)//发送 { try { if (this.txtSend.Text == "") { MessageBox.Show("无法送信息"); return; } string str = EncryptDES(this.txtSend.Text);//DES加密 TransData.SendVarData(clientSocket, Encoding.UTF8.GetBytes(str));//发送加密的信息 this.richTextBoxAccept.AppendText("发送:" this.txtSend.Text Environment.NewLine);//显示 this.txtSend.Text = "";//清空 } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void getClientPublicKey()//获得客户端公钥 { Byte[] message = TransData.ReceiveVarData(clientSocket); this.rsa = new RSACryptoServiceProvider(); this.rsa.KeySize = 1024; this.rsa.FromXmlString(Encoding.UTF8.GetString(message)); } private void encryptAndSendSymmetricKey()//加密对称密钥 并发送 { byte[] symKeyEncrypted = rsa.Encrypt(Encoding.UTF8.GetBytes(SAKey), false); byte[] symIVEncrypted = rsa.Encrypt(SAIV, false); TransData.SendVarData(clientSocket, symKeyEncrypted); TransData.SendVarData(clientSocket, symIVEncrypted); } private string DecryptDES(string decryptString)//DES解密字符串 { if (decryptString == "") return ""; try { byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, DCSP.CreateDecryptor(Encoding.UTF8.GetBytes(this.SAKey), this.SAIV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.UTF8.GetString(ms.ToArray()); } catch (Exception ee) { MessageBox.Show(ee.Message); return decryptString; } } public string EncryptDES(string encryptString)//DES加密字符串 { try { byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, DCSP.CreateEncryptor(Encoding.UTF8.GetBytes(this.SAKey), this.SAIV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (Exception ee) { MessageBox.Show(ee.Message); return encryptString; } } private void AcceptMessage()//接收信息 { while (true) { try { byte[] message = TransData.ReceiveVarData(this.clientSocket); string str = Encoding.UTF8.GetString(message); this.richTextBoxAccept.AppendText("收到:" this.DecryptDES(str) Environment.NewLine);//接收信息并解密 } catch (Exception ee) { MessageBox.Show(ee.Message); break; } } } private void button1_Click(object sender, EventArgs e) { this.Close(); } } } //信息对称加密 EncryptDES(string encryptString) //对称加密密钥 使用RSA非对称发送 //所以服务器需要客户端公钥 getClientPublicKey(),然后加密对称密钥发送给客户端 encryptAndSendSymmetricKey //服务器要通过接收到的信息(客户端通过收到的对称密钥[服务器本身就有]加密过的密文)进行对称解密