C#实现对文件进行加密解密的方法

本文实例讲述了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();
 }
}

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • C# salt+hash 加密

    一.先明确几个基本概念 1.伪随机数:pseudo-random number generators ,简称为:PRNGs,是计算机利用一定的算法来产生的.伪随机数并不是假随机数,这里的"伪"是有规律的意思,就  是计算机产生的伪随机数既是随机的又是有规律的.怎样理解呢?产生的伪随机数有时遵守一定的规律,有时不遵守任何规律:伪随机数有一部分遵守一定的规律:另一部分不遵守任何规律.比如"世上没有两片形状完全相同的树叶",这正是点到了事物的特性,即随机性,但是每种树的叶

  • C#最简单的字符串加密解密方法

    public static string encode(string str) { string htext = ""; for (int i = 0; i < str.Length; i++) { htext = htext + (char)(str[i] + 10 - 1 * 2); } return htext; } public static string decode(string str) { string dtext = ""; for (int

  • C#编写DES加密、解密类

    这个C#类封装的DES加密解密,可以使用默认秘钥进行加密.解密,也可以自定义秘钥进行加密.解密,调用简单方便. 示例一: using System; using System.Security.Cryptography; using System.Text; namespace DotNet.Utilities { /// <summary> /// DES加密/解密类. /// </summary> public class DESEncrypt { public DESEncr

  • 浅谈C#中Md5和Sha1两种加密方式

    1.新建控制台应用程序 2.新建类 EncryptHelper.cs public static class EncryptHelper { /// <summary> /// 基于Md5的自定义加密字符串方法:输入一个字符串,返回一个由32个字符组成的十六进制的哈希散列(字符串). /// </summary> /// <param name="str">要加密的字符串</param> /// <returns>加密后的十六

  • 基于C#对用户密码使用MD5加密与解密

    C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1)16位的MD5加密 /// <summary> /// 16位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns&

  • C#使用 Salt + Hash 来为密码加密

    (一) 为什么要用哈希函数来加密密码 如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的人,都将可以直接看到这些密码. 解决的办法是将密码加密后再存储进数据库,比较常用的加密方法是使用哈希函数(Hash Function).哈希函数的具体定义,大家可以在网上或者相关书籍中查阅到,简单地说,它的特性如下: (1)原始密码经哈希函数计算后得到一个哈希值 (2)改变原始密码,哈希函数计算出的哈希值也会相应改变

  • PHP和C#可共用的可逆加密算法详解

    在一些项目中要求在php中生成加密,然后在asp.net中接受过来的密码再解密,下面和大家分享一个PHP与asp.net C#可共用的可逆加密算法,感兴趣的可以参考参考. php加密算法: <?php class DES { var $key; var $iv; //偏移量 function DES($key = '11001100', $iv=0 ) { //key长度8例如:1234abcd $this->key = $key; if( $iv == 0 ) { $this->iv

  • C#实现的AES加密解密完整实例

    本文实例讲述了C#实现的AES加密解密.分享给大家供大家参考,具体如下: /****************************************************************** * 创建人:HTL * 说明:C# AES加密解密 *******************************************************************/ using System; using System.Security.Cryptography;

  • C#加密app.config中连接字符串的方法

    本文实例讲述了C#加密app.config中连接字符串的方法.分享给大家供大家参考.具体如下: 连接字符串中包含数据库的访问信息,帐号和密码,因此一般不以明文显示,本代码用来加密连接字符串. public static class EncryptConnection { public static void EncryptConnectionString(bool encrypt) { Configuration configFile = null; try { // Open the conf

  • 详解C#实现MD5加密的示例代码

    C#实现MD5加密,具体如下: 方法一 首先,先简单介绍一下MD5 MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer science和rsa data security inc的ronald l. rivest开发出来, 经md2.md3和md4发展而来. MD5具有很好的安全性(因为它具有不可逆的特征,加过密的密文经过解密后和加密前的东东相同的可能性极小) 引用 using System.S

随机推荐