private async Task <IBuffer> Protect(string input)
        {
            var buffer          = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8);
            var protectedbuffer = await _protector.ProtectAsync(buffer);

            return(protectedbuffer);
        }
Пример #2
0
        public static async Task <IBuffer> Protect(string toProtect)
        {
            DataProtectionProvider Provider = new DataProtectionProvider(Descriptor);
            IBuffer protectedBuffer         = CryptographicBuffer.ConvertStringToBinary(toProtect, Encoding);

            return(await Provider.ProtectAsync(protectedBuffer));
        }
Пример #3
0
        /// <summary>
        /// The protect data async.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        private static async Task <string> ProtectDataAsync(string data)
        {
            var     dataProtection  = new DataProtectionProvider(Scope);
            IBuffer dataBuffer      = CryptographicBuffer.ConvertStringToBinary(data, Encoding);
            IBuffer encryptedBuffer = await dataProtection.ProtectAsync(dataBuffer);

            return(CryptographicBuffer.EncodeToBase64String(encryptedBuffer));
        }
        private async Task <string> ProtectAsync(byte[] unprotectedData)
        {
            DataProtectionProvider protectionProvider = new DataProtectionProvider(ProtectionScope);
            IBuffer unprotectedBuffer = CryptographicBuffer.CreateFromByteArray(unprotectedData);
            IBuffer protectedBuffer   = await protectionProvider.ProtectAsync(unprotectedBuffer);

            return(CryptographicBuffer.EncodeToBase64String(protectedBuffer));
        }
Пример #5
0
        public static async Task <string> Encrypt(string plainText)
        {
            var provider  = new DataProtectionProvider(SID);
            var buffer    = CryptographicBuffer.ConvertStringToBinary(plainText, encoding: UTF8);
            var encrypted = await provider.ProtectAsync(buffer);

            return(Convert.ToBase64String(encrypted.ToArray()));
        }
        public static async Task <string> encryptData(string str)
        {
            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);

            DataProtectionProvider Provider = new DataProtectionProvider("LOCAL=user");

            return(CryptographicBuffer.EncodeToBase64String(await Provider.ProtectAsync(buffMsg)));
        }
Пример #7
0
        public async Task <byte[]> EncryptAsync(byte[] bytes)
        {
            var data          = bytes.AsBuffer(0, bytes.Length);
            var provider      = new DataProtectionProvider("Local=user");
            var protectedData = await provider.ProtectAsync(data);

            return(protectedData.ToArray());
        }
        private async Task <byte[]> EncryptAsync(string plainText)
        {
            DataProtectionProvider provider = new DataProtectionProvider("LOCAL=user");
            var plainBuffer     = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);
            var protectedBuffer = await provider.ProtectAsync(plainBuffer);

            CryptographicBuffer.CopyToByteArray(protectedBuffer, out byte[] protectedBytes);
            return(protectedBytes);
        }
Пример #9
0
        public async Task <string> ProtectDataAsync(string strMsg, string strDescriptor, BinaryStringEncoding encoding)
        {
            DataProtectionProvider provider = new DataProtectionProvider(strDescriptor);

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

            return(CryptographicBuffer.ConvertBinaryToString(encoding, buffProtect));
        }
Пример #10
0
        public static async Task <string> Encrypt(string strClearText)
        {
            DataProtectionProvider Provider = new DataProtectionProvider(strDescriptor);

            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strClearText, encoding);

            IBuffer buffProtected = await Provider.ProtectAsync(buffMsg);

            return(CryptographicBuffer.EncodeToBase64String(buffProtected));
        }
Пример #11
0
        private async Task <byte[]> encryptData(string unencryptedData)
        {
            var Provider = new DataProtectionProvider("LOCAL=user");

            var buffMsg = CryptographicBuffer.ConvertStringToBinary(unencryptedData, BinaryStringEncoding.Utf8);

            var buffProtected = await Provider.ProtectAsync(buffMsg);

            return(buffProtected.ToArray());
        }
