加密&解密Windows应用商店应用中的本地映像

时间:2015-04-27 13:29:36

标签: c# encryption windows-store-apps base64 windows-8.1

我正在构建一个包含图像本地文件夹的Windows应用商店应用。

我想保护所有图像,以便无法访问它们:

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

我知道我应该使用DataProtectionProvider类加密和解密图像,但是文档只显示了如何加密/解密字符串......

我应该如何将Bitmap图像转换为字节数组?或者我应该用Base64对其进行编码?是否有使用此过程的教程或示例?

2 个答案:

答案 0 :(得分:1)

如果要加密的图像是从文件加载并写回文件,这是最简单的。然后你可以这样做:

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是另一种选择。例如,如果您有一个包含图像数据的byte[],那么您可以从中创建内存输入流:

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

编辑然后例如解密文件并将其显示在您可以执行的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;

答案 1 :(得分:0)

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