<div >
<table>
<tr>
<td >文章编号</td>
<td >:</td>
<td >307010</td>
</tr>
<tr>
<td >最后修改</td>
<td >:</td>
<td >2007年1月23日</td>
</tr>
<tr>
<td >修订</td>
<td >:</td>
<td >7.3</td>
</tr>
<div >本文介绍如何使用 Microsoft .NET Framework 提供的加密类对文本文件进行加密以使其处于不可读状态,然后再对该信息进行解密,以恢复到原来的格式。
<p ><a href="http://support.microsoft.com/kb/307010/zh-cn#top">
<img alt="" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif">回到顶端
<h3 ID="tocheadRef">要求
下面列出了推荐使用的硬件、软件、网络架构以及所需的 Service Pack:
<table>
<tr>
<td >•</td>
<td >Microsoft windows 2000 Professional、windows 2000 Server、windows 2000 Advanced Server、windows NT 4.0 Server 或 Microsoft windows XP Professional</td>
</tr>
<tr>
<td >•</td>
<td >Microsoft Visual Studio 2005 或 Microsoft Visual Studio .NET</td>
</tr>
Microsoft .NET Framework 中的 System.Security.Cryptographic 命名空间提供了多种帮助您加密和解密的工具。CryptoStream 类就是所提供的诸多类中的一个。CryptoStream 类设计用于在内容以流的形式输出到文件时加密和解密内容。
<p ><a href="http://support.microsoft.com/kb/307010/zh-cn#top">
<img alt="" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif">回到顶端
<h3 ID="tocheadRef">加密文件
要加密文件,请按照下列步骤 *** 作:
<table>
<tr>
<td >1.</td>
<td >启动 Visual Studio 2005 或 Visual Studio .NET。</td>
</tr>
<tr>
<td >2.</td>
<td >单击“项目”下的“Visual C#”,然后单击“模板”下的“控制台应用程序”。Visual C# .NET 为您创建一个静态类,以及一个空的 Main() 过程。</td>
</tr>
<tr>
<td >3.</td>
<td >对以下命名空间使用 using 语句(如以下示例代码中所示):
<table>
<tr>
<td >•</td>
<td >System</td>
</tr>
<tr>
<td >•</td>
<td >System.Security</td>
</tr>
<tr>
<td >•</td>
<td >System.Security.Cryptography</td>
</tr>
<tr>
<td >•</td>
<td >System.Text</td>
</tr>
<tr>
<td >•</td>
<td >System.IO</td>
</tr>using System; using System.IO; using System.Security; using System.Security.Cryptography; using System.Runtime.InteropServices; using System.Text;
要解密文件,请按照下列步骤 *** 作:
<table>
<tr>
<td >1.</td>
<td >创建一个方法,然后将它命名为 Decryptfile。解密过程与加密过程相似,但 Decryptfile 过程与 Encryptfile 过程有两个关键区别。
<table>
<tr>
<td >•</td>
<td >CryptoStream 对象是使用 CreateDecryptor 而非 CreateEncryptor 创建的,这将指定对象的使用方式。</td>
</tr>
<tr>
<td >•</td>
<td >在将解密的文本写入到目标文件时,CryptoStream 对象现在是源,而不是目标流。</td>
</tr>static voID Decryptfile(string sinputfilename,string sKey) { DESCryptoServiceProvIDer DES = new DESCryptoServiceProvIDer(); //A 64 bit key and IV is required for this provIDer. //Set secret key For DES algorithm. DES.Key = ASCIIEnCoding.ASCII.GetBytes(sKey); //Set initialization vector. DES.IV = ASCIIEnCoding.ASCII.GetBytes(sKey); //Create a file stream to read the encrypted file back. fileStream fsread = new fileStream(sinputfilename,fileAccess.Read); //Create a DES decryptor from the DES instance. ICryptotransform desdecrypt = DES.CreateDecryptor(); //Create crypto stream set to read and do a //DES decryption transform on incoming bytes. CryptoStream cryptostreamDecr = new CryptoStream(fsread,desdecrypt,CryptoStreamMode.Read); //Print the contents of the decrypted file. StreamWriter fsDecrypted = new StreamWriter(sOutputfilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } static voID Main() { // Must be 64 bits,8 bytes. // distribute this key to the user who will decrypt this file. string sSecretKey; // Get the key for the file to encrypt. sSecretKey = GenerateKey(); // For additional security pin the key. GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned ); // Encrypt the file. Encryptfile(@"C:\MyData.txt",@"C:\Encrypted.txt",sSecretKey); // Decrypt the file. Decryptfile(@"C:\Encrypted.txt",@"C:\Decrypted.txt",sSecretKey); // Remove the key from memory. ZeroMemory(gch.AddrOfPinnedobject(),sSecretKey.Length * 2); gch.Free(); } 用一个文本文件 (.txt) 测试此代码,确认它可对此文件进行正确的加密和解密。确保将文件解密到一个新文件(如本文中的 Main() 过程中所示),而不是解密到原来的文件中。检查解密后的文件,然后与原文件进行比较。
<p ><a href="http://support.microsoft.com/kb/307010/zh-cn#top">
<img alt="" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif">回到顶端
<h3 ID="tocheadRef">完整代码列表
using System;using System.IO;using System.Security;using System.Security.Cryptography;using System.Runtime.InteropServices;using System.Text;namespace CSEncryptDecrypt{class Class1{// Call this function to remove the key from memory after use for security[System.Runtime.InteropServices.Dllimport("KERNEL32.DLL",EntryPoint="RtlZeroMemory")]public static extern bool ZeroMemory(IntPtr Destination,int Length);// Function to Generate a 64 bits Key.static string GenerateKey(){// Create an instance of Symetric Algorithm. Key and IV is generated automatically.DESCryptoServiceProvIDer desCrypto =(DESCryptoServiceProvIDer)DESCryptoServiceProvIDer.Create();// Use the automatically generated key for Encryption.return ASCIIEnCoding.ASCII.GetString(desCrypto.Key);}static voID Encryptfile(string sinputfilename,string sKey){fileStream fsinput = new fileStream(sinputfilename,fileAccess.Read);fileStream fsEncrypted = new fileStream(sOutputfilename,fileAccess.Write);DESCryptoServiceProvIDer DES = new DESCryptoServiceProvIDer();DES.Key = ASCIIEnCoding.ASCII.GetBytes(sKey);DES.IV = ASCIIEnCoding.ASCII.GetBytes(sKey);ICryptotransform desencrypt = DES.CreateEncryptor();CryptoStream cryptostream = new CryptoStream(fsEncrypted,CryptoStreamMode.Write);byte[] bytearrayinput = new byte[fsinput.Length];fsinput.Read(bytearrayinput,bytearrayinput.Length);cryptostream.Write(bytearrayinput,bytearrayinput.Length);cryptostream.Close();fsinput.Close();fsEncrypted.Close();}static voID Decryptfile(string sinputfilename,string sKey){DESCryptoServiceProvIDer DES = new DESCryptoServiceProvIDer();//A 64 bit key and IV is required for this provIDer.//Set secret key For DES algorithm.DES.Key = ASCIIEnCoding.ASCII.GetBytes(sKey);//Set initialization vector.DES.IV = ASCIIEnCoding.ASCII.GetBytes(sKey);//Create a file stream to read the encrypted file back.fileStream fsread = new fileStream(sinputfilename,fileAccess.Read);//Create a DES decryptor from the DES instance.ICryptotransform desdecrypt = DES.CreateDecryptor();//Create crypto stream set to read and do a//DES decryption transform on incoming bytes.CryptoStream cryptostreamDecr = new CryptoStream(fsread,CryptoStreamMode.Read);//Print the contents of the decrypted file.StreamWriter fsDecrypted = new StreamWriter(sOutputfilename);fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());fsDecrypted.Flush();fsDecrypted.Close();}static voID Main(){// Must be 64 bits,8 bytes.// distribute this key to the user who will decrypt this file.string sSecretKey;// Get the Key for the file to Encrypt.sSecretKey = GenerateKey();// For additional security Pin the key.GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );// Encrypt the file.Encryptfile(@"C:\MyData.txt",sSecretKey);// Decrypt the file.Decryptfile(@"C:\Encrypted.txt",sSecretKey);// Remove the Key from memory.ZeroMemory(gch.AddrOfPinnedobject(),sSecretKey.Length * 2);gch.Free();}}}
<div >有关加密以及使用 .NET 的加密功能的更多信息,请访问下面的 MSDN 网站:
<div >System.Security.Cryptography 命名空间<a href="http://msdn2.microsoft.com/zh-cn/library/system.security.cryptography(VS.80).aspx">http://msdn2.microsoft.com/zh-cn/library/system.security.cryptography(VS.80).aspx (http://msdn2.microsoft.com/zh-cn/library/system.security.cryptography(VS.80).aspx)
<div >Microsoft .NET Framework 开发中心<a href="http://msdn2.microsoft.com/zh-cn/netframework/default.aspx">http://msdn2.microsoft.com/zh-cn/netframework/default.aspx (http://msdn2.microsoft.com/zh-cn/netframework/default.aspx)
有关 Visual C# .NET 的更多一般信息,请访问以下 Usenet 新闻组:
<div >
<a href="http://go.microsoft.com/fwlink/?linkID=5217">microsoft.public.dotnet.languages.csharp@H_502_54@ (http://go.microsoft.com/fwlink/?linkID=5217)
<p ><a href="http://support.microsoft.com/kb/307010/zh-cn#top">
<img alt="" src="http://support.microsoft.com/library/images/support/en-us/uparrow.gif">回到顶端
<a ID="applIEsto">@H_502_54@这篇文章中的信息适用于:以上是内存溢出为你收集整理的如何使用 Visual C# 加密和解密文件 转载全部内容,希望文章能够帮你解决如何使用 Visual C# 加密和解密文件 转载所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)