c# 圖片加密解密的實(shí)例代碼
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Net.Template.Common
{
/// <summary>
/// 對(duì)圖片的加密和解密
/// </summary>
public class DEncrypt4ImageHelper
{
public DEncrypt4ImageHelper() { }
#region 加密方法 圖片加密
/// <summary>
/// 圖片加密
/// </summary>
/// <param name="filePath">源文件</param>
/// <param name="savePath">保存為文件名稱(chēng)</param>
/// <param name="keyStr">密鑰</param>
public static void EncryptFile(string filePath, string savePath, string keyStr)
{
//通過(guò)des加密
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//通過(guò)流打開(kāi)文件
FileStream fs = File.OpenRead(filePath);
//獲取文件二進(jìn)制字符
byte[] inputByteArray = new byte[fs.Length];
//讀流文件
fs.Read(inputByteArray, 0, (int)fs.Length);
//關(guān)閉流
fs.Close();
//獲得加密字符串二進(jìn)制字符
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
//計(jì)算指定字節(jié)組指定區(qū)域哈希值
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
//加密密鑰數(shù)組
byte[] sKey = new byte[8];
//加密變量
byte[] sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
//獲取加密密鑰
des.Key = sKey;
//設(shè)置加密初始化向量
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
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)
{
//通過(guò)des解密
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//通過(guò)流讀取文件
FileStream fs = File.OpenRead(filePath);
//獲取文件二進(jìn)制字符
byte[] inputByteArray = new byte[fs.Length];
//讀取流文件
fs.Read(inputByteArray, 0, (int)fs.Length);
//關(guān)閉流
fs.Close();
//密鑰數(shù)組
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
//定義哈希變量
SHA1 ha = new SHA1Managed();
//計(jì)算指定字節(jié)組指定區(qū)域哈希值
byte[] hb = ha.ComputeHash(keyByteArray);
//加密密鑰數(shù)組
byte[] sKey = new byte[8];
//加密變量
byte[] sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
//獲取加密密鑰
des.Key = sKey;
//加密變量
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
}
#endregion
}
}
相關(guān)文章
unity實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03淺析WPF中Binding的數(shù)據(jù)校驗(yàn)和類(lèi)型轉(zhuǎn)換
在WPF開(kāi)發(fā)中,Binding實(shí)現(xiàn)了數(shù)據(jù)在Source和Target之間的傳遞和流通,那在WPF開(kāi)發(fā)中,如何實(shí)現(xiàn)數(shù)據(jù)的校驗(yàn)和類(lèi)型轉(zhuǎn)換呢,下面就跟隨小編一起學(xué)習(xí)一下吧2024-03-03unity中實(shí)現(xiàn)Edge瀏覽器鼠標(biāo)手勢(shì)的功能思路詳解
這篇文章主要介紹了unity中實(shí)現(xiàn)Edge瀏覽器鼠標(biāo)手勢(shì)的功能思路詳解,實(shí)現(xiàn)起來(lái)其實(shí)并不復(fù)雜,涉及的技術(shù)點(diǎn)有pc端和移動(dòng)端屏幕拖動(dòng)事件,二維向量的相關(guān)運(yùn)算,手勢(shì)匹配算法,事件系統(tǒng)設(shè)計(jì)模式,需要的朋友可以參考下2023-12-12優(yōu)雅又實(shí)用的C#代碼優(yōu)化技巧分享
這篇文章主要為大家整理了一些優(yōu)雅又實(shí)用的C#代碼優(yōu)化技巧,文中的示例代碼講解詳細(xì),對(duì)我們深入了解C#有一定的幫助,需要的可以參考一下2023-06-06C#判斷一個(gè)類(lèi)是否實(shí)現(xiàn)了某個(gè)接口3種實(shí)現(xiàn)方法
這篇文章主要介紹了C#判斷一個(gè)類(lèi)是否實(shí)現(xiàn)了某個(gè)接口3種實(shí)現(xiàn)方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06C#實(shí)現(xiàn)讓窗體獲得焦點(diǎn)的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)讓窗體獲得焦點(diǎn)的方法,涉及C#窗體事件相關(guān)操作技巧,需要的朋友可以參考下2017-06-06如何在UpdatePanel中調(diào)用JS客戶(hù)端腳本
本文將介紹如何在UpdatePanel中調(diào)用JS客戶(hù)端腳本,需要了解的朋友可以參考下2012-12-12