如何在不打开对话框的情况下按位置加密特定图像

本文关键字:加密 位置 图像 情况下 打开对话框 | 更新日期: 2023-09-27 18:25:13

这是我用来加密和解密图像的简单程序。但我不想点击按钮并加密或解密特定的图像。我想在form_loadform_closed中使用它。我的图像位置是C:'Users'Mateen'Downloads'MyImage.jpg。当打开表单时,图像应该解密并显示在图片框中,当关闭表单时,它应该被加密回来。

有人能帮忙吗?

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();
        }
    }
}

如何在不打开对话框的情况下按位置加密特定图像

对我来说似乎很难。解密部分并不难,但如何确保程序在不加密的情况下不会退出?如果程序挂起,会发生什么情况?如果它被TaskManager杀死了怎么办?

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()
    }
}

}