相关示例代码如下:

  1. using System;
  2. using System.IO;
  3. using System.Security;
  4. using System.Security.Cryptography;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7.  
  8. namespace CSEncryptDecrypt
  9. {
  10. class Class1
  11. {
  12. // Call this function to remove the key from memory after use for security
  13. [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
  14. public static extern bool ZeroMemory(IntPtr Destination, int Length);
  15.  
  16. // Function to Generate a 64 bits Key.
  17. static string GenerateKey()
  18. {
  19. // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
  20. DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  21.  
  22. // Use the Automatically generated key for Encryption.
  23. return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
  24. }
  25.  
  26. static void EncryptFile(string sInputFilename,
  27. string sOutputFilename,
  28. string sKey)
  29. {
  30. FileStream fsInput = new FileStream(sInputFilename,
  31. FileMode.Open,
  32. FileAccess.Read);
  33.  
  34. FileStream fsEncrypted = new FileStream(sOutputFilename,
  35. FileMode.Create,
  36. FileAccess.Write);
  37. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  38. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  39. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  40. ICryptoTransform desencrypt = DES.CreateEncryptor();
  41. CryptoStream cryptostream = new CryptoStream(fsEncrypted,
  42. desencrypt,
  43. CryptoStreamMode.Write);
  44.  
  45. byte[] bytearrayinput = new byte[fsInput.Length];
  46. fsInput.Read(bytearrayinput, , bytearrayinput.Length);
  47. cryptostream.Write(bytearrayinput, , bytearrayinput.Length);
  48. cryptostream.Close();
  49. fsInput.Close();
  50. fsEncrypted.Close();
  51. }
  52.  
  53. static void DecryptFile(string sInputFilename,
  54. string sOutputFilename,
  55. string sKey)
  56. {
  57. DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  58. //A 64 bit key and IV is required for this provider.
  59. //Set secret key For DES algorithm.
  60. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  61. //Set initialization vector.
  62. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  63.  
  64. //Create a file stream to read the encrypted file back.
  65. FileStream fsread = new FileStream(sInputFilename,
  66. FileMode.Open,
  67. FileAccess.Read);
  68. //Create a DES decryptor from the DES instance.
  69. ICryptoTransform desdecrypt = DES.CreateDecryptor();
  70. //Create crypto stream set to read and do a
  71. //DES decryption transform on incoming bytes.
  72. CryptoStream cryptostreamDecr = new CryptoStream(fsread,
  73. desdecrypt,
  74. CryptoStreamMode.Read);
  75. //Print the contents of the decrypted file.
  76. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
  77. fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
  78. fsDecrypted.Flush();
  79. fsDecrypted.Close();
  80. }
  81.  
  82. static void Main()
  83. {
  84. // Must be 64 bits, 8 bytes.
  85. // Distribute this key to the user who will decrypt this file.
  86. string sSecretKey;
  87.  
  88. // Get the Key for the file to Encrypt.
  89. sSecretKey = GenerateKey();
  90.  
  91. // For additional security Pin the key.
  92. GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );
  93.  
  94. // Encrypt the file.
  95. EncryptFile(@"C:\MyData.txt",
  96. @"C:\Encrypted.txt",
  97. sSecretKey);
  98.  
  99. // Decrypt the file.
  100. DecryptFile(@"C:\Encrypted.txt",
  101. @"C:\Decrypted.txt",
  102. sSecretKey);
  103.  
  104. // Remove the Key from memory.
  105. ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * );
  106. gch.Free();
  107. }
  108. }
  109. }

