110,561
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 加密保存
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
//进行加密
byte[] imgData =CommonClass.ImageOperae.EncryptFle(pictureBox1.Image, "201104");
string cmdText = "";
using (OleDbConnection conn = new OleDbConnection(CommonClass.OleDbHelper.connAutomation))
{
//数据库保存略
}
}
}
/// <summary>
/// 读取文件
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(CommonClass.OleDbHelper.connAutomation);
conn.Open();
OleDbDataAdapter obda = new OleDbDataAdapter("select [image] from table1 where id=6", conn);
DataTable dt = new DataTable();
obda.Fill(dt);
conn.Close();
byte[] buffer = dt.Rows[0][0] as byte[];
//解密打开
MemoryStream ms = new MemoryStream(CommonClass.ImageOperae.DecryptFile(buffer,"sxm"));
pictureBox1.Image = img;
}
/// <summary>
/// 图片加密
/// </summary>
public static Byte[] EncryptFile(Image img, string keyStr)
{
//通过des加密
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
//获取文件二进制字符
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] inputByteArray = ms.GetBuffer();
//获得加密字符串二进制字符
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
//计算指定字节组指定区域哈希值
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
//加密密钥数组
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;
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
ms.Close();
return ms.ToArray();
}
/// <summary>
/// 图片解密
/// </summary>
public static byte[] DecryptFile(byte[] inputByteArray, string keyStr)
{
//通过des解密
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//密钥数组
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
//定义哈希变量
SHA1 ha = new SHA1Managed();
//计算指定字节组指定区域哈希值
byte[] hb = ha.ComputeHash(keyByteArray);
//加密密钥数组
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();
cs.Close();
ms.Close();
return ms.ToArray();
}
byte[] inputByteArray = ms.GetBuffer();
byte[] inputByteArray = ms.ToArray();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
ms.Position = 0;
byte[] result = new byte[dinputByteArray.Length];
cs.Read(result, 0, result.Length);
return result;
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, 0, (int)fs.Length);
//关闭流
fs.Close();
//获得加密字符串二进制字符
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
//下面都一样
MemoryStream ms = new MemoryStream(inputByteArray);
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
cs.Read(...//bolabola自己改吧...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using System.Data.OleDb;
namespace CommonClass
{
public partial class ImageOperae : Form
{
public ImageOperae()
{
InitializeComponent();
}
/// <summary>
/// 加密保存
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
this.pictureBox1.ImageLocation = @"c:\Winter.jpg";
if (pictureBox1.Image != null)
{
//进行加密
byte[] imgData = CommonClass.ImageOperae.EncryptFile(pictureBox1.Image, "201104");
using (OleDbConnection conn = new OleDbConnection(CommonClass.OleDbHelper.connAutomation))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
//cmd.CommandText = "delete from table1";
//cmd.ExecuteNonQuery();
//cmd.Parameters.Clear();
OleDbParameter prm = new OleDbParameter();
prm.ParameterName = "@ppp";
prm.OleDbType = OleDbType.Binary;
prm.Value = imgData;
cmd.Parameters.Add(prm);
cmd.CommandText = "insert into table1 (id, [image]) values ('6', ?)";
int count = cmd.ExecuteNonQuery();
MessageBox.Show(count.ToString());
cmd.Dispose();
conn.Close();
}
}
}
/// <summary>
/// 读取文件
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(CommonClass.OleDbHelper.connAutomation);
conn.Open();
OleDbDataAdapter obda = new OleDbDataAdapter("select [image] from table1 where id=6", conn);
DataTable dt = new DataTable();
obda.Fill(dt);
conn.Close();
byte[] buffer = dt.Rows[0][0] as byte[];
//解密打开
MemoryStream ms = new MemoryStream(CommonClass.ImageOperae.DecryptFile(buffer, "201104"));
Image img = Image.FromStream(ms);
pictureBox1.Image = img;
}
/// <summary>
/// 图片加密
/// </summary>
public static Byte[] EncryptFile(Image img, string keyStr)
{
//通过des加密
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
//获取文件二进制字符
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] inputByteArray = ms.GetBuffer();
//获得加密字符串二进制字符
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
//计算指定字节组指定区域哈希值
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
//加密密钥数组
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;
ms = new MemoryStream(); //加上这句话
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
ms.Close();
return ms.ToArray();
}
/// <summary>
/// 图片解密
/// </summary>
public static byte[] DecryptFile(byte[] inputByteArray, string keyStr)
{
//通过des解密
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//密钥数组
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
//定义哈希变量
SHA1 ha = new SHA1Managed();
//计算指定字节组指定区域哈希值
byte[] hb = ha.ComputeHash(keyByteArray);
//加密密钥数组
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();
cs.Close();
ms.Close();
return ms.ToArray();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommonClass
{
public class OleDbHelper
{
public static string connAutomation
= @"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa; password=123;Initial Catalog=test;Data Source=.\sql2008";
}
}