I'm working on a file encryption UWP app on Windows. I found a way to do that in a blogpost and tried it with little modifications. But it always fails. I don't know what's wrong with the code.
I'm a beginner to UWP apps. I tried different methods to encrypt files. If there's any better way to do file encryption on UWP platform please mention them too.
It shows following exception:-
Exception from HRESULT : 0XC0000225
(When a user click a button it shows a dialog box and when user hit OK I need to encrypt the selected file.)
if (result == ContentDialogResult.Primary)
{
// Encrypt the file.
try {
IBuffer data = await FileIO.ReadBufferAsync(storageFile);
IBuffer SecuredData = await SampleDataProtectionStream("LOCAL = user", data);
EncryptedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("EncryptedFile" + storageFile.FileType, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBufferAsync(EncryptedFile, SecuredData);
}
catch (Exception ex)
{
Details.Text = ($"Error processing command for '{storageFile.Name}':\n {ex.Message}");
}
}
else
{
// Do nothing.
}
}
public async Task<IBuffer> SampleDataProtectionStream(String descriptor, IBuffer buffMsg)
{
DataProtectionProvider Provider = new DataProtectionProvider(descriptor);
InMemoryRandomAccessStream inputData = new InMemoryRandomAccessStream();
InMemoryRandomAccessStream protectedData = new InMemoryRandomAccessStream();
IOutputStream outputStream = inputData.GetOutputStreamAt(0);
DataWriter writer = new DataWriter(outputStream);
writer.WriteBuffer(buffMsg);
await writer.StoreAsync();
await outputStream.FlushAsync();
IInputStream source = inputData.GetInputStreamAt(0);
IOutputStream dest = protectedData.GetOutputStreamAt(0);
await Provider.ProtectStreamAsync(source, dest);
await dest.FlushAsync();
//Verify that the protected data does not match the original
DataReader reader1 = new DataReader(inputData.GetInputStreamAt(0));
DataReader reader2 = new DataReader(protectedData.GetInputStreamAt(0));
await reader1.LoadAsync((uint)inputData.Size);
await reader2.LoadAsync((uint)protectedData.Size);
IBuffer buffOriginalData = reader1.ReadBuffer((uint)inputData.Size);
IBuffer buffProtectedData = reader2.ReadBuffer((uint)protectedData.Size);
if (CryptographicBuffer.Compare(buffOriginalData, buffProtectedData))
{
throw new Exception("ProtectStreamAsync returned unprotected data");
}
// Return the encrypted data.
return buffProtectedData;
}
UPDATE : I tested this code too;
if (result == ContentDialogResult.Primary)
{
try
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
StorageFile file = await openPicker.PickSingleFileAsync();
IBuffer data = await FileIO.ReadBufferAsync(file);
IBuffer SecuredData = await SampleDataProtectionStream("LOCAL = user", data);
EncryptedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("EncryptedFile" + file.FileType, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBufferAsync(EncryptedFile, SecuredData);
}
catch (Exception ex)
{
Details.Text = ($"Error occured while processing command for '{storageFile.Name}':\n{ex.Message}");
}
}
Exception from HRESULT : 0XC0000225
I have tested with above code and open file with FileOpenPicker
it works well, it could encrypt file and make file in local storage. And error 0XC0000225
means can't find the file, please check if storageFile
is null.
private async void Button_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
try
{
IBuffer data = await FileIO.ReadBufferAsync(file);
IBuffer SecuredData = await SampleDataProtectionStream("LOCAL = user", data);
var EncryptedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("EncryptedFile" + file.FileType, CreationCollisionOpt
await FileIO.WriteBufferAsync(EncryptedFile, SecuredData);
}
catch (Exception ex)
{
Details.Text = ($"Error processing command for '{file.Name}':\n {ex.Message}");
}
}
else
{
}
}
User contributions licensed under CC BY-SA 3.0