Пример #12
0
        /// <summary>
        /// Store the refresh token to the local settings
        /// </summary>
        private async void StoreRefreshToken(string refreshToken)
        {
            // Encode the refresh token to a buffer
            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(refreshToken, BinaryStringEncoding.Utf8);

            // Encrypt the refresh token
            IBuffer buffProtected = await _provider.ProtectAsync(buffMsg);

            _localSettings.Values["refreshToken"] = CryptographicBuffer.EncodeToBase64String(buffProtected);
        }
Пример #13
0
        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)));
        }
Пример #14
0
        public async Task <string> ProtectData(string plainText)
        {
            DataProtectionProvider provider = new DataProtectionProvider("LOCAL=user");
            IBuffer buffMsg       = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);
            IBuffer buffProtected = await provider.ProtectAsync(buffMsg);

            string base64Encoded = CryptographicBuffer.EncodeToBase64String(buffProtected);

            return(base64Encoded);
        }
Пример #15
0
        private async Task <IBuffer> Protect(string dataToBeStored)
        {
            var provider = new DataProtectionProvider("LOCAL=user");

            var encoding      = BinaryStringEncoding.Utf8;
            var buffMsg       = CryptographicBuffer.ConvertStringToBinary(dataToBeStored, encoding);
            var buffProtected = await provider.ProtectAsync(buffMsg);

            return(buffProtected);
        }
Пример #16
0
        public int setDbCryptKey(String partition, String key, bool bPersistent)
        {
            Windows.Storage.Streams.IBuffer keyByte    = Encoding.UTF8.GetBytes(key).AsBuffer();
            DataProtectionProvider          protecotor = new DataProtectionProvider();
            IAsyncOperation <IBuffer>       task       = protecotor.ProtectAsync(keyByte);

            task.AsTask().Wait();
            Windows.Storage.Streams.IBuffer protectedKeyByte = task.GetResults();
            this.writeKeyToFile(partition, protectedKeyByte.ToArray());
            return(getErrorCode() == 0 ? 1 : 0);
        }
Пример #17
0
        void DoSet(string key, object value)
        {
            var data  = this.serializer.Serialize(value);
            var bytes = Encoding.UTF8.GetBytes(data);

            // LOCAL=user and LOCAL=machine do not require enterprise auth capability
            var provider = new DataProtectionProvider("LOCAL=user");
            var buffer   = provider.ProtectAsync(bytes.AsBuffer()).GetResults();

            this.settingsStore.Set(key, buffer.ToArray());
        }
Пример #18
0
        /// <summary>
        /// Protectd a string for use on a local machine using the data protection providers.
        /// </summary>
        /// <param name="unprotectedString">The unprotected string to protect</param>
        /// <returns></returns>
        public static async Task <string> ProtectStringAsync(string unprotectedString)
        {
            var provider = new DataProtectionProvider("LOCAL=machine");

            byte[]  unprotectedData = Encoding.UTF8.GetBytes(unprotectedString);
            IBuffer protectedBuffer = await provider.ProtectAsync(unprotectedData.AsBuffer());

            byte[] protectedData = protectedBuffer.ToArray();

            return(Convert.ToBase64String(protectedData));
        }
Пример #19
0
        public static async Task <string> ProtectAsync(this string clearText, string scope = "LOCAL=user")
        {
            Contract.Requires(clearText != null);
            Contract.Requires(scope != null);

            IBuffer clearBuffer     = CryptographicBuffer.ConvertStringToBinary(clearText, BinaryStringEncoding.Utf8);
            var     provider        = new DataProtectionProvider(scope);
            IBuffer encryptedBuffer = await provider.ProtectAsync(clearBuffer);

            return(CryptographicBuffer.EncodeToBase64String(encryptedBuffer));
        }
