コード例 #1
0
        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public async static Task <string> GetEncryptedLocalStringValueOrDefault(string Key, string defaultValue)
        {
            string value;

            // If the key exists, retrieve the value.
            var obj = ApplicationData.Current.LocalSettings.Values[Key];

            if (obj != null)
            {
                value = (string)ApplicationData.Current.LocalSettings.Values[Key];
                if (value != null)
                {
                    IBuffer EncryptedBytes = Convert.FromBase64String(value).AsBuffer();

                    var     provider         = new DataProtectionProvider();
                    IBuffer UnencryptedBytes = await provider.UnprotectAsync(EncryptedBytes);

                    string Final_UnEncryptedString = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, UnencryptedBytes);

                    return(Final_UnEncryptedString);
                }
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return(value);
        }
コード例 #2
0
        /// <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);
            }
        }
コード例 #3
0
        /// <inheritdoc />
        public async Task <IEnumerable <string> > GetStringsForIdentifier(string identifier)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // Create a DataProtectionProvider object.
                var provider   = new DataProtectionProvider(Descriptor);
                var returnList = new List <string>();

                foreach (var path in store.GetFileNames(GetStringsPath(identifier)))
                {
                    using (var stream = new BinaryReader(new IsolatedStorageFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, store)))
                    {
                        var length      = stream.ReadInt32();
                        var dataAsBytes = stream.ReadBytes(length);

                        // Decrypt the protected message specified on input.
                        var buffUnprotected = await provider.UnprotectAsync(dataAsBytes.AsBuffer());

                        // 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.
                        returnList.Add(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffUnprotected));
                    }
                }

                return(returnList);
            }
        }
コード例 #4
0
        public static async Task <string> decryptData(string str)
        {
            DataProtectionProvider Provider = new DataProtectionProvider("LOCAL=user");
            IBuffer decryptedBuffer         = await Provider.UnprotectAsync(CryptographicBuffer.DecodeFromBase64String(str));

            return(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decryptedBuffer));
        }
コード例 #5
0
        /// <inheritdoc />
        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()));
        }
コード例 #6
0
        /// <summary>
        /// The unprotect data async.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        private static async Task <string> UnprotectDataAsync(string data)
        {
            var     dataProtection  = new DataProtectionProvider(Scope);
            IBuffer encryptedBuffer = CryptographicBuffer.DecodeFromBase64String(data);
            IBuffer dataBuffer      = await dataProtection.UnprotectAsync(encryptedBuffer);

            return(CryptographicBuffer.ConvertBinaryToString(Encoding, dataBuffer));
        }
コード例 #7
0
        /// <summary>
        /// Unprotect a string containing data protection provider protected data from the current machine.
        /// </summary>
        /// <param name="encodedProtectedString">The Base64 encoded protected data</param>
        /// <returns>A UTF8 encoding string of the unprotected data</returns>
        public static async Task <string> UnprotectStringAsync(string encodedProtectedString)
        {
            DataProtectionProvider provider = new DataProtectionProvider();

            byte[] protectedData   = Convert.FromBase64String(encodedProtectedString);
            byte[] unprotectedData = (await provider.UnprotectAsync(protectedData.AsBuffer())).ToArray();
            return(Encoding.UTF8.GetString(unprotectedData, 0, unprotectedData.Length));
        }
コード例 #8
0
        private async Task <string> DecryptAsync(byte[] protectedBytes)
        {
            DataProtectionProvider provider = new DataProtectionProvider("LOCAL=user");
            var protectedBuffer             = CryptographicBuffer.CreateFromByteArray(protectedBytes);
            var plainBuffer = await provider.UnprotectAsync(protectedBuffer);

            return(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, plainBuffer));
        }
コード例 #9
0
ファイル: UWPFileIO.cs プロジェクト: jcapellman/jcMPP
        private async Task <string> decryptData(byte[] encryptedData)
        {
            var Provider = new DataProtectionProvider();

            var buffUnprotected = await Provider.UnprotectAsync(CryptographicBuffer.CreateFromByteArray(encryptedData));

            return(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffUnprotected));
        }
