zoukankan      html  css  js  c++  java
  • C# DES加密与解密算法实例

    引入System.Web.dll,用System.Web命名空间的
    pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5");

    Code

    先看下效果

    点加密
    加密出来了~
    然后点解密
    解密成功~就算你再次启动程序~也是一样可以的情况~
    代码如下~仔细看哦 ~~我不多说明了~
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    //添加
    using System.Globalization;
    using System.Security.Cryptography;

    namespace WindowsApplication1
    {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
              public string key="??^&$%^&";//随便写~
              public string s2;
              //// 创建Key
              //public string GenerateKey()
              //{
              //      DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
              //      return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
              //}
              // 加密字符串
              public string EncryptString(string sInputString, string sKey)
              {
                  byte[] data = Encoding.UTF8.GetBytes(sInputString);
                  DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                  DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                  DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                  ICryptoTransform desencrypt = DES.CreateEncryptor();
                  byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                  return BitConverter.ToString(result);
              }
              // 解密字符串
              public string DecryptString(string sInputString, string sKey)
              {
                  string[] sInput = sInputString.Split("-".ToCharArray());
                  byte[] data = new byte[sInput.Length];
                  for (int i = 0; i < sInput.Length; i++)
                  {
                      data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
                  }
                  DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                  DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                  DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                  ICryptoTransform desencrypt = DES.CreateDecryptor();
                  byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                  return Encoding.UTF8.GetString(result);
              }
            
              private void Form1_Load(object sender, EventArgs e)
              {
              }
              private void button1_Click(object sender, EventArgs e)
              {//加密
                  textBox2.Text = EncryptString(textBox1.Text, key);
              }
              private void button2_Click(object sender, EventArgs e)
              {//解密
                  textBox2.Text = DecryptString(textBox1.Text, key);
              }
          }
    }
    以上在VS2005中运行成功~^_^~~
    Code
  • 相关阅读:
    ctf web 百度杯”CTF比赛 九月场Upload i春秋
    ctf web 西普实验吧 登陆一下好吗 MySQL隐式转化 MySQL表达式的计算顺序
    Firefox 47.0.1
    给数组原型添加方法
    JS中几种常见的数组算法(前端面试必看)
    进制转换技巧解析
    redis通过6379端口无法连接服务器
    阿里云图片或文件上传 启动时报Error creating bean with name 'ossClient'问题
    20170628-三七互娱-测试工程师(提前批)
    20170514-vivo-软件工程师Java(提前批)
  • 原文地址:https://www.cnblogs.com/CCJVL/p/1367943.html
Copyright © 2011-2022 走看看