欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2015年07月08日 11:05:08   投稿:hebedich  
本文給大家分享的是使用C#對(duì)文件進(jìn)行加密解密的代碼,十分的簡(jiǎ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();
  
  }
  
}

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

相關(guān)文章

  • 判斷圖片-判斷位圖是否是黑白圖片的方法

    判斷圖片-判斷位圖是否是黑白圖片的方法

    近來(lái)工作需要判斷圖片否是是彩色的,由于是十萬(wàn)張以上的大批量理處,所以通過(guò)序程來(lái)動(dòng)自判斷。
    2013-05-05
  • C#實(shí)現(xiàn)將程序運(yùn)行信息寫(xiě)入日志的方法

    C#實(shí)現(xiàn)將程序運(yùn)行信息寫(xiě)入日志的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)將程序運(yùn)行信息寫(xiě)入日志的方法,可實(shí)現(xiàn)將程序運(yùn)行信息寫(xiě)入日志并存儲(chǔ)在Debug目錄下的"/Log/PRG"下的功能,涉及C#針對(duì)日志的相關(guān)寫(xiě)入技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • C#實(shí)現(xiàn)綁定Combobox的方法

    C#實(shí)現(xiàn)綁定Combobox的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)綁定Combobox的方法,涉及Combobox參數(shù)設(shè)置的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • WPF TextBox和PasswordBox添加水印

    WPF TextBox和PasswordBox添加水印

    這篇文章主要為大家詳細(xì)介紹了WPF TextBox和PasswordBox添加水印的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • c# List和Dictionary常用的操作

    c# List和Dictionary常用的操作

    這篇文章主要介紹了c# List和Dictionary常用的操作,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • Unity實(shí)現(xiàn)相機(jī)截圖功能

    Unity實(shí)現(xiàn)相機(jī)截圖功能

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)相機(jī)截圖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Unity Shader實(shí)現(xiàn)裁切效果

    Unity Shader實(shí)現(xiàn)裁切效果

    這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)裁切效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • DevExpress TreeList 常見(jiàn)問(wèn)題解決方法

    DevExpress TreeList 常見(jiàn)問(wèn)題解決方法

    這篇文章主要介紹了DevExpress TreeList 常見(jiàn)問(wèn)題解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • Unity實(shí)現(xiàn)模型點(diǎn)擊事件的方法

    Unity實(shí)現(xiàn)模型點(diǎn)擊事件的方法

    這篇文章主要介紹了Unity實(shí)現(xiàn)模型點(diǎn)擊事件的方法,本文通過(guò)多種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • C#反射調(diào)用dll文件中的方法操作泛型與屬性字段

    C#反射調(diào)用dll文件中的方法操作泛型與屬性字段

    這篇文章介紹了C#反射調(diào)用dll文件中的方法操作泛型與屬性字段,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論