我正在开发一个 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/