这是我用来加密和解密图像的简单程序。但我不想点击按钮加密或解密特定图像。我想在form_load
和form_closed
中使用它。我的图片位置为C:\Users\Mateen\Downloads\MyImage.jpg
。当Form打开时,图像应解密并显示在图片框中,当表单关闭时,它应该被加密回来。
任何人都可以提供帮助吗?
using System;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void EncryptFile()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "JPG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|JPEG Files (*.jpeg)|*.jpg|EXE Files (*.exe)|*.exe|GIF Files (*.gif)|*.gif";
dialog.InitialDirectory = @"C:\Users\Mateen\Downloads\MyImage.jpg";
dialog.Title = "Please select an image file to encrypt.";
byte[] ImageBytes;
if (dialog.ShowDialog() == DialogResult.OK)
{
ImageBytes = File.ReadAllBytes(dialog.FileName);
for (int i = 0; i < ImageBytes.Length; i++)
{
ImageBytes[i] = (byte)(ImageBytes[i] + 5);
}
File.WriteAllBytes(dialog.FileName, ImageBytes);
}
}
private void DecryptFile()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "JPG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|JPEG Files (*.jpeg)|*.jpg|EXE Files (*.exe)|*.exe|GIF Files (*.gif)|*.gif";
dialog.InitialDirectory = @"C:\Users\Mateen\Downloads\MyImage.jpg";
dialog.Title = "Please select an image file to decrypt.";
byte[] ImageBytes;
if (dialog.ShowDialog() == DialogResult.OK)
{
ImageBytes = File.ReadAllBytes(dialog.FileName);
for (int i = 0; i < ImageBytes.Length; i++)
{
ImageBytes[i] = (byte)(ImageBytes[i] - 5);
}
File.WriteAllBytes(dialog.FileName, ImageBytes);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
EncryptFile();
}
private void button2_Click(object sender, EventArgs e)
{
DecryptFile();
}
}
}
答案 0 :(得分:0)
对我来说似乎很难。解密部分并不难,但是如何确保程序在没有加密的情况下不退出?如果您的程序挂起会发生什么?如果它从TaskManager中被杀死怎么办?
答案 1 :(得分:0)
var file = new FileInfo("C:\Users\Mateen\Downloads\MyImage.jpg");
byte[] buffer;
using(var stream = file.OpenRead()) {
buffer = = new byte[stream.Length];
stream.Read(buffer,0,(int)stream.Length);
}
使用上面的代码,您的缓冲区将与您现在拥有的ImageBytes相同。
Rijndael加密字节[]到Base64
private static string Encrypt(byte[] input, DeriveBytes pwd)
{
using (var symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
var rgbKey = pwd.GetBytes(KeySize / 8);
using (var encryptor = symmetricKey.CreateEncryptor(rgbKey, InitVector))
{
using (var ms = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(input, 0, input.Length);
cryptoStream.FlushFinalBlock();
}
var ctb = ms.ToArray();
return Convert.ToBase64String(ctb);
}
}
}
}
private static byte[] Decrypt(string input, DeriveBytes pwd)
{
var inputBytes = Convert.FromBase64String(input);
using (var symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
var rgbKey = pwd.GetBytes(KeySize / 8);
using (var decryptor = symmetricKey.CreateDecryptor(rgbKey, InitVector))
{
using (var ms = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
}
return ms.ToArray();
}
}
}
}
DeriveBytes pwd这是一个可由用户提供的实际密码:
var derivedPassword = new Rfc2898DeriveBytes("{PasswordHERE}", InitVector);
您的代码:
using System;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void EncryptFile()
{
var file = new FileInfo("C:\Users\Mateen\Downloads\MyImage.jpg");
byte[] ImageBytes;
using(var stream = file.OpenRead()) {
ImageBytes= = new byte[stream.Length];
stream.Read(ImageBytes,0,(int)stream.Length);
}
for (int i = 0; i < ImageBytes.Length; i++)
{
ImageBytes[i] = (byte)(ImageBytes[i] + 5);
}
//do something with your encrypted image here.
}
private void DecryptFile()
{
var file = new FileInfo("C:\Users\Mateen\Downloads\MyImage.jpg");
byte[] ImageBytes;
using(var stream = file.OpenRead()) {
ImageBytes= = new byte[stream.Length];
stream.Read(ImageBytes,0,(int)stream.Length);
}
for (int i = 0; i < ImageBytes.Length; i++)
{
ImageBytes[i] = (byte)(ImageBytes[i] - 5);
}
//Do something here
}
private void Form1_Load(object sender, EventArgs e)
{
Encrypt()
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Decrypt()
}
}
}