C# 加密和解密文件的更多相关文章

  1. 用mp3stego来加密与解密文件的几次尝试

    用法来自实验吧的"Canon"隐写题目的灵感. 先来简单的聊一下这道题目,打开题目后发现了一个mp3文件,除此之外还有一枚压缩包.然而压缩包是加密的,看来我们需要通过解出来mp3里 ...

  2. (8) openssl rsautl(签名/验证签名/加解密文件)和openssl pkeyutl(文件的非对称加密)

    rsautl是rsa的工具,相当于rsa.dgst的部分功能集合,可用于生成数字签名.验证数字签名.加密和解密文件. pkeyutl是非对称加密的通用工具,大体上和rsautl的用法差不多,所以此处只 ...

  3. C# 加密解密文件

    using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptograph ...

  4. Java实现文件的加密与解密

    最近在做一个项目,需要将资源文件(包括图片.动画等类型)进行简单的加密后再上传至云上的服务器,而在应用程序中对该资源使用前先将读取到的文件数据进行解密以得到真正的文件信息.此策略的原因与好处是将准备好 ...

  5. C#使用RSA证书文件加密和解密示例

    修改MSDN上的示例,使之可以通过RSA证书文件加密和解密,中间遇到一个小问题. Q:执行ExportParameters()方法时,回报CryptographicException:该项不适于在指定 ...

  6. base64加密解密文件

    1 //字符串加密 -(void)demo1 { //普通的 8 bit二进制数据 NSString *str = @"hello world!"; //将字符串转换成二进制数据 ...

  7. TEA加密算法的文件加密和解密的实现

    一.TEA加密算法简介 TEA加密算法是由英国剑桥大学计算机实验室提出的一种对称分组加密算法.它采用扩散和混乱方法,对64位的明文数据块,用128位密钥分组进行加密,产生64位的密文数据块,其循环轮数 ...

  8. 使用 gzexe 快速加密解密文件内容

    使用 gzexe 快速加密解密文件内容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用sshpass工具编写远程管理脚本 1>.安装依赖包 [root@node101 ...

  9. Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作

    # -*- coding: gb2312 -*- #用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作 #作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.co ...

随机推荐

  1. Linux启动ssh服务

    Linux启动ssh服务 在Linux下启动ssh服务使用如下命令其一即可: # service sshd start # /etc/init.d/sshd start 开机启动 使用如下方法其就可以 ...

  2. 何为RunLoop?RunLoop有哪些应用场景?

    一.RunLoop的作用 一个应用开始运行以后放在那里,如果不对它进行任何操作,这个应用就像静止了一样,不会自发的有任何动作发生,但是如果我们点击界面上的一个按钮,这个时候就会有对应的按钮响应事件发生 ...

  3. Tomcat8内置jdk8运行环境发布web项目

    简单说明:之前部署项目都是没有改变之前的环境变量,最近由于公司的数据源换了,jdk由1.7改成了1.8,tomcat7也改为了1.8,现在需要部署采用新数据源的这个项目, 为了不改变之前的环境变量,使 ...

  4. 高通Android display分析【转】

    本文转载自:http://blog.csdn.net/zhangchiytu/article/details/6777039 高通7系列硬件架构分析 如上图,高通7系列 Display的硬件部分主要由 ...

  5. Centos7 关闭Ipv6

  6. linux运维面试题1

    一.填空题 1. 在Linux 系统 中,以文件方式访问设备 . 2. Linux 内核引导时,从文件/etc/fstab中读取要加载的文件系统 . 3. Linux 文件系统中每个文件用indoe节 ...

  7. Spring的AOP面向切面编程

    什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...

  8. ios 发布相关材料记录

    1 app icon Spotlight iOS 5,6 base: 29pt, 需要 @1x, @2x, @3x,得出:29 x 29, 58 x 58, 87 x 87 iOS 7,8 base: ...

  9. Spark-运行时架构

    Spark运行时架构 在分布式环境下,Spark集群采用的时主/从结构.在一个Spark集群中,有一个节点负责中央协调,调度各个分布式工作节点.这个中央协调节点被称为驱动器(Driver),与之对应的 ...

  10. 利用Phoenix为HBase创建二级索引

    为什么需要Secondary Index 对于Hbase而言,如果想精确地定位到某行记录,唯一的办法是通过rowkey来查询.如果不通过rowkey来查找数据,就必须逐行地比较每一列的值,即全表扫瞄. ...