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