實現簡單的資料加密-可直接引用

來源:互聯網
上載者:User

  //要加密的檔案,加密後的檔案,加密的key值

public static bool DescryptDES(string inf, string ouf, string encrypKey)
    {
        byte[] rgbIv = Keys; //加密的IV向量,使用者自己定義一個,這裡就沒直接給出{0x25}
        byte[] rgbKey = Encoding.UTF8.GetBytes(encrypKey.Substring(0, 8));
        FileStream infs = new FileStream(inf, FileMode.Open, FileAccess.Read);
        FileStream outfs = new FileStream(ouf, FileMode.OpenOrCreate, FileAccess.Write);
        //讀的流放一個位置,我們知道讀的流都是二進位的
        byte[] bytein = new byte[100];
        long readLen = 0;
        long toatalLen = inf.Length;
        int erentLen;//每次讀的步長
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(outfs, des.CreateEncryptor(rgbKey, rgbIv), CryptoStreamMode.Write);
        while(readLen<toatalLen)
        {
           erentLen= infs.Read(bytein, 0, 100);//每次讀取的布長,因為他都迴圈
           encStream.Write(bytein, 0, erentLen);
           readLen = readLen + erentLen;
        }
        encStream.Close();
        infs.Close();
        outfs.Close();
        return true; //加密成功
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.