当前位置:编程学习 > 网站相关 >>

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的专栏
补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,