最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#對文件進(jìn)行加密解密代碼

 更新時間:2015年07月08日 11:05:08   投稿:hebedich  
本文給大家分享的是使用C#對文件進(jìn)行加密解密的代碼,十分的簡單實(shí)用,有需要的小伙伴可以參考下。

加密代碼

using System;
using System.IO;
using System.Security.Cryptography;
  
public class Example19_9
{
  public static void Main()
  {
  
    // Create a new file to work with
    FileStream fsOut = File.Create(@"c:\temp\encrypted.txt");
  
    // Create a new crypto provider
    TripleDESCryptoServiceProvider tdes =
      new TripleDESCryptoServiceProvider();
  
    // Create a cryptostream to encrypt to the filestream
    CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(),
      CryptoStreamMode.Write);
  
    // Create a StreamWriter to format the output
    StreamWriter sw = new StreamWriter(cs);
  
    // And write some data
    sw.WriteLine("'Twas brillig, and the slithy toves");
    sw.WriteLine("Did gyre and gimble in the wabe.");
    sw.Flush();
    sw.Close();
  
    // save the key and IV for future use
    FileStream fsKeyOut = File.Create(@"c:\\temp\encrypted.key");
  
    // use a BinaryWriter to write formatted data to the file
    BinaryWriter bw = new BinaryWriter(fsKeyOut);
  
    // write data to the file
    bw.Write( tdes.Key );
    bw.Write( tdes.IV );
  
    // flush and close
    bw.Flush();
    bw.Close();
  
  }
  
}

解密代碼如下

using System;
using System.IO;
using System.Security.Cryptography;
  
public class Example19_10
{
  public static void Main()
  {
  
    // Create a new crypto provider
    TripleDESCryptoServiceProvider tdes =
      new TripleDESCryptoServiceProvider();
  
    // open the file containing the key and IV
    FileStream fsKeyIn = File.OpenRead(@"c:\temp\encrypted.key");
  
    // use a BinaryReader to read formatted data from the file
    BinaryReader br = new BinaryReader(fsKeyIn);
  
    // read data from the file and close it
    tdes.Key = br.ReadBytes(24);
    tdes.IV = br.ReadBytes(8);
  
    // Open the encrypted file
    FileStream fsIn = File.OpenRead(@"c:\\temp\\encrypted.txt");
  
    // Create a cryptostream to decrypt from the filestream
    CryptoStream cs = new CryptoStream(fsIn, tdes.CreateDecryptor(),
      CryptoStreamMode.Read);
  
    // Create a StreamReader to format the input
    StreamReader sr = new StreamReader(cs);
  
    // And decrypt the data
    Console.WriteLine(sr.ReadToEnd());
    sr.Close();
  
  }
  
}

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

  • TortoiseSVN使用教程

    TortoiseSVN使用教程

    TortoiseSVN 是 Subversion 版本控制系統(tǒng)的一個免費(fèi)開源客戶端,可以超越時間的管理文件和目錄。本文給大家介紹TortoiseSVN使用教程,對tortoisesvn使用相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • C#實(shí)現(xiàn)簡單合并word文檔的方法

    C#實(shí)現(xiàn)簡單合并word文檔的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)簡單合并word文檔的方法,涉及C#針對word文檔的讀取、插入、保存等技巧,非常具有實(shí)用價值,需要的朋友可以參考下
    2015-09-09
  • WinForm使用正則表達(dá)式提取內(nèi)容的方法示例

    WinForm使用正則表達(dá)式提取內(nèi)容的方法示例

    這篇文章主要介紹了WinForm使用正則表達(dá)式提取內(nèi)容的方法,結(jié)合實(shí)例形式分析了WinForm基于正則匹配獲取指定內(nèi)容的相關(guān)操作技巧,需要的朋友可以參考下
    2017-05-05
  • C#8.0默認(rèn)接口實(shí)現(xiàn)的詳細(xì)實(shí)例

    C#8.0默認(rèn)接口實(shí)現(xiàn)的詳細(xì)實(shí)例

    Microsoft使用C#8.0發(fā)布了許多新功能,他們引入的主要功能之一是默認(rèn)接口方法。這篇文章主要給大家介紹了關(guān)于C#8.0默認(rèn)接口實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • C#連接數(shù)據(jù)庫的方法

    C#連接數(shù)據(jù)庫的方法

    ASP.NET連接數(shù)據(jù)庫的技術(shù)叫ADO.NET,它是用來向數(shù)據(jù)庫提交sql語句的一堆類。這里連接的是Sql Server 2008數(shù)據(jù)庫,其他數(shù)據(jù)庫用法差不多,就是調(diào)用的類名不一樣
    2015-11-11
  • UnityRTS實(shí)現(xiàn)相機(jī)移動縮放功能

    UnityRTS實(shí)現(xiàn)相機(jī)移動縮放功能

    這篇文章主要為大家詳細(xì)介紹了UnityRTS實(shí)現(xiàn)相機(jī)的移動縮放功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • C# 根據(jù)字符串生成二維碼的實(shí)例代碼

    C# 根據(jù)字符串生成二維碼的實(shí)例代碼

    這篇文章主要介紹了C# 根據(jù)字符串生成二維碼的實(shí)例,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#圖像亮度調(diào)整的方法

    C#圖像亮度調(diào)整的方法

    這篇文章主要介紹了C#圖像亮度調(diào)整的方法,涉及C#操作圖像亮度的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • 基于C#實(shí)現(xiàn)哈夫曼樹算法

    基于C#實(shí)現(xiàn)哈夫曼樹算法

    哈夫曼樹又稱最優(yōu)二叉樹,也就是帶權(quán)路徑最短的樹,對于哈夫曼樹,我想大家對它是非常的熟悉,使用下面我們就來學(xué)習(xí)一下如何通過C#實(shí)現(xiàn)哈夫曼樹算法吧
    2023-11-11
  • 詳解C# Lazy Loading(延遲加載)

    詳解C# Lazy Loading(延遲加載)

    這篇文章主要介紹了C# Lazy Loading(延遲加載)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04

最新評論

武川县| 安远县| 西吉县| 巨野县| 定陶县| 双峰县| 松潘县| 临邑县| 安远县| 六盘水市| 佛山市| 祁连县| 于田县| 白河县| 阿城市| 游戏| 安吉县| 曲阜市| 宜黄县| 甘洛县| 麻栗坡县| 台东市| 南和县| 顺义区| 永善县| 昭通市| 治县。| 济南市| 芦溪县| 泰顺县| 兴隆县| 绥江县| 商河县| 海淀区| 婺源县| 额济纳旗| 平泉县| 江口县| 磐石市| 甘德县| 长岭县|