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

C#對文件進行加密解密代碼

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

加密代碼

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)容了,希望大家能夠喜歡。

相關文章

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

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

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

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

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

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

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

    WPF TextBox和PasswordBox添加水印

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

    c# List和Dictionary常用的操作

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

    Unity實現(xiàn)相機截圖功能

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

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

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

    DevExpress TreeList 常見問題解決方法

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

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

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

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

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

最新評論