简体   繁体   English

在Windows应用商店应用中加密和解密本地图像

[英]Encrypt & Decrypt Local Images in Windows Store App

I'm building a Windows Store App including a local folder of Images. 我正在构建一个Windows应用商店应用程序,包括图像的本地文件夹。

I want to protect all the Images so they can't be accessed from: 我想保护所有图像,以便无法访问它们:

C:\Users[username]\AppData\Local\Packages\LocalState\Settings\settings.dat

I know I should encrypt and decrypt the Images using the DataProtectionProvider class, but the documentation only shows how to encrypt/decrypt strings... 我知道我应该使用DataProtectionProvider类加密和解密图像,但文档只显示如何加密/解密字符串...

How should I convert a Bitmap image into a byte array? 我应该如何将Bitmap图像转换为字节数组? or should I encode it with Base64 ? 或者我应该用Base64编码吗? Is there any tutorial or sample using this process? 是否有使用此过程的教程或示例?

It's easiest if the images you want to encrypt are loaded from files and written back out to files. 如果要加密的图像是从文件加载并写回文件,这是最简单的。 Then you can do: 然后你可以这样做:

async void EncryptFile(IStorageFile fileToEncrypt, IStorageFile encryptedFile)
{
    IBuffer buffer = await FileIO.ReadBufferAsync(fileToEncrypt);

    DataProtectionProvider dataProtectionProvider = 
        new DataProtectionProvider(ENCRYPTION_DESCRIPTOR);

    IBuffer encryptedBuffer = 
        await dataProtectionProvider.ProtectAsync(buffer);

    await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);
}

DataProtectionProvider.ProtectStreamAsync is another alternative if you can get stream instances from your inputs and outputs. 如果您可以从输入和输出获取流实例,则DataProtectionProvider.ProtectStreamAsync是另一种替代方法。 For example, if you have a byte[] containing your image data then you can create an in-memory input stream from it: 例如,如果你有一个包含图像数据的byte[] ,那么你可以从它创建一个内存输入流:

byte[] imageData = ...
using (var inputMemoryStream = new MemoryStream(imageData).AsInputStream())
{
    ...
}

Edit: Then for example to decrypt the file and display it in an Image control you could do: 编辑:然后例如解密文件并将其显示在Image控件中,您可以执行以下操作:

var encryptedBuffer = await FileIO.ReadBufferAsync(encryptedFile);

var dataProtectionProvider = new DataProtectionProvider();

var buffer = await dataProtectionProvider.UnprotectAsync(encryptedBuffer);

var bmp = new BitmapImage();
await bmp.SetSourceAsync(buffer.AsStream().AsRandomAccessStream());
imageControl.Source = bmp;
        public async void Protect()
        {
            for (int i = 1; i < 24; i++)
            {
                string imageFile = ImagePages[i];
                var fileToEncrypt = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
                var encryptedFile1 = await ApplicationData.Current.LocalFolder.CreateFileAsync("encryptedPage" + i);

                var encryptedFile2 = await EncryptFile(fileToEncrypt, encryptedFile1);
                IBuffer buffer = await DecryptFile(encryptedFile2);
                //(2.) It goes here and throw the 'System.ArgumentException' having the encryptedFile's ContentType=""

                var bmp = new BitmapImage();
                await bmp.SetSourceAsync(buffer.AsStream().AsRandomAccessStream());

                //Fill the List responsible for the Portrait View
                MyPortrait mp = new MyPortrait();
                mp.onlyImage = bmp;
                PImageList.Add(mp);
            }
        }    

        public async Task<IStorageFile> EncryptFile(IStorageFile fileToEncrypt, IStorageFile encryptedFile)
        {
            IBuffer buffer = await FileIO.ReadBufferAsync(fileToEncrypt);
            //I have no more exceptions here

            DataProtectionProvider dataProtectionProvider = new DataProtectionProvider("LOCAL=user");

            IBuffer encryptedBuffer = await dataProtectionProvider.ProtectAsync(buffer);
            //(1.) After arriving here when deploying it goes to (2.)

            await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);

            return encryptedFile;
        }

        public async Task<IBuffer> DecryptFile(IStorageFile encryptedFile)
        {
            var protectedBuffer = await FileIO.ReadBufferAsync(encryptedFile);

            var dataProtectionProvider = new DataProtectionProvider();

            var buffer = await dataProtectionProvider.UnprotectAsync(protectedBuffer);

            return buffer;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM