【ADO.NET基础-数据加密】第一篇(加密解密篇)

可以采用下面的函数实现密码的加密 
public static string EncryptString(string str)
    {
        //密文
        string key = "www";

        //把密文转成二进制型

        byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key);


        //把字符串转成二进制型

        byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str);

        //生成DESCryptoServiceProvider对象
        System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();

     
        //设定
        des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
        des.IV = ResizeBytesArray(bytesKey, des.IV.Length);

        //写出被加密的数据的MemoryStream
        System.IO.MemoryStream msOut = new System.IO.MemoryStream();

        //生成DES加密对象
        System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateEncryptor();

        //生成CryptoStream
        System.Security.Cryptography.CryptoStream cryptStreem =
            new System.Security.Cryptography.CryptoStream(msOut,
            desdecrypt,
            System.Security.Cryptography.CryptoStreamMode.Write);

        //写入
        cryptStreem.Write(bytesIn, 0, bytesIn.Length);
        cryptStreem.FlushFinalBlock();

        //取得加密后的数据
        byte[] bytesOut = msOut.ToArray();

        //关闭
        cryptStreem.Close();
        msOut.Close();

        //返回
        return System.Convert.ToBase64String(bytesOut);
    }

 可以采用下面的函数实现密码的解密 
    /// <summary>
    /// 对数据解密


    /// </summary>
    /// <param name="str">被加密的数据</param>
    /// <returns>解密后的数据</returns>
    public static string DecryptString(string str)
    {
        //秘文
        string key = "www";

        string result = "";
        //把密文转成二进制型
        byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key);

        //生成DESCryptoServiceProvider对象
        System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();

      
        //设定
        des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
        des.IV = ResizeBytesArray(bytesKey, des.IV.Length);

        //返回
        byte[] bytesIn = System.Convert.FromBase64String(str);

        //生成MemoryStream对象以便读出被加密的数据
        System.IO.MemoryStream msIn = new System.IO.MemoryStream(bytesIn);

        //生成DES复原对象
        System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor();

        //生成CryptoStream对象以便读入数据
        System.Security.Cryptography.CryptoStream cryptStreem =
            new System.Security.Cryptography.CryptoStream(msIn,
            desdecrypt,
            System.Security.Cryptography.CryptoStreamMode.Read);

        //生成StreamReader以便取得解密后的数据
        System.IO.StreamReader srOut = new System.IO.StreamReader(cryptStreem, System.Text.Encoding.UTF8);

        //取得解密后的数据
        result = srOut.ReadToEnd();

        //关闭
        srOut.Close();
        cryptStreem.Close();
        msIn.Close();

        return result;
    }

    /// <summary>
    /// 使用共有key变更二进制的数据的大小


    /// </summary>
    /// <param name="bytes">变更二进制的数据的大小</param>
    /// <param name="newSize">二进制的数据的大小</param>
    /// <returns>变更后的二进制列</returns>
    private static byte[] ResizeBytesArray(byte[] bytes, int newSize)
    {
        byte[] newBytes = new byte[newSize];
        if (bytes.Length < newSize)
        {
            for (int i = 0; i < bytes.Length; i++)
                newBytes[i] = bytes[i];
        }
        else
        {
            int pos = 0;
            for (int i = newSize; i < bytes.Length; i++)
            {
                newBytes[pos++] ^= bytes[i];
                if (pos >= newBytes.Length)
                    pos = 0;
            }
        }

 

posted @ 2017-07-06 16:14  OldELeven  阅读(299)  评论(0编辑  收藏  举报