c# des 加密解密

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;

namespace Test
{
    /// <summary>
    /// DES加密/解密类
    /// </summary>
    class ClassDES
    {
        public ClassDES()
        {           
        }
        /// <summary>
        /// 密钥
        /// </summary>
        /// <param name="text">密钥文本</param>
        /// <param name="type">类型(sha1或md5)</param>
        /// <returns></returns>
        public static string SetKey(string text,string type)
        {
            if (type == "md5")
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "md5").ToLower();
            }
            else
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "sha1").ToLower();
            }
        }

        //========加密========#region ========加密========
 
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="text">要加密的文本</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string Encrypt(string text,string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray=Encoding.Default.GetBytes(text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms=new System.IO.MemoryStream();
            CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
            cs.Write(inputByteArray,0,inputByteArray.Length);
            cs.FlushFinalBlock();
           StringBuilder ret=new StringBuilder();
            foreach( byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}",b);
            }
            return ret.ToString();
        }
       
        //========解密========#region ========解密========

        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="text">解密文本</param>
        /// <param name="sKey">解密密钥</param>
        /// <returns></returns>
        public static string Decrypt(string text,string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len=text.Length/2;
            byte[] inputByteArray = new byte[len];
            int x,i;
            for(x=0;x<len;x++)
            {
                i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
                inputByteArray[x]=(byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms=new System.IO.MemoryStream();
            CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
            cs.Write(inputByteArray,0,inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C#进行DES加密解密的代码示例: ```csharp using System; using System.Security.Cryptography; using System.Text; public class DESExample { public static string Encrypt(string plainText, byte[] key, byte[] iv) { byte[] encrypted; using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = key; des.IV = iv; ICryptoTransform encryptor = des.CreateEncryptor(des.Key, des.IV); using (var memoryStream = new System.IO.MemoryStream()) { using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { using (var streamWriter = new System.IO.StreamWriter(cryptoStream)) { streamWriter.Write(plainText); } encrypted = memoryStream.ToArray(); } } } return Convert.ToBase64String(encrypted); } public static string Decrypt(string cipherText, byte[] key, byte[] iv) { byte[] decrypted; using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = key; des.IV = iv; ICryptoTransform decryptor = des.CreateDecryptor(des.Key, des.IV); using (var memoryStream = new System.IO.MemoryStream(Convert.FromBase64String(cipherText))) { using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { using (var streamReader = new System.IO.StreamReader(cryptoStream)) { decrypted = Encoding.UTF8.GetBytes(streamReader.ReadToEnd()); } } } } return Encoding.UTF8.GetString(decrypted); } } ``` 使用示例: ```csharp byte[] key = Encoding.UTF8.GetBytes("01234567"); byte[] iv = Encoding.UTF8.GetBytes("abcdefgh"); string plainText = "Hello, World!"; string encryptedText = DESExample.Encrypt(plainText, key, iv); Console.WriteLine($"Encrypted: {encryptedText}"); string decryptedText = DESExample.Decrypt(encryptedText, key, iv); Console.WriteLine($"Decrypted: {decryptedText}"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值