Encrypt and decrypt local images in the Windows Store app

I am creating an application for the Windows Store, including the local Images folder.

I want to protect all images so that they cannot be accessed:

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

I know that I have to encrypt and decrypt images using the DataProtectionProvider class, but the documentation only shows how to encrypt / decrypt strings ...

How to convert a Bitmap image to an array of bytes? or should I encode it using Base64 ? Is there any tutorial or sample using this process?

+5
source share
2 answers

This is easiest if the images you want to encrypt are downloaded from files and written back 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 receive streaming instances from your inputs and outputs. For example, if you have a byte[] containing your image data, you can create an input stream from it in memory:

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

Edit: Then, for example, to decrypt the file and display it in the Image control, which you could do:

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

All Articles