文件的对称加密与解密
来源:互联网 发布:淘宝删除中差评步骤 编辑:程序博客网 时间:2024/05/18 00:34
<pre name="code" class="csharp"> //Encryption,MD5 Hash Algorithm class MD5Encryption { public string GetMd5Hash(string input) { using (MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider ()) { return BitConverter.ToString(md5.ComputeHash (UTF8Encoding.Default.GetBytes(input))).Replace("-",""); } } public string GetFileMd5Hash(string filePath) { using (MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider ()) using (FileStream fs=new FileStream(filePath,FileMode.Open,FileAccess.Read,FileShare.Read)) { return BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", ""); } } }
对称加密
//Symmetric Algorithm class SymmetricEncryption { static int bufferSize = 128 * 1024; //salt key static byte[] salt = { 134, 216, 7, 36, 88, 164, 91, 227, 174, 76, 191, 197, 192, 154, 200, 248 }; static byte[] iv = { 134, 216, 7, 36, 88, 164, 91, 227, 174, 76, 191, 197, 192, 154, 200, 248 }; private static SymmetricAlgorithm CreateRijndael(string password, byte[] salt) { PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA256", 1000); SymmetricAlgorithm sma = Rijndael.Create(); sma.KeySize = 256; sma.Key = pdb.GetBytes(32); sma.Padding = PaddingMode.PKCS7; return sma; } public static void EncryptFile(string inFile, string outFile, string password) { using (FileStream infs=File.OpenRead(inFile), outfs=File.Open(outFile,FileMode.OpenOrCreate)) using (SymmetricAlgorithm algorithm =CreateRijndael(password,salt)) { algorithm.IV = iv; using (CryptoStream crypoStream=new CryptoStream (outfs,algorithm.CreateEncryptor(),CryptoStreamMode.Write)) { byte[] bytes = new byte[bufferSize]; int readSize = -1; while ((readSize=infs.Read(bytes,0,bytes.Length))!=0) { crypoStream.Write(bytes, 0, readSize); } crypoStream.Flush(); } } } public static void DecryptFile(string inFile, string outFile, string password) { using (FileStream infs=File.OpenRead(inFile), outfs=File.OpenWrite(outFile)) using(SymmetricAlgorithm algorithm=CreateRijndael(password,salt)) { algorithm.IV = iv; using (CryptoStream crypoStream=new CryptoStream (infs,algorithm.CreateDecryptor(),CryptoStreamMode.Read)) { byte[] bytes = new byte[bufferSize]; int readSize = -1; int numReads = (int)(infs.Length / bufferSize); int slack = (int)(infs.Length % bufferSize); for (int i = 0; i < numReads; ++i) { readSize = crypoStream.Read(bytes, 0, bytes.Length); outfs.Write(bytes, 0, readSize); } if (slack>0) { readSize = crypoStream.Read(bytes, 0, (int)slack); outfs.Write(bytes, 0, readSize); } outfs.Flush(); } } } }
0 0
- 文件的对称加密与解密
- Java加密与解密的艺术-非对称加密
- 对称加密中DES的加密与解密算法
- 使用对称加密与解密算法对文件加密与解密
- 文件的加密与解密
- Java加密与解密的艺术-对称加密算法
- RSA 非对称加密与解密
- Java对称与非对称加密解密,AES与RSA
- Java对称与非对称加密解密,AES与RSA
- Java对称与非对称加密解密,AES与RSA
- java 对称加密——基于口令的加密与解密
- 对文件压缩加密,解密解压缩,对称加密,DES算法
- 使用对称加密aes对文件进行zip加密解密
- 模拟文件的加密与解密
- Vim的文件加密与解密
- c++文件的加密与解密
- Android文件的加密与解密
- 文件的简单加密与解密(不安全)
- 再谈SMB中继攻击
- Android 的 SurfaceView 双缓冲应用
- 驱动开发之用户模式和内核模式
- Android开发规范
- jquery $(document).ready() 与window.onload的区别以及DOMContentLoaded事件
- 文件的对称加密与解密
- 黑马程序员_IO流
- 用python 写一个nagios插件 监控http内容
- 在 Spring 3.1 中使用 @Cacheable 实现缓存
- struts1与struts2
- 特征提取-计算tf-idf
- Eclipse 快捷键
- 搭建MinGW+Msys+Git环境
- window 下,为Python 安装protobuf