• 热门专题

C#使用DESCryptoServiceProvider做加解密功能

作者:邁向程式殿堂  发布日期:2013-05-21 11:36:58
  • 这边自己纪录一下简易的加解密功能,不然,每次要做加解密的Fucntion都还需要在查一下做法,这边的加解密功能主

    要是采用DES算法进行,透过DESCryptoServiceProvider类别实作,并用CryptoStream类别产生加解的结果

    在一开始实作前,需要加入下面几个Namespace

    using System.Security.Cryptography;
    using System.Text;
    using System.IO;使用DESCryptoServiceProvider类别,我们需要先定义好IV (对称算法的初始化向量)和Key(金钥)部分,当然这两个在加

    解密中都必须要一致,才有办法开锁跟闭锁。这边Key一定要等于8位

    public static string strKey = "12345678";
    public static string strIV = "Edward.K";字符串加密功能

    byte[] buffer = Encoding.UTF8.GetBytes(_strQ);
    MemoryStream ms = new MemoryStream();
    DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();_strQ为要加密的字符串,这边我们须转为byte,并且用UTF8进行编码。

    CryptoStream类别的用法:

     


     

    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();回传结果,必须在byte[]再转回为字符串

    Convert.ToBase64String(ms.ToArray()).Replace("+", "%");字符串解密功能

    解密部分其实很简单,只要把原本的CreateEncryptor改为CreateDecryptor就可以。不过,因为上面有进行Replace动作, www.it165.net

    所以,在解密时候也需要把这部分进行还原

    _strQ = _strQ.Replace("%", "+");此外,加密时候进行base64转换,解密时候也必须转换回来

    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();测试结果

    string str = "ABCDEF";
    Console.WriteLine(Encrypt(str));
    Console.WriteLine(Decrypt(Encrypt(str)));
    Console.Read();


     

    完整范例码

     

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="_strQ"></param>
    /// <returns></returns>
    public static 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("+", "%");
    }
    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="_strQ"></param>
    /// <returns></returns>
    public static 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());
    }
    

     

延伸阅读:

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规