C# DES TCP加密

来源:互联网 发布:阿里云腾讯云对比性能 编辑:程序博客网 时间:2024/05/17 22:36

 

using System;using System.Text;using System.Windows.Forms;using System.Security.Cryptography;using System.IO;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        public byte[] Encrypt(string pToEncrypt)        {            byte[] rebyte;            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);            des.Key = new byte[] { 22, 85, 08, 13, 13, 15, 05, 38 };            des.IV = new byte[] { 22, 85, 08, 13, 13, 15, 05, 38 };            MemoryStream ms = new MemoryStream();            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);            cs.Write(inputByteArray, 0, inputByteArray.Length);            cs.FlushFinalBlock();            rebyte = ms.ToArray();            return rebyte;        }        private void button1_Click(object sender, EventArgs e)        {            MessageBox.Show(Encoding.Default.GetString(Decrypt(Encrypt("我操"))));        }        public byte[] Decrypt(byte[] inputByteArray)        {            byte[] rebyte;            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            des.Key = new byte[] { 22, 85, 08, 13, 13, 15, 05, 38 };            des.IV = new byte[] { 22, 85, 08, 13, 13, 15, 05, 38 };            MemoryStream ms = new MemoryStream();            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);            cs.Write(inputByteArray, 0, inputByteArray.Length);            cs.FlushFinalBlock();            rebyte = ms.ToArray();            return rebyte;        }    }}

将byte[]运用于socket即可