c# 图片加密解密的实例代码。

代码:

using System;
using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using System.IO; namespace Net.Template.Common { /// <summary> /// 对图片的加密和解密 /// </summary> public class DEncrypt4ImageHelper { public DEncrypt4ImageHelper() { } #region 加密方法 图片加密 /// <summary> /// 图片加密 /// </summary> /// <param name="filePath">源文件</param> /// <param name="savePath">保存为文件名称</param> /// <param name="keyStr">密钥</param> public static void EncryptFile(string filePath, string savePath, string keyStr) { //通过des加密 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //通过流打开文件 FileStream fs = File.OpenRead(filePath); //获取文件二进制字符 byte[] inputByteArray = new byte[fs.Length]; //读流文件 fs.Read(inputByteArray, , (int)fs.Length); //关闭流 fs.Close(); //获得加密字符串二进制字符 byte[] keyByteArray = Encoding.Default.GetBytes(keyStr); //计算指定字节组指定区域哈希值 SHA1 ha = new SHA1Managed(); byte[] hb = ha.ComputeHash(keyByteArray); //加密密钥数组 byte[] sKey = new byte[]; //加密变量 byte[] sIV = new byte[]; for (int i = ; i < ; i++) sKey[i] = hb[i]; for (int i = ; i < ; i++) sIV[i - ] = hb[i]; //获取加密密钥 des.Key = sKey; //设置加密初始化向量 des.IV = sIV; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, , inputByteArray.Length); cs.FlushFinalBlock(); fs = File.OpenWrite(savePath); foreach (byte b in ms.ToArray()) { www.jbxue.com fs.WriteByte(b); } fs.Close(); cs.Close(); ms.Close(); } #endregion #region 解密方法 图片解密 /// <summary> /// 图片解密 /// </summary> /// <param name="filePath">源文件</param> /// <param name="savePath">保存文件</param> /// <param name="keyStr">密钥</param> public static void DecryptFile(string filePath, string savePath, string keyStr) { //通过des解密 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //通过流读取文件 FileStream fs = File.OpenRead(filePath); //获取文件二进制字符 byte[] inputByteArray = new byte[fs.Length]; //读取流文件 fs.Read(inputByteArray, , (int)fs.Length); //关闭流 fs.Close(); //密钥数组 byte[] keyByteArray = Encoding.Default.GetBytes(keyStr); //定义哈希变量 SHA1 ha = new SHA1Managed(); //计算指定字节组指定区域哈希值 byte[] hb = ha.ComputeHash(keyByteArray); //加密密钥数组 byte[] sKey = new byte[]; //加密变量 byte[] sIV = new byte[]; for (int i = ; i < ; i++) sKey[i] = hb[i]; for (int i = ; i < ; i++) sIV[i - ] = hb[i]; //获取加密密钥 des.Key = sKey; //加密变量 des.IV = sIV; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, , inputByteArray.Length); cs.FlushFinalBlock(); fs = File.OpenWrite(savePath); foreach (byte b in ms.ToArray()) { fs.WriteByte(b); } fs.Close(); cs.Close(); ms.Close(); } #endregion } }

最新文章

  1. 使用Filter跟踪Asp.net MVC页面加载时间
  2. 源码阅读 etherum-transactions.py
  3. sql 查询执行的详细时间profile
  4. svn diff excel
  5. SQL 行转列
  6. UI基础视图----UIImageView总结
  7. ajax存在跨域问题,为什么浏览器不允许js跨域请求?
  8. kubernetes之管理容器的计算资源
  9. vue 2.0 + ElementUI构建树形表格
  10. 大牛的IT经验,方法【跟,帮】
  11. 【转】使用ffmpeg转码的MP4文件需要加载完了才能播放的解决办法
  12. Linux md5sum 的用法
  13. sass的基本使用
  14. 将关系型数据库抽取成redis的思路
  15. AGC001 E - BBQ Hard 组合数学
  16. VC++ 获取exe或者dll版本信息
  17. using关键字在C#中的3种用法
  18. MacBook PRO蓝牙无法搜索设备
  19. H5本地存储一
  20. VB6 让程序结束后带有返回值

热门文章

  1. [Python]计算闰年时候出现的and和or优先级的问题以及短路逻辑
  2. jmeter插件JMeterPlugins-Standard 压力测试
  3. tomcat 内存溢出原因分析及解决
  4. React从入门到精通系列之(14)refs和DOM元素
  5. C++ STL标准模板库(queue)
  6. 第一百三十九节,JavaScript,封装库--CSS选择器
  7. org.springframework.beans.factory.parsing.BeanDefinitionParsingException
  8. smartJS 0.1 API 讲解 - Trigger
  9. django数据库设计
  10. Android无线测试之—UiAutomator UiObject API介绍七