C# DES TCP加密
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即可
摘自 lihongdian的专栏
补充:综合编程 , 安全编程 ,