private async Task RestoreNavigationStateAsync() { try { // Get the input stream for the SessionState file StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(NavigationStateFileName); using (IInputStream inStream = await file.OpenSequentialReadAsync()) { var memoryStream = new MemoryStream(); var provider = new DataProtectionProvider(NavigationDataProtectionProvider); // Decrypt the prevously saved session data. await provider.UnprotectStreamAsync(inStream, memoryStream.AsOutputStream()); // Deserialize the Session State var data = new DataContractSerializer(typeof(string)).ReadObject(memoryStream); RootFrame.SetNavigationState((string)data); } } catch (Exception e) { throw new Exception("Couldn't Restore the state.", e); } }
/// <summary> /// Restore the state from persistent storage /// </summary> /// <returns></returns> public async Task <bool> LoadAsync() { try { StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(this.Filename); using (var fs = await file.OpenSequentialReadAsync()) { var ms = new MemoryStream(); var provider = new DataProtectionProvider("LOCAL=user"); await provider.UnprotectStreamAsync(fs, ms.AsOutputStream()); ms.Seek(0, SeekOrigin.Begin); var knownTypes = this.KnownTypes ?? Enumerable.Empty <Type>(); var serializer = new DataContractSerializer(this._values.GetType(), knownTypes); XDocument doc = XDocument.Load(ms); this._values = (Dictionary <string, Dictionary <string, object> >)serializer.ReadObject(doc.Root.CreateReader()); return(true); } } catch (FileNotFoundException) { return(false); } catch (Exception ex) { throw new StateManagerException("Failed to load persisted state", ex); } }
/// <summary> /// Restores previously saved <see cref="SessionState"/>. /// </summary> /// <returns>An asynchronous task that reflects when session state has been read. The /// content of <see cref="SessionState"/> should not be relied upon until this task /// completes.</returns> public async Task RestoreSessionStateAsync() { _sessionState = new Dictionary <String, Object>(); try { // Get the input stream for the SessionState file StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(Constants.SessionStateFileName); using (IInputStream inStream = await file.OpenSequentialReadAsync()) { var memoryStream = new MemoryStream(); var provider = new DataProtectionProvider("LOCAL=user"); // Decrypt the prevously saved session data. await provider.UnprotectStreamAsync(inStream, memoryStream.AsOutputStream()); memoryStream.Seek(0, SeekOrigin.Begin); // Deserialize the Session State DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary <string, object>), _knownTypes); _sessionState = (Dictionary <string, object>)serializer.ReadObject(memoryStream); } } catch (Exception e) { throw new SessionStateServiceException(e); } }
public async Task <byte[]> Decrypt(byte[] encryptedBytes) { var provider = new DataProtectionProvider(); var encryptedContentBuffer = CryptographicBuffer.CreateFromByteArray(encryptedBytes); var contentInputStream = new InMemoryRandomAccessStream(); var unprotectedContentStream = new InMemoryRandomAccessStream(); IOutputStream outputStream = contentInputStream.GetOutputStreamAt(0); var dataWriter = new DataWriter(outputStream); dataWriter.WriteBuffer(encryptedContentBuffer); await dataWriter.StoreAsync(); await dataWriter.FlushAsync(); IInputStream decodingInputStream = contentInputStream.GetInputStreamAt(0); IOutputStream protectedOutputStream = unprotectedContentStream.GetOutputStreamAt(0); await provider.UnprotectStreamAsync(decodingInputStream, protectedOutputStream); await protectedOutputStream.FlushAsync(); DataReader reader2 = new DataReader(unprotectedContentStream.GetInputStreamAt(0)); await reader2.LoadAsync((uint)unprotectedContentStream.Size); IBuffer unprotectedBuffer = reader2.ReadBuffer((uint)unprotectedContentStream.Size); return(unprotectedBuffer.ToArray()); }
public static async void Decrypt(Stream input, Stream output) { var outputStream = output.AsOutputStream(); DataProtectionProvider provider = new DataProtectionProvider(LocalUser); await provider.UnprotectStreamAsync(input.AsInputStream(), outputStream); }
/// <summary> /// Unprotect stream data with data protection provider. /// </summary> /// <param name="source">The source stream contains the protected data.</param> /// <param name="destination">The destination stream contains the unprotected data.</param> /// <returns></returns> public async Task UnprotectStreamAsync(IInputStream source, IOutputStream destination) { var provider = new DataProtectionProvider(); try { await provider.UnprotectStreamAsync(source, destination); await destination.FlushAsync(); } catch (Exception) { } }
private async void onUnProtect(object sender, RoutedEventArgs e) { if (inputFile == null || outputFile == null) { return; } IRandomAccessStream inputstr = await inputFile.OpenAsync(FileAccessMode.Read); IRandomAccessStream outputstr = await outputFile.OpenAsync(FileAccessMode.ReadWrite); DataProtectionProvider dp = new DataProtectionProvider(); await dp.UnprotectStreamAsync(inputstr, outputstr); this.msgLabel.Text = "解密数据完成。"; inputFile = null; outputFile = null; ClearDisplay(); }
public static async Task <IRandomAccessStream> UnprotectPDFStream(IRandomAccessStream source) { // Create a DataProtectionProvider object. DataProtectionProvider Provider = new DataProtectionProvider(); InMemoryRandomAccessStream unprotectedData = new InMemoryRandomAccessStream(); IOutputStream dest = unprotectedData.GetOutputStreamAt(0); await Provider.UnprotectStreamAsync(source.GetInputStreamAt(0), dest); await unprotectedData.FlushAsync(); unprotectedData.Seek(0); return(unprotectedData); }
private async Task <String> SampleDataUnprotectStream( IBuffer buffProtected, BinaryStringEncoding encoding) { // Create a DataProtectionProvider object. DataProtectionProvider Provider = new DataProtectionProvider(); // Create a random access stream to contain the encrypted message. InMemoryRandomAccessStream inputData = new InMemoryRandomAccessStream(); // Create a random access stream to contain the decrypted data. InMemoryRandomAccessStream unprotectedData = new InMemoryRandomAccessStream(); // Retrieve an IOutputStream object and fill it with the input (encrypted) data. IOutputStream outputStream = inputData.GetOutputStreamAt(0); DataWriter writer = new DataWriter(outputStream); writer.WriteBuffer(buffProtected); await writer.StoreAsync(); await outputStream.FlushAsync(); // Retrieve an IInputStream object from which you can read the input (encrypted) data. IInputStream source = inputData.GetInputStreamAt(0); // Retrieve an IOutputStream object and fill it with decrypted data. IOutputStream dest = unprotectedData.GetOutputStreamAt(0); await Provider.UnprotectStreamAsync(source, dest); await dest.FlushAsync(); // Write the decrypted data to an IBuffer object. DataReader reader2 = new DataReader(unprotectedData.GetInputStreamAt(0)); await reader2.LoadAsync((uint)unprotectedData.Size); IBuffer buffUnprotectedData = reader2.ReadBuffer((uint)unprotectedData.Size); // Convert the IBuffer object to a string using the same encoding that was // used previously to conver the plaintext string (before encryption) to an // IBuffer object. String strUnprotected = CryptographicBuffer.ConvertBinaryToString(encoding, buffUnprotectedData); // Return the decrypted data. return(strUnprotected); }
public async Task RestoreAsync() { var file = await ApplicationData.Current.LocalFolder.GetFileAsync(SessionStateFileName); using (var inStream = await file.OpenSequentialReadAsync()) { using (var memoryStream = new MemoryStream()) { var provider = new DataProtectionProvider("LOCAL=user"); await provider.UnprotectStreamAsync(inStream, memoryStream.AsOutputStream()); memoryStream.Seek(0, SeekOrigin.Begin); var bytes = memoryStream.ToArray(); this.DeserializeState(bytes); } } this.LoadApplicationState(); }
/// <summary> /// Restores previously saved <see cref="SessionState"/>. /// </summary> /// <returns>An asynchronous task that reflects when session state has been read. The /// content of <see cref="SessionState"/> should not be relied upon until this task /// completes.</returns> public async Task RestoreSessionStateAsync() { _sessionState = new Dictionary <String, Object>(); try { JsonSerializer serializer = new JsonSerializer { TypeNameHandling = TypeNameHandling.All, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = DateTimeZoneHandling.Utc, }; // Get the input stream for the SessionState file StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(SessionStateFileName); using (IInputStream inStream = await file.OpenSequentialReadAsync()) using (var memoryStream = new MemoryStream()) { var provider = new DataProtectionProvider("LOCAL=user"); // Decrypt the prevously saved session data. await provider.UnprotectStreamAsync(inStream, memoryStream.AsOutputStream()); memoryStream.Seek(0, SeekOrigin.Begin); using (var reader = new JsonTextReader(new StreamReader(memoryStream))) { // Deserialize the Session State _sessionState = serializer.Deserialize <Dictionary <string, object> >(reader); } } } catch (Exception e) { throw new SessionStateServiceException(e); } }
public static async Task <IBuffer> SampleDataUnprotectStream(IBuffer buffProtected) { // Create a DataProtectionProvider object. DataProtectionProvider Provider = new DataProtectionProvider(); // Create a random access stream to contain the encrypted message. InMemoryRandomAccessStream inputData = new InMemoryRandomAccessStream(); // Create a random access stream to contain the decrypted data. InMemoryRandomAccessStream unprotectedData = new InMemoryRandomAccessStream(); // Retrieve an IOutputStream object and fill it with the input (encrypted) data. IOutputStream outputStream = inputData.GetOutputStreamAt(0); DataWriter writer = new DataWriter(outputStream); writer.WriteBuffer(buffProtected); await writer.StoreAsync(); await outputStream.FlushAsync(); // Retrieve an IInputStream object from which you can read the input (encrypted) data. IInputStream source = inputData.GetInputStreamAt(0); // Retrieve an IOutputStream object and fill it with decrypted data. IOutputStream dest = unprotectedData.GetOutputStreamAt(0); await Provider.UnprotectStreamAsync(source, dest); await dest.FlushAsync(); // Write the decrypted data to an IBuffer object. DataReader reader2 = new DataReader(unprotectedData.GetInputStreamAt(0)); await reader2.LoadAsync((uint)unprotectedData.Size); IBuffer buffUnprotectedData = reader2.ReadBuffer((uint)unprotectedData.Size); // Return the decrypted data. return(buffUnprotectedData); }
public async void SampleDataProtectionStream(String descriptor) { EncryptDecryptText.Text += "*** Sample Stream Data Protection for " + descriptor + " ***\n"; IBuffer data = CryptographicBuffer.GenerateRandom(10000); DataReader reader1, reader2; IBuffer buff1, buff2; DataProtectionProvider Provider = new DataProtectionProvider(descriptor); InMemoryRandomAccessStream originalData = new InMemoryRandomAccessStream(); //Populate the new memory stream IOutputStream outputStream = originalData.GetOutputStreamAt(0); DataWriter writer = new DataWriter(outputStream); writer.WriteBuffer(data); await writer.StoreAsync(); await outputStream.FlushAsync(); //open new memory stream for read IInputStream source = originalData.GetInputStreamAt(0); //Open the output memory stream InMemoryRandomAccessStream protectedData = new InMemoryRandomAccessStream(); IOutputStream dest = protectedData.GetOutputStreamAt(0); // Protect await Provider.ProtectStreamAsync(source, dest); //Flush the output if (await dest.FlushAsync()) { EncryptDecryptText.Text += " Protected output was successfully flushed\n"; } //Verify the protected data does not match the original reader1 = new DataReader(originalData.GetInputStreamAt(0)); reader2 = new DataReader(protectedData.GetInputStreamAt(0)); await reader1.LoadAsync((uint)originalData.Size); await reader2.LoadAsync((uint)protectedData.Size); EncryptDecryptText.Text += " Size of original stream: " + originalData.Size + "\n"; EncryptDecryptText.Text += " Size of protected stream: " + protectedData.Size + "\n"; if (originalData.Size == protectedData.Size) { buff1 = reader1.ReadBuffer((uint)originalData.Size); buff2 = reader2.ReadBuffer((uint)protectedData.Size); if (CryptographicBuffer.Compare(buff1, buff2)) { EncryptDecryptText.Text += "ProtectStreamAsync returned unprotected data"; return; } } EncryptDecryptText.Text += " Stream Compare completed. Streams did not match.\n"; source = protectedData.GetInputStreamAt(0); InMemoryRandomAccessStream unprotectedData = new InMemoryRandomAccessStream(); dest = unprotectedData.GetOutputStreamAt(0); // Unprotect DataProtectionProvider Provider2 = new DataProtectionProvider(); await Provider2.UnprotectStreamAsync(source, dest); if (await dest.FlushAsync()) { EncryptDecryptText.Text += " Unprotected output was successfully flushed\n"; } //Verify the unprotected data does match the original reader1 = new DataReader(originalData.GetInputStreamAt(0)); reader2 = new DataReader(unprotectedData.GetInputStreamAt(0)); await reader1.LoadAsync((uint)originalData.Size); await reader2.LoadAsync((uint)unprotectedData.Size); EncryptDecryptText.Text += " Size of original stream: " + originalData.Size + "\n"; EncryptDecryptText.Text += " Size of unprotected stream: " + unprotectedData.Size + "\n"; buff1 = reader1.ReadBuffer((uint)originalData.Size); buff2 = reader2.ReadBuffer((uint)unprotectedData.Size); if (!CryptographicBuffer.Compare(buff1, buff2)) { EncryptDecryptText.Text += "UnrotectStreamAsync did not return expected data"; return; } EncryptDecryptText.Text += "*** Done!\n"; }