c# - 如何解密内存中的加密图像并在应用程序中使用

标签 c# wpf encryption

我正在开发一个 WPF 应用程序,该应用程序使用文件夹中的资源(图像、视频等),我正在通过给定的加密机制加密所有资源

现在,当我通过应用下面给出的解密技术来使用资源时,这里我需要将文件保存在本地驱动器中的某个位置并在应用程序中使用,我不想要它,因为当文件解密后用户可以查看该文件并对其进行更改。

是否可以像内存中的图像一样解密文件并在应用程序中使用它,而无需将文件保存在内存中。

public class EncryptFile_DecryptFile   
{   
   #region Encrypt Images & save it  
   public string EncryptFile(Image img,string ImagePath_to_Save)  
    {          
       byte[] ImageBytes;
        ImageBytes = imageToByteArray(img);   

            for (int i = 0; i < ImageBytes.Length; i++)
            {
                ImageBytes[i] = (byte)(ImageBytes[i] ^ 5);
            }
            File.WriteAllBytes(ImagePath_to_Save, ImageBytes);
     return ImagePath_to_Save;
    }
   #endregion
   #region Convert Image in to Byte
   public byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }
    #endregion


   #region Decrypt Image & save it
   public string DecryptFile(string ImagePath_to_Save)
    {
             byte[] ImageBytes;

            ImageBytes = File.ReadAllBytes(ImagePath_to_Save);

            for (int i = 0; i < ImageBytes.Length; i++)
            {
                ImageBytes[i] = (byte)(ImageBytes[i] ^ 5);
            }

            File.WriteAllBytes(ImagePath_to_Save, ImageBytes);
             return ImagePath_to_Save;

    }
   #endregion
}

否则请建议我一些在应用程序中使用加密资源的替代方法。

最佳答案

您可以使用 MemoryStream而不是文件流。这会将您的图像保留在内存中。

以下是实现它的方法。

public Stream DecryptFile(string encryptedImageFile){
  byte[] ImageBytes;

  ImageBytes = File.ReadAllBytes(encryptedImageFile);

  for (int i = 0; i < ImageBytes.Length; i++){
    ImageBytes[i] = (byte)(ImageBytes[i] ^ 5);
  }

  return new MemoryStream(ImageBytes);
}

关于c# - 如何解密内存中的加密图像并在应用程序中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18865054/

相关文章:

php - 将解密算法传递到MySql select语句中

android - 保护客户端/服务器数据。 (安卓)

c# - 点击首先进入方法,然后转到另一个页面

c# - 获取有关 Android 库的触摸信息

c# - PhotoChooserTask的使用

WPF 数据网格绑定(bind) : add new item

WPF 工具提示绑定(bind)

c# - WPF Datagrid在添加新项目后进入编辑

c# - 在 WPF 中覆盖样式值的正确方法

java - Java加解密中IV和salt的处理