コード例 #10
0
ファイル: Protector.cs プロジェクト: cuprumator/UWP-TestVPN
        public static async Task <string> Unprotect(IBuffer tounprotect)
        {
            DataProtectionProvider Provider = new DataProtectionProvider();

            IBuffer buffUnprotected = await Provider.UnprotectAsync(tounprotect);

            return(CryptographicBuffer.ConvertBinaryToString(Encoding, buffUnprotected));
        }
コード例 #11
0
        public async Task <byte[]> DecryptAsync(byte[] bytes)
        {
            var data            = bytes.AsBuffer(0, bytes.Length);
            var provider        = new DataProtectionProvider();
            var unprotectedData = await provider.UnprotectAsync(data);

            return(unprotectedData.ToArray());
        }
コード例 #12
0
        private async Task <byte[]> UnprotectDataAsync(string protectedData)
        {
            DataProtectionProvider protectionProvider = new DataProtectionProvider(ProtectionScope);
            IBuffer protectedBuffer   = CryptographicBuffer.DecodeFromBase64String(protectedData);
            IBuffer unprotectedBuffer = await protectionProvider.UnprotectAsync(protectedBuffer);

            CryptographicBuffer.CopyToByteArray(unprotectedBuffer, out byte[] result);
            return(result);
        }
コード例 #13
0
        public async Task <string> UnprotectDataAsync(string strProtect, BinaryStringEncoding encoding)
        {
            DataProtectionProvider provider = new DataProtectionProvider();

            encoding = BinaryStringEncoding.Utf8;
            IBuffer buffProtect = CryptographicBuffer.ConvertStringToBinary(strProtect, encoding);
            IBuffer buffMsg     = await provider.UnprotectAsync(buffProtect);

            return(CryptographicBuffer.ConvertBinaryToString(encoding, buffMsg));
        }
コード例 #14
0
        public async Task <string> UnprotectData(string cipherText)
        {
            DataProtectionProvider provider = new DataProtectionProvider();
            IBuffer @protected      = CryptographicBuffer.DecodeFromBase64String(cipherText);
            IBuffer buffUnprotected = await provider.UnprotectAsync(@protected);

            String strClearText = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffUnprotected);

            return(strClearText);
        }
コード例 #15
0
        public static async Task <string> UnprotectAsync(this string encryptedText)
        {
            Contract.Requires(encryptedText != null);

            IBuffer encryptedBuffer = CryptographicBuffer.DecodeFromBase64String(encryptedText);
            var     provider        = new DataProtectionProvider();
            IBuffer clearBuffer     = await provider.UnprotectAsync(encryptedBuffer);

            return(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, clearBuffer));
        }
コード例 #16
0
        public async Task <string> Unprotect(IBuffer buffProtected)
        {
            var provider = new DataProtectionProvider();

            var buffUnprotected = await provider.UnprotectAsync(buffProtected);

            var strClearText = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffUnprotected);

            return(strClearText);
        }
コード例 #17
0
        public static async Task <IBuffer> UnprotectAsync(this IBuffer buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            var provider = new DataProtectionProvider();

            return(await provider.UnprotectAsync(buffer).AsTask().ConfigureAwait(false));
        }
コード例 #18
0
        public static async Task <string> Decrypt(string encrypted)
        {
            var provider    = new DataProtectionProvider(SID);
            var bytes       = Convert.FromBase64String(encrypted);
            var buffer      = bytes.AsBuffer();
            var plainBuffer = await provider.UnprotectAsync(buffer);

            var plainText = CryptographicBuffer.ConvertBinaryToString(UTF8, plainBuffer);

            return(plainText);
        }
        public byte[] Decrypt(byte[] encryptedMessage)
        {
            if (encryptedMessage == null)
            {
                return(null);
            }

            DataProtectionProvider dataProtectionProvider = new DataProtectionProvider(ProtectionDescriptor);
            IBuffer buffer = RunAsyncTaskAndWait(dataProtectionProvider.UnprotectAsync(encryptedMessage.AsBuffer()).AsTask());

            return(buffer.ToArray(0, (int)buffer.Length));
        }
        public byte[] Decrypt(byte[] encryptedMessage)
        {
            if (encryptedMessage == null)
            {
                return(null);
            }

            DataProtectionProvider dataProtectionProvider = new DataProtectionProvider(ProtectionDescriptor);
            IBuffer buffer = dataProtectionProvider.UnprotectAsync(encryptedMessage.AsBuffer()).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();

            return(buffer.ToArray(0, (int)buffer.Length));
        }
コード例 #21
0
        static public async Task <string> UnprotectDataAsync(string name)
        {
            DataProtectionProvider Provider = new DataProtectionProvider();

            var value = localSettings.Values[name];

            var buffer = CryptographicBuffer.DecodeFromBase64String(value as string);

            IBuffer buffUnprotected = await Provider.UnprotectAsync(buffer);

            return(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffUnprotected));
        }
コード例 #22
0
        public static async Task <String> Decrypt(string strProtected)
        {
            DataProtectionProvider Provider = new DataProtectionProvider();

            IBuffer buffProtected = CryptographicBuffer.DecodeFromBase64String(strProtected);

            IBuffer buffUnprotected = await Provider.UnprotectAsync(buffProtected);

            String strClearText = CryptographicBuffer.ConvertBinaryToString(encoding, buffUnprotected);

            return(strClearText);
        }
コード例 #23
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()));
        }
コード例 #24
0
        /// <summary>
        /// Decrypts data using local windows user security.
        /// </summary>
        /// <param name="encrypted">Data to decrypt</param>
        /// <returns>Decrypted data</returns>
        /// <remarks>
        /// Pretty safe, but the encrypted data will only be possible to decrypt on this same windows machine because we are using the windows account
        /// </remarks>
        public static byte[] Decrypt(byte[] encrypted)
        {
            DataProtectionProvider provider = new DataProtectionProvider(LocalUser);
            IBuffer buffer = CryptographicBuffer.CreateFromByteArray(encrypted);

            var decryptedBuffer = provider.UnprotectAsync(buffer).GetResults();

            byte[] decrypted = new byte[decryptedBuffer.Length];
            CryptographicBuffer.CopyToByteArray(decryptedBuffer, out decrypted);

            return(decrypted);
        }
コード例 #25
0
        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));
        }
コード例 #26
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 string Decrypt(string encryptedMessage)
        {
            if (string.IsNullOrEmpty(encryptedMessage))
            {
                return(encryptedMessage);
            }

            DataProtectionProvider dataProtectionProvider = new DataProtectionProvider(ProtectionDescriptor);
            IBuffer messageBuffer     = Convert.FromBase64String(encryptedMessage).AsBuffer();
            IBuffer unprotectedBuffer = RunAsyncTaskAndWait(dataProtectionProvider.UnprotectAsync(messageBuffer).AsTask());

            return(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, unprotectedBuffer));
        }
コード例 #28
0
        public static async Task <string> UnprotectAsync([NotNull] 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));
        }
コード例 #29
0
        protected async Task <String> UnprotectAsync(IBuffer buffProtected)
        {
            // Create a DataProtectionProvider object.
            DataProtectionProvider provider = new DataProtectionProvider();

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

            // Execution of the UnprotectAsync method resumes here
            // after the awaited task (Provider.UnprotectAsync) completes
            // Convert the unprotected message from an IBuffer object to a string.
            return(CryptographicBuffer.ConvertBinaryToString(Encoding, buffer));
        }
コード例 #30
0
        public static async Task <IBuffer> UnprotectAsync(byte[] encryptedText)
        {
            if (encryptedText == null)
            {
                throw new ArgumentNullException(nameof(encryptedText));
            }

            var encryptedBuffer = CryptographicBuffer.CreateFromByteArray(encryptedText);
            var provider        = new DataProtectionProvider();
            var clearBuffer     = await provider.UnprotectAsync(encryptedBuffer);

            return(clearBuffer);
            //return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, clearBuffer);
        }