2015-04-27 29 views
5

Tôi đang xây dựng một ứng dụng Windows Store bao gồm thư mục hình ảnh cục bộ.Mã hóa & giải mã hình ảnh cục bộ trong ứng dụng Windows Store

tôi muốn bảo vệ tất cả các hình ảnh nên họ không thể được truy cập từ:

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

Tôi biết tôi nên mã hóa và giải mã hình ảnh bằng cách sử dụng lớp DataProtectionProvider, nhưng tài liệu chỉ cho thấy làm thế nào để mã hóa/giải mã các chuỗi ...

Làm cách nào để chuyển đổi hình ảnh bitmap thành mảng byte? hoặc tôi nên mã hóa nó với Base64? Có bất kỳ hướng dẫn hoặc mẫu nào sử dụng quy trình này không?

Trả lời

1

Dễ nhất là nếu hình ảnh bạn muốn mã hóa được tải từ tệp và được ghi lại tệp. Sau đó, bạn có thể làm:

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 là một giải pháp thay thế nếu bạn có thể nhận các phiên bản luồng từ đầu vào và đầu ra của mình. Ví dụ, nếu bạn có một byte[] chứa dữ liệu hình ảnh của bạn sau đó bạn có thể tạo thêm một input stream trong bộ nhớ từ nó:

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

Edit: Sau đó, ví dụ để giải mã các tập tin và hiển thị nó trong một điều khiển Image bạn có thể làm:

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; 
+0

Còn về 'encryptedFile' thì sao? Nó phải là một hình ảnh và nó phải được trống đầu tiên ... làm thế nào để tôi khởi tạo nó? Và về phương thức 'DecryptFile()', tôi có nên làm như vậy bằng cách sử dụng 'encryptedFile', một' decryptedFile', 'UnprotectAsync' và' WriteBufferAsync' mới không? – yalematta

+0

@LayaleMatta Bạn có thể khởi tạo 'encryptedFile' bằng cách sử dụng bất kỳ tệp/API mở nào, ví dụ: 'var encryptedFile = đang chờ ApplicationData.Current.LocalFolder.CreateFileAsync (tên tệp)'. Và yup; Tôi đã thêm một ví dụ giải mã vào câu trả lời của tôi ở trên để chỉ ra điều đó. – peterdn

+0

Tôi nhận được 'System.IO.FileNotFoundException' trong phương thức encryptFile tại' IBuffer buffer = await FileIO.ReadBufferAsync (fileToEncrypt); 'Tôi có suy nghĩ đúng và hình ảnh tồn tại trong thư mục cục bộ mà tôi đề cập đến. Đây là đường dẫn đến fileToEncrypt: '" C: \\ [...] \\ Dự án \\ eBookApp \\ eBookApp \\ bin \\ Gỡ lỗi \\ AppX \\\ Tài sản \\ Hình ảnh \\ page1.jpg " ' – yalematta

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

Tôi nghi ngờ đó là vì bạn đang cố gắng đọc từ 'encryptedFile' sau khi đã viết cho nó trong' EncryptFile'. Thử mở lại tệp đã mã hóa bằng 'GetFileAsync' trước khi chuyển nó sang' DecryptFile'. – peterdn

+1

Chính xác!Tôi đã trả về tệp encryptedFile từ phương thức EncryptFile! Cảm ơn bạn! Bạn đá @peterdn – yalematta

Các vấn đề liên quan