C# 加密和解密文件
相关示例代码如下:
- 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 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();
- DES.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 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 sOutputFilename,
- 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,
- 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,
- 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 * );
- gch.Free();
- }
- }
- }
C# 加密和解密文件的更多相关文章
- 用mp3stego来加密与解密文件的几次尝试
用法来自实验吧的"Canon"隐写题目的灵感. 先来简单的聊一下这道题目,打开题目后发现了一个mp3文件,除此之外还有一枚压缩包.然而压缩包是加密的,看来我们需要通过解出来mp3里 ...
- (8) openssl rsautl(签名/验证签名/加解密文件)和openssl pkeyutl(文件的非对称加密)
rsautl是rsa的工具,相当于rsa.dgst的部分功能集合,可用于生成数字签名.验证数字签名.加密和解密文件. pkeyutl是非对称加密的通用工具,大体上和rsautl的用法差不多,所以此处只 ...
- C# 加密解密文件
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptograph ...
- Java实现文件的加密与解密
最近在做一个项目,需要将资源文件(包括图片.动画等类型)进行简单的加密后再上传至云上的服务器,而在应用程序中对该资源使用前先将读取到的文件数据进行解密以得到真正的文件信息.此策略的原因与好处是将准备好 ...
- C#使用RSA证书文件加密和解密示例
修改MSDN上的示例,使之可以通过RSA证书文件加密和解密,中间遇到一个小问题. Q:执行ExportParameters()方法时,回报CryptographicException:该项不适于在指定 ...
- base64加密解密文件
1 //字符串加密 -(void)demo1 { //普通的 8 bit二进制数据 NSString *str = @"hello world!"; //将字符串转换成二进制数据 ...
- TEA加密算法的文件加密和解密的实现
一.TEA加密算法简介 TEA加密算法是由英国剑桥大学计算机实验室提出的一种对称分组加密算法.它采用扩散和混乱方法,对64位的明文数据块,用128位密钥分组进行加密,产生64位的密文数据块,其循环轮数 ...
- 使用 gzexe 快速加密解密文件内容
使用 gzexe 快速加密解密文件内容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用sshpass工具编写远程管理脚本 1>.安装依赖包 [root@node101 ...
- Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作
# -*- coding: gb2312 -*- #用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作 #作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.co ...
随机推荐
- Linux启动ssh服务
Linux启动ssh服务 在Linux下启动ssh服务使用如下命令其一即可: # service sshd start # /etc/init.d/sshd start 开机启动 使用如下方法其就可以 ...
- 何为RunLoop?RunLoop有哪些应用场景?
一.RunLoop的作用 一个应用开始运行以后放在那里,如果不对它进行任何操作,这个应用就像静止了一样,不会自发的有任何动作发生,但是如果我们点击界面上的一个按钮,这个时候就会有对应的按钮响应事件发生 ...
- Tomcat8内置jdk8运行环境发布web项目
简单说明:之前部署项目都是没有改变之前的环境变量,最近由于公司的数据源换了,jdk由1.7改成了1.8,tomcat7也改为了1.8,现在需要部署采用新数据源的这个项目, 为了不改变之前的环境变量,使 ...
- 高通Android display分析【转】
本文转载自:http://blog.csdn.net/zhangchiytu/article/details/6777039 高通7系列硬件架构分析 如上图,高通7系列 Display的硬件部分主要由 ...
- Centos7 关闭Ipv6
- linux运维面试题1
一.填空题 1. 在Linux 系统 中,以文件方式访问设备 . 2. Linux 内核引导时,从文件/etc/fstab中读取要加载的文件系统 . 3. Linux 文件系统中每个文件用indoe节 ...
- Spring的AOP面向切面编程
什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...
- 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: ...
- Spark-运行时架构
Spark运行时架构 在分布式环境下,Spark集群采用的时主/从结构.在一个Spark集群中,有一个节点负责中央协调,调度各个分布式工作节点.这个中央协调节点被称为驱动器(Driver),与之对应的 ...
- 利用Phoenix为HBase创建二级索引
为什么需要Secondary Index 对于Hbase而言,如果想精确地定位到某行记录,唯一的办法是通过rowkey来查询.如果不通过rowkey来查找数据,就必须逐行地比较每一列的值,即全表扫瞄. ...