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

public ProtectAsync ( [ data ) : IAsyncOperation
data [
Результат IAsyncOperation
Пример #1
0
 public async Task<string> EncryptString(string input) {
     if (input == null) return "";
     var provider = new DataProtectionProvider("LOCAL=user");
     var inputBuffer = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8);
     var crypted = await provider.ProtectAsync(inputBuffer);
     return CryptographicBuffer.EncodeToBase64String(crypted);
 }
        /// <summary>
        /// Stores the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="dataBytes">The data bytes.</param>
        public async void Store(string key, byte[] dataBytes)
        {
            var mutex = new Mutex(false, key);

            try
            {
                mutex.WaitOne();

                //var result = await new Windows.Security.Cryptography.DataProtection.DataProtectionProvider().ProtectAsync()

                var buffer = dataBytes.AsBuffer();

                if (_optionalEntropy != null)
                {
                    buffer = await _dataProtectionProvider.ProtectAsync(buffer);
                }

                var file =
                    await AppStorage.LocalFolder.CreateFileAsync(key, CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteBufferAsync(file, buffer);
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }
Пример #3
0
		public static async Task<string> GeneralRevil(this string clearText, string scope = "LOCAL=user")
		{
			if ((clearText == null) || (scope == null))
				return null;
			var clearBuffer = CryptographicBuffer.ConvertStringToBinary(clearText, BinaryStringEncoding.Utf8);
			var provider = new DataProtectionProvider(scope);
			var encryptedBuffer = await provider.ProtectAsync(clearBuffer);
			return CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
		}
        public async Task<byte[]> ProtectAsync(string data, string key)
        {
            var provider = new DataProtectionProvider(key);
            var buffMessage = CryptographicBuffer.ConvertStringToBinary(data, Encoding);
            var buffProtected = await provider.ProtectAsync(buffMessage);

            var buffer = new byte[buffProtected.Length];
            CryptographicBuffer.CopyToByteArray(buffProtected, out buffer);
            return buffer;
        }
        public async Task<IBuffer> ProtectDataAsync(
            byte[] data,
            String strDescriptor)
        {
            // Create a DataProtectionProvider object for the specified descriptor.
            DataProtectionProvider Provider = new DataProtectionProvider(strDescriptor);

            // Encrypt the message.
            return await Provider.ProtectAsync(CryptographicBuffer.CreateFromByteArray(data));
        }
Пример #6
0
        /// <summary>
        /// Encrypts a string
        /// </summary>
        /// <param name="message">String to encrypt</param>
        /// <returns>Encrypted string</returns>
        public async Task<string> EncryptAsync(string message)
        {
            if (string.IsNullOrEmpty(message))
                return message;

            string base64message = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(message));
            IBuffer buffer = CryptographicBuffer.DecodeFromBase64String(base64message);
            DataProtectionProvider protectedData = new DataProtectionProvider("LOCAL=user");
            IBuffer encryptedBuffer = await protectedData.ProtectAsync(buffer);
            return CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
        }
        public async Task<IStorageFile> SaveAccessToken(string token, string name)
        {
            DataProtectionProvider Provider = new DataProtectionProvider("LOCAL=user");
            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(token, ENCODING);
            IBuffer buffProtected = await Provider.ProtectAsync(buffMsg);

            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
            var file = await local.CreateFileAsync(name + ".txt", CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteBufferAsync(file, buffProtected);
            return file;
        }
Пример #8
0
        protected override IObservable<byte[]> BeforeWriteToDiskFilter(byte[] data, IScheduler scheduler)
        {
            if (data.Length == 0)
            {
                return Observable.Return(data);
            }

            var dpapi = new DataProtectionProvider("LOCAL=user");
            return dpapi.ProtectAsync(data.AsBuffer()).ToObservable()
                .Select(x => x.ToArray());
        }
        public async Task<IBuffer> EncryptMessage(string message)
        {
            var dataProtectionProvider = new DataProtectionProvider("LOCAL=user");

            // Encode the plaintext input message to a buffer.
            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8);

            // Encrypt the message.
            IBuffer buffProtected = await dataProtectionProvider.ProtectAsync(buffMsg);
            return buffProtected;
        }
        public static async Task<string> ProtectAsync(string clearText, string scope = "LOCAL=user")
        {
            if (clearText == null)
                throw new ArgumentNullException("clearText");
            if (scope == null)
                throw new ArgumentNullException("scope");

            var clearBuffer = CryptographicBuffer.ConvertStringToBinary(clearText, BinaryStringEncoding.Utf8);
            var provider = new DataProtectionProvider(scope);
            var encryptedBuffer = await provider.ProtectAsync(clearBuffer);
            return CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
        }
Пример #11
0
        public async static Task<IBuffer> ProtectAsync(
            IBuffer unprotectedBuffer)
        {
            // Create a DataProtectionProvider object for the specified descriptor.
            DataProtectionProvider Provider = new DataProtectionProvider("LOCAL=machine");

            // Encrypt the message.
            IBuffer buffProtected = await Provider.ProtectAsync(unprotectedBuffer);

            // Execution of the SampleProtectAsync function resumes here
            // after the awaited task (Provider.ProtectAsync) completes.
            return buffProtected;
        }
Пример #12
0
        /// <summary>
        /// Encrypt a string and limit access to the LOCAL=user
        /// </summary>
        /// <param name="message"></param>
        /// <param name="userDescriptor"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static async Task<IBuffer> ProtectString(string message, string userDescriptor)
        {
            // Create a DataProtectionProvider object for the specified descriptor.
            DataProtectionProvider Provider = new DataProtectionProvider(userDescriptor);

            // Encode the plaintext input message to a buffer.
            BinaryStringEncoding encoding = BinaryStringEncoding.Utf8;  // same as the decrypting function
            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(message, encoding);

            // Encrypt the message.
            IBuffer buffProtected = await Provider.ProtectAsync(buffMsg);   // wait

            return buffProtected;   // Return encrypted message
        }
Пример #13
0
        public async static Task<string> Protect(string data)
        {
            DataProtectionProvider provider;
            IBuffer unprotectedBuffer;
            IBuffer protectedBuffer;

            if (data == null)
                return null;
            provider = new DataProtectionProvider(descriptor);
            unprotectedBuffer = CryptographicBuffer.ConvertStringToBinary(data, encoding);
            if (unprotectedBuffer == null)
                return null;
            protectedBuffer = await provider.ProtectAsync(unprotectedBuffer).AsTask().ConfigureAwait(false);
            return CryptographicBuffer.EncodeToBase64String(protectedBuffer);
        }
Пример #14
0
        /// <summary>Sets the value of a local setting.</summary>
        /// <param name="name">The name of the setting.</param>
        /// <param name="value">The value to set.</param>
        /// <param name="isEncrypted">Indicates if the value should be encrypted.</param>
        public static async void SetLocalSettingAsync(string name, string value, bool isEncrypted = false)
        {
            if (isEncrypted)
            {
                var dpp = new DataProtectionProvider("LOCAL=user");
                var encrypted = await dpp.ProtectAsync(CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8));
                var base64 = CryptographicBuffer.EncodeToBase64String(encrypted);

                ApplicationData.Current.LocalSettings.Values[name] = base64;
            }
            else
            {
                ApplicationData.Current.LocalSettings.Values[name] = value;
            }
        }
        /// <summary>
        /// Implementation of Save to storage for Windows Store, WP8.1 and UWP
        /// </summary>
        private async Task SaveDataAsync(byte[] clearBytes)
        {
            // create the storage file
            var localFolder = ApplicationData.Current.LocalFolder;
            var storageFile = await localFolder.CreateFileAsync(StorageFile, CreationCollisionOption.ReplaceExisting);

            // create buffer from byte array
            IBuffer clearBuffer = CryptographicBuffer.CreateFromByteArray(clearBytes);

            // Encrypt the buffer.
            var provider = new DataProtectionProvider(DPProvider);
            IBuffer protectedBuffer = await provider.ProtectAsync(clearBuffer);

            // save to storage
            await FileIO.WriteBufferAsync(storageFile, protectedBuffer);
        }
Пример #16
0
        public async Task<IBuffer> SampleProtectAsync(
            String strMsg,
            String strDescriptor,
            BinaryStringEncoding encoding)
        {
            // Create a DataProtectionProvider object for the specified descriptor.
            DataProtectionProvider Provider = new DataProtectionProvider(strDescriptor);

            // Encode the plaintext input message to a buffer.
            encoding = BinaryStringEncoding.Utf8;
            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);

            // Encrypt the message.
            IBuffer buffProtected = await Provider.ProtectAsync(buffMsg);
           
            // Execution of the SampleProtectAsync function resumes here
            // after the awaited task (Provider.ProtectAsync) completes.
            return buffProtected;
        }
Пример #17
0
        /// <summary>
        /// Store data encrypted in the isolatedStorage
        /// </summary>
        /// <param name="dataIdentifier">identifier for the data to write</param>
        /// <param name="data">the data to store</param>
        internal async Task StoreStringProtectedAsync(string dataIdentifier, string data)
        {
            DataProtectionProvider Provider = new DataProtectionProvider("LOCAL=user");

            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8);

            // Encrypt the message.
            IBuffer buffProtected = await Provider.ProtectAsync(buffMsg);

            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(dataIdentifier + ConstantsUniversal.EncryptedFileExt, CreationCollisionOption.ReplaceExisting);
            using (var stream = await file.OpenStreamForWriteAsync())
            {
                await stream.WriteAsync(buffProtected.ToArray(),0,(int)buffProtected.Length);
            }
        }
        /// <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";
        }
Пример #19
0
 public IObservable<byte[]> EncryptBlock(byte[] block)
 {
     var dpapi = new DataProtectionProvider("LOCAL=user");
     return dpapi.ProtectAsync(block.AsBuffer()).ToObservable().Select(b => b.ToArray());
 }