UnprotectAsync() публичный Метод

public UnprotectAsync ( [ data ) : IAsyncOperation
data [
Результат IAsyncOperation
        /// <summary>
        /// Retrieves the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>System.Byte[].</returns>
        /// <exception cref="System.Exception"></exception>
        public byte[] Retrieve(string key)
        {
            var mutex = new Mutex(false, key);

            try
            {
                mutex.WaitOne();

                var file = AppStorage.LocalFolder.GetFileAsync(key);

                var bufferTask = FileIO.ReadBufferAsync(file.GetResults());

                var buffer = bufferTask.GetResults();

                if (_optionalEntropy != null)
                {
                    buffer = _dataProtectionProvider.UnprotectAsync(buffer).GetResults();
                }

                return(buffer.ToArray());
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("No entry found for key {0}.", key), ex);
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }
Пример #2
0
        /// <summary>Get the value of a local setting.</summary>
        /// <param name="name">The name of the setting.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <param name="isEncrypted">Is the setting encrypted.</param>
        /// <returns>The value of the setting if available, or the default value.</returns>
        public static async Task<string> GetLocalSettingAsync(string name, string defaultValue = "", bool isEncrypted = false)
        {
            object value = ApplicationData.Current.LocalSettings.Values[name];
            if (value == null)
            {
                return defaultValue;
            }

            if (isEncrypted)
            {
                try
                {
                    var dpp = new DataProtectionProvider();
                    var decrypted = await dpp.UnprotectAsync(CryptographicBuffer.DecodeFromBase64String(value.ToString()));

                    return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decrypted);
                }
                catch
                {
                    return defaultValue;
                }
            }
            else
            {
                return value.ToString();
            }
        }
 public IObservable<byte[]> DecryptBlock(byte[] block)
 {
     // Do not include a protectionDescriptor
     // http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.dataprotection.dataprotectionprovider.unprotectasync.aspx
     var dpapi = new DataProtectionProvider();
     return dpapi.UnprotectAsync(block.AsBuffer()).ToObservable().Select(b => b.ToArray());
 }
        /// <summary>
        /// Implementation of Load from storage for Windows Store.
        /// </summary>
        protected async Task<byte[]> LoadDataAsync()
        {
            try
            {
                // find the storage file
                var localFolder = ApplicationData.Current.LocalFolder;

                var storageFile = await localFolder.GetFileAsync(StorageFile);

                // read the data. It will be encrypted data
                IBuffer buffProtected = await FileIO.ReadBufferAsync(storageFile);
                DataProtectionProvider provider = new DataProtectionProvider();

                // decrypt the data
                IBuffer clearBuffer = await provider.UnprotectAsync(buffProtected);

                // convert it to byte array
                byte[] clearBytes = new byte[clearBuffer.Length];
                CryptographicBuffer.CopyToByteArray(clearBuffer, out clearBytes);

                return clearBytes;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public async Task<string> UnprotectAsync(byte[] data, string key)
        {
            var provider = new DataProtectionProvider(key);
            var buffProtected = CryptographicBuffer.CreateFromByteArray(data);
            var buffUnprotected = await provider.UnprotectAsync(buffProtected);

            return CryptographicBuffer.ConvertBinaryToString(Encoding, buffUnprotected);
        }
Пример #6
0
		public static async Task<string> SaylaMass(this string encryptedText)
		{
			if (encryptedText == null)
				return null;
			var encryptedBuffer = CryptographicBuffer.DecodeFromBase64String(encryptedText);
			var provider = new DataProtectionProvider();
			var clearBuffer = await provider.UnprotectAsync(encryptedBuffer);
			return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, clearBuffer);
		}
        public static async Task<string> UnprotectAsync(this string encryptedText)
        {
            if (encryptedText == null)
                throw new ArgumentNullException("encryptedText");

            var encryptedBuffer = CryptographicBuffer.DecodeFromBase64String(encryptedText);
            var provider = new DataProtectionProvider();
            var clearBuffer = await provider.UnprotectAsync(encryptedBuffer);
            return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, clearBuffer);
        }
Пример #8
0
        protected override IObservable<byte[]> AfterReadFromDiskFilter(byte[] data, IScheduler scheduler)
        {
            if (data.Length == 0)
            {
                return Observable.Return(data);
            }

            var dpapi = new DataProtectionProvider();
            return dpapi.UnprotectAsync(data.AsBuffer()).ToObservable()
                .Select(x => x.ToArray());
        }
Пример #9
0
        public async Task<IBuffer> UnProtectAsync()
        {
            DataProtectionProvider Provider = new DataProtectionProvider();

            // Encrypt the message.
            IBuffer buffProtected = await Provider.UnprotectAsync(this.Buffer);

            // Execution of the SampleProtectAsync function resumes here
            // after the awaited task (Provider.ProtectAsync) completes.
            return buffProtected;
        }
        public async Task<string> ReadAccessToken(string name)
        {
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
            var file = await local.GetFileAsync(name + ".txt");

            var text = await FileIO.ReadBufferAsync(file);

            DataProtectionProvider Provider = new DataProtectionProvider();
            IBuffer buffUnprotected = await Provider.UnprotectAsync(text);

            return CryptographicBuffer.ConvertBinaryToString(ENCODING, buffUnprotected);
        }
Пример #11
0
        public async static Task<string> UnProtect(string data)
        {
            DataProtectionProvider provider;
            IBuffer unprotectedBuffer;
            IBuffer protectedBuffer;

            if (data == null)
                return null;
            provider = new DataProtectionProvider(descriptor);
            protectedBuffer = CryptographicBuffer.DecodeFromBase64String(data);
            unprotectedBuffer = await provider.UnprotectAsync(protectedBuffer).AsTask().ConfigureAwait(false);
            return CryptographicBuffer.ConvertBinaryToString(encoding, unprotectedBuffer);
        }
        public async Task<string> DecryptMessage(IBuffer buffer)
        {
            if (buffer == null) return string.Empty;

            var dataProtectionProvider = new DataProtectionProvider("LOCAL=user");

            // Decrypt the message
            IBuffer buffUnProtected = await dataProtectionProvider.UnprotectAsync(buffer);

            // Decode the buffer to a plaintext message.
            string message = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffUnProtected);
            return message;
        }
Пример #13
0
        public async Task<String> SampleUnprotectData(
            IBuffer buffProtected,
            BinaryStringEncoding encoding)
        {
            // Create a DataProtectionProvider object.
            DataProtectionProvider Provider = new DataProtectionProvider();

            // Decrypt the protected message specified on input.
            IBuffer buffUnprotected = await Provider.UnprotectAsync(buffProtected);

            // Execution of the SampleUnprotectData method resumes here
            // after the awaited task (Provider.UnprotectAsync) completes
            // Convert the unprotected message from an IBuffer object to a string.
            String strClearText = CryptographicBuffer.ConvertBinaryToString(encoding, buffUnprotected);

            // Return the plaintext string.
            return strClearText;
        }
Пример #14
0
        /// <summary>
        /// Decrypts an encrypted string
        /// </summary>
        /// <param name="message">String to decrypt</param>
        /// <returns>Decrypted string inside the encrypted param string</returns>
        public async Task<string> DecryptAsync(string message)
        {
            if (string.IsNullOrEmpty(message))
                return message;

            IBuffer buffer = CryptographicBuffer.DecodeFromBase64String(message);
            var protectedData = new DataProtectionProvider("LOCAL=user");
            var decryptedBuffer = await protectedData.UnprotectAsync(buffer);
            string base64message = CryptographicBuffer.EncodeToBase64String(decryptedBuffer);
            byte[] msgContents = Convert.FromBase64String(base64message);
            return System.Text.Encoding.UTF8.GetString(msgContents, 0, msgContents.Length);
        }
Пример #15
0
 public async Task<string> DecryptString(string input) {
     var provider = new DataProtectionProvider();
     var inputBuffer = CryptographicBuffer.DecodeFromBase64String(input);
     var decrypted = await provider.UnprotectAsync(inputBuffer);
     return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decrypted);
 }
Пример #16
0
        /// <summary>
        /// Retreive encrypted Data from the isolated storage
        /// </summary>
        /// <param name="dataIdentifier">identifier for the data to read</param>
        /// <returns>Decrypted string or null if no value has been stored for the given dataIdentifier</returns>
        internal async Task<string> RetrieveProtectedStringAsync(string dataIdentifier)
        {
            DataProtectionProvider Provider = new DataProtectionProvider("LOCAL=user");
            IBuffer buffer;

            try
            {
                buffer = await FileIO.ReadBufferAsync(await ApplicationData.Current.LocalFolder.GetFileAsync(dataIdentifier + ConstantsUniversal.EncryptedFileExt));
            }
            catch (FileNotFoundException)
            {
                return null;
            }
            
            IBuffer buffUnprotected = await Provider.UnprotectAsync(buffer);
            return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffUnprotected);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="descriptor">The descriptor string used to protect the data</param>
        public async void SampleDataProtection(String descriptor)
        {
            EncryptDecryptText.Text += "*** Sample Data Protection for " + descriptor + " ***\n";

            DataProtectionProvider Provider = new DataProtectionProvider(descriptor);
            EncryptDecryptText.Text += "    DataProtectionProvider is Ready\n";

            // Create random data for protection
            IBuffer data = CryptographicBuffer.GenerateRandom(73);
            EncryptDecryptText.Text += "    Original Data: " + CryptographicBuffer.EncodeToHexString(data) + "\n";

            // Protect the random data
            IBuffer protectedData = await Provider.ProtectAsync(data);
            EncryptDecryptText.Text += "    Protected Data: " + CryptographicBuffer.EncodeToHexString(protectedData) + "\n";

            if (CryptographicBuffer.Compare(data, protectedData))
            {
                EncryptDecryptText.Text += "ProtectAsync returned unprotected data";
                return;
            }

            EncryptDecryptText.Text += "    ProtectAsync succeeded\n";

            // Unprotect
            DataProtectionProvider Provider2 = new DataProtectionProvider();
            IBuffer unprotectedData = await Provider2.UnprotectAsync(protectedData);

            if (!CryptographicBuffer.Compare(data, unprotectedData))
            {
                EncryptDecryptText.Text += "UnprotectAsync returned invalid data";
                return;
            }

            EncryptDecryptText.Text += "    Unprotected Data: " + CryptographicBuffer.EncodeToHexString(unprotectedData) + "\n";
            EncryptDecryptText.Text += "*** Done!\n";
        }