DESCryptoServiceProvider加密解密的简单使用例子

  DES.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace Wpfbinding
{
    class DES
    {
        DESCryptoServiceProvider des;
        public DES()
        {
            des = DESCryptoServiceProvider.Create() as DESCryptoServiceProvider;
            des.IV = des.Key;
        }
        public string Key
        {
            get { return Convert.ToBase64String(des.Key); }
            set { des.Key = Convert.FromBase64String(value); }
        }
        // 加密字符串   
        public string Encode(string sInputString)
        {
            byte[] data = Encoding.UTF8.GetBytes(sInputString);
            ICryptoTransform desEncrypt = des.CreateEncryptor();
            byte[] result = desEncrypt.TransformFinalBlock(data, 0, data.Length);
            return Convert.ToBase64String(result);
        }
        // 解密字符串   
        public string Decode(string sInputString)
        {
            byte[] data = Convert.FromBase64String(sInputString);
            ICryptoTransform desDecrypt = des.CreateDecryptor();
            byte[] result = desDecrypt.TransformFinalBlock(data, 0, data.Length);
            return Encoding.UTF8.GetString(result);
        }   
    }
}

.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wpfbinding
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {

            InitializeComponent();
            testc();
        }
        void testc()
        {
            DES des = new DES(); string str = "测试,Hello Word";
            textBlock1.Text = str;
            textBlock2.Text = des.Encode(str);
            textBlock3.Text = des.Decode(des.Encode(str));
        }
    }
}

 DES内置key加特殊字符处理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace Wpfbinding
{
    class DES
    {
    public string strKey = "12345678";
    public string strIV = "Edward.K";
    public string Encrypt(string _strQ)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(_strQ);
        MemoryStream ms = new MemoryStream();
        DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(ms, tdes.CreateEncryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);
        encStream.Write(buffer, 0, buffer.Length);
        encStream.FlushFinalBlock();
        return Convert.ToBase64String(ms.ToArray()).Replace("+", "%");
    }
    public string Decrypt(string _strQ)
    {
        _strQ = _strQ.Replace("%", "+");
        byte[] buffer = Convert.FromBase64String(_strQ);
        MemoryStream ms = new MemoryStream();
        DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(ms, tdes.CreateDecryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);
        encStream.Write(buffer, 0, buffer.Length);
        encStream.FlushFinalBlock();
        return Encoding.UTF8.GetString(ms.ToArray());
    }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值