ImageVerifierCode 换一换
格式:DOCX , 页数:11 ,大小:17.28KB ,
资源ID:9901347      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-9901347.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#加密解密文件.docx)为本站会员(b****8)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

C#加密解密文件.docx

1、C#加密解密文件C#加密解密文件 C#加密解密文件转 2008年06月26日 星期四 15:57加密文件要加密文件,请按照下列步骤操作:1. 启动 Visual Studio 2005 或 Visual Studio .NET。 2. 单击“项目”下的“Visual C#”,然后单击“模板”下的“控制台应用程序”。Visual C# .NET 为您创建一个静态类,以及一个空的 Main() 过程。 3. 对以下命名空间使用 using 语句(如以下示例代码中所示): • System &

2、#8226; System.Security • System.Security.Cryptography • System.Text • System.IO 这样,在后面的代码中就不必从这些命名空间中限定声明了。这些语句必须位于任何其他声明之前。using System;using System.IO;using System.Security;using System.Security.Cryptography;using System.Runtime.InteropServices;using System.Text; 4. 生成密钥以加密和解密数据。

3、DESCryptoServiceProvider 基于一种对称加密算法。对称加密需要密钥和初始化矢量 (IV) 来加密数据。要解密该数据,您必须拥有此同一密钥和 IV。您还必须使用相同的加密算法。您可以使用下列方法之一生成密钥: • 方法 1 您可以提示用户输入密码。然后,将此密码用作密钥和 IV。 • 方法 2 当您创建对称加密类的新实例时,将为会话自动创建一个新的密钥和 IV。使用由受管理的对称加密类生成的密钥和 IV 来加密和解密文件。5. 添加以下函数为会话生成一个新的密钥(按照步骤 4 的方法 2 中的说明):/ Call this function to r

4、emove the key from memory after usefor security.System.Runtime.InteropServices.DllImport(KERNEL32.DLL, EntryPoint=RtlZeroMemory)public static extern bool ZeroMemory(ref string Destination, int Length);/ Function to Generate a 64 bits Key.static string GenerateKey() / Create an instance of Symetric A

5、lgorithm. Key and IV is generated automatically.DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();/ Use the Automatically generated key for Encryption. return ASCIIEncoding.ASCII.GetString(desCrypto.Key);6. 在您的类中创建一个命名为 EncryptFile 的方法。EncryptFile 类必须具有以

6、下 3 个参数: • sInputFilename • sOutputFilename • sKey(用于加密和解密文件的密钥。) static void EncryptFile(string sInputFilename,string sOutputFilename,string sKey) 7. 在 EncryptFile 过程中,创建一个输入 FileStream 对象和一个输出 FileStream 对象。这些对象可以从目标文件中读取和向其中写入。FileStream fsInput = new FileStream(sInputFilename,

7、FileMode.Open, FileAccess.Read);FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write); 8. 声明一个 DESCryptoServiceProvider 类的实例。这表示对文件使用的实际加密和解密技术。此时,如果您更喜欢使用 RSAsecutiry 或另一种加密技术,则可以创建一个不同的提供程序。DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 9. 必须以字

8、节数组的形式给加密提供程序提供密钥。System.Text 命名空间提供了一个名为 GetBytes() 的函数。GetBytes() 函数的编码特征之一是,它取一个字符串,然后返回一个字节数组。各种加密技术采用的密钥长度是不相同的。例如,数据加密标准 (DES) 使用等于 8 个字节或 8 个字符的 64 位密钥。如果您不提供密钥,提供程序就会随机生成一个密钥。这将成功地加密文件,但是无法解密文件。请注意,您还必须提供初始化矢量 (IV)。该值用作加密的一部分。与密钥相似,如果您未提供 IV,提供程序就会随机生成一个。由于该值对于加密和解密必须相同,所以不能让提供程序随机生成这些值。DES.

9、Key = ASCIIEncoding.ASCII.GetBytes(sKey);DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 10. 创建一个 CryptoStream 类的实例,方法是:使用加密提供程序获得一个加密对象(CreateEncryptor),并将现有的输出 FileStream 对象作为构造函数的一部分。ICryptoTransform desencrypt = DES.CreateEncryptor();CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencr

10、ypt, CryptoStreamMode.Write); 11. 读入输入文件,然后写出到输出文件。传递 CryptoStream 对象,文件将使用您提供的密钥加密。byte bytearrayinput = new bytefsInput.Length - 1;fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);解密文件要解密文件,请按照下列步骤操作:1. 创建一个方法,然后将它命名为 DecryptFile。解

11、密过程与加密过程相似,但 DecryptFile 过程与 EncryptFile 过程有两个关键区别。 •CryptoStream 对象是使用 CreateDecryptor 而非 CreateEncryptor 创建的,这将指定对象的使用方式。 • 在将解密的文本写入到目标文件时,CryptoStream 对象现在是源,而不是目标流。 static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)DESCryptoServiceProvider DES = new

12、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

13、= new FileStream(sInputFilename, FileMode.Open, 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(f

14、sread, 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(); 2. 将以下几行添加到 Main() 过程,以调用 EncryptFile 和 DecryptFi

15、le: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 )

16、; / 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();3. 保存文件。运行您的应用程序。确保输入文件名使用的路径指向一个现有文件。测试过

17、程用一个文本文件 (.txt) 测试此代码,确认它可对此文件进行正确的加密和解密。确保将文件解密到一个新文件(如本文中的 Main() 过程中所示),而不是解密到原来的文件中。检查解密后的文件,然后与原文件进行比较。完整代码列表using System;using System.IO;using System.Security;using System.Security.Cryptography;using System.Runtime.InteropServices;using System.Text;namespace CSEncryptDecrypt class Class1 / Cal

18、l 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 in

19、stance 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(str

20、ing sInputFilename, string sOutputFilename, string sKey) FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DE

21、S.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); byte bytearrayinput = new bytefsInput.Length; fsInput.Read(byte

22、arrayinput, 0, bytearrayinput.Length); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Close(); fsInput.Close(); fsEncrypted.Close(); static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey) DESCryptoServiceProvider DES = new DESCryptoServiceProv

23、ider(); /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 FileStr

24、eam(sInputFilename, FileMode.Open, 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, de

25、sdecrypt, 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. / Dist

26、ribute 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();

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2