des加密(c#实现)

 private string Encrypt(string datastr, string keystr)
    {
        DESCryptoServiceProvider desc = new DESCryptoServiceProvider();

        byte[] key = System.Text.Encoding.ASCII.GetBytes(keystr);
        byte[] data = System.Text.Encoding.Unicode.GetBytes(datastr);

        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);

        cs.Write(data, 0, data.Length);
        cs.FlushFinalBlock();

        return System.Convert.ToBase64String(ms.ToArray());
    }
    private string Decrypt(string datastr, string keystr)
    {
        byte[] inputByteArray = new Byte[datastr.Length];

        inputByteArray = Convert.FromBase64String(datastr);
        DESCryptoServiceProvider desc = new DESCryptoServiceProvider();

        byte[] key = System.Text.Encoding.ASCII.GetBytes(keystr);
       

        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write);

        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        System.Text.Encoding encoding = new System.Text.ASCIIEncoding();
        return encoding.GetString(ms.ToArray());
    }

    //复杂加密  解密

    /// <summary>
    /// 一次加密
    /// </summary>
    /// <param name="pToEncrypt">加密内容</param>
    /// <returns></returns>
    private string encrypt1(string pToEncrypt)
    {
        string EncryptString = "";
        try
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(secretKey1);
            des.IV = ASCIIEncoding.ASCII.GetBytes(secretKey1);
            MemoryStream ms = new 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);
            }
            EncryptString = encrypt2(ret.ToString());
        }
        catch
        {
            EncryptString = "EncryptFailing";
        }
        return EncryptString;
    }

    /// <summary>
    /// 二次加密
    /// </summary>
    /// <param name="pToEncrypt">加密内容</param>
    /// <returns></returns>
    private string encrypt2(string pToEncrypt)
    {
        string EncryptString = "";
        try
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(secretKey2);
            des.IV = ASCIIEncoding.ASCII.GetBytes(secretKey2);
            MemoryStream ms = new 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);
            }
            EncryptString = ret.ToString();
        }
        catch
        {
            EncryptString = "EncryptFailing";
        }
        return EncryptString;
    }

    /// <summary>
    /// 一次解密
    /// </summary>
    /// <param name="pToDecrypt">解密内容</param>
    /// <returns></returns>
    private string Decrypt1(string pToDecrypt)
    {
        string EncryptString = "";
        MemoryStream ms = new MemoryStream();
        try
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(secretKey2);
            des.IV = ASCIIEncoding.ASCII.GetBytes(secretKey2);
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            EncryptString = Decrypt2(System.Text.Encoding.Default.GetString(ms.ToArray()));
        }
        catch
        {
            EncryptString = "DecryptFailing";
        }
        return EncryptString;
    }

    /// <summary>
    /// 二次解密
    /// </summary>
    /// <param name="pToDecrypt">解密内容</param>
    /// <returns></returns>
    private string Decrypt2(string pToDecrypt)
    {
        string EncryptString = "";
        MemoryStream ms = new MemoryStream();
        try
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(secretKey1);
            des.IV = ASCIIEncoding.ASCII.GetBytes(secretKey1);
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            EncryptString = System.Text.Encoding.Default.GetString(ms.ToArray());
        }
        catch
        {
            EncryptString = "DecryptFailing";
        }
        return EncryptString;
    }

 

    protected void encry_Click(object sender, EventArgs e)
    {
        string encryText = this.TextBox1.Text.Trim();
        this.decry.Text = encrypt1(encryText);
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string decryText = this.decry.Text.Trim();
        this.secretKey1 = this.TextBox3.Text;
        this.Label1.Text = Decrypt1(decryText);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = createCon();
        SqlCommand cmd = new SqlCommand();
        con.Open();
        int t1 = DateTime.Now.Minute*60*1000+DateTime.Now.Second*1000+DateTime.Now.Millisecond;
       
        for (int i = 0; i < 500; i++)
        {
            string unencryText="skfjiwpa;;PDKJEFI";
            string encryText = Encrypt(unencryText, "kewidgkd");
            cmd.Connection = con;
            cmd.CommandText = "insert into password values('" + encryText + "')";
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
       
        int t2 = DateTime.Now.Minute * 60 * 1000 + DateTime.Now.Second * 1000 + DateTime.Now.Millisecond;
        con.Close();
        this.Label5.Text = Convert.ToString(t2 - t1);
    } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值