Пример #20
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 byte[] Encrypt(byte[] message)
        {
            if (message == null)
            {
                return(new byte[] {});
            }

            DataProtectionProvider dataProtectionProvider = new DataProtectionProvider(ProtectionDescriptor);
            IBuffer protectedBuffer = RunAsyncTaskAndWait(dataProtectionProvider.ProtectAsync(message.AsBuffer()).AsTask());

            return(protectedBuffer.ToArray(0, (int)protectedBuffer.Length));
        }
        public byte[] Encrypt(byte[] message)
        {
            if (message == null)
            {
                return(new byte[] {});
            }

            DataProtectionProvider dataProtectionProvider = new DataProtectionProvider(ProtectionDescriptor);
            IBuffer protectedBuffer = dataProtectionProvider.ProtectAsync(message.AsBuffer()).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();

            return(protectedBuffer.ToArray(0, (int)protectedBuffer.Length));
        }
Пример #23
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));
        }
Пример #24
0
        static public async void ProtectAsync(string value, string name)
        {
            DataProtectionProvider Provider = new DataProtectionProvider("LOCAL=user");

            var buffMsg = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);

            var buffProtected = await Provider.ProtectAsync(buffMsg);

            var result = CryptographicBuffer.EncodeToBase64String(buffProtected);

            localSettings.Values[name] = result;
        }
        public string Encrypt(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return(message);
            }

            DataProtectionProvider dataProtectionProvider = new DataProtectionProvider(ProtectionDescriptor);
            IBuffer messageBuffer   = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8);
            IBuffer protectedBuffer = RunAsyncTaskAndWait(dataProtectionProvider.ProtectAsync(messageBuffer).AsTask());

            return(Convert.ToBase64String(protectedBuffer.ToArray(0, (int)protectedBuffer.Length)));
        }
        private static async Task EncryptAndWriteAsync(TokenCacheNotificationArgs args)
        {
            StorageFile cacheFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(CacheFileName, CreationCollisionOption.ReplaceExisting);

            byte[] clearBlob = args.TokenCache.SerializeMsalV3();

            DataProtectionProvider dataProtectionProvider = new DataProtectionProvider(ProtectionDescriptor);
            IBuffer protectedBuffer = await dataProtectionProvider.ProtectAsync(clearBlob.AsBuffer());

            byte[] protectedBlob = protectedBuffer.ToArray(0, (int)protectedBuffer.Length);

            await FileIO.WriteBytesAsync(cacheFile, protectedBlob);
        }
Пример #27
0
        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);
        }
Пример #28
0
        internal static async Task <string> EncryptCookieValueAsync(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return(message);
            }

            var base64message   = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(message));
            var buffer          = CryptographicBuffer.DecodeFromBase64String(base64message);
            var protectedData   = new DataProtectionProvider("LOCAL=user");
            var encryptedBuffer = await protectedData.ProtectAsync(buffer);

            return(CryptographicBuffer.EncodeToBase64String(encryptedBuffer));
        }
Пример #29
0
        static async Task PlatformSetAsync(string key, string data)
        {
            var settings = GetSettings(Alias);

            var bytes = Encoding.UTF8.GetBytes(data);

            // LOCAL=user and LOCAL=machine do not require enterprise auth capability
            var provider = new DataProtectionProvider("LOCAL=user");
            var buffer   = await provider.ProtectAsync(bytes.AsBuffer()).AsTask().ConfigureAwait(false);

            var encBytes = buffer.ToArray();

            settings.Values[key] = encBytes;
        }
Пример #30
0
        /// <summary>
        /// These providers do not require the enterprise authentication capability on either platform:
        /// "LOCAL=user"
        /// "LOCAL=machine"
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="protectionDescriptor"></param>
        /// <returns></returns>
        public static async Task <IBuffer> ProtectAsync(this IBuffer buffer, string protectionDescriptor = "LOCAL=user")
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (protectionDescriptor == null)
            {
                throw new ArgumentNullException("protectionDescriptor");
            }

            var provider = new DataProtectionProvider(protectionDescriptor);

            return(await provider.ProtectAsync(buffer).AsTask().ConfigureAwait(false));
        }