Exemple #1
0
        public void LocalEncrypt()
        {
            var value = DateTime.Now.ToString() + " 🔮🎮";

            var key_str = Environment.MachineName;

            (var key, var iv) = AESUtils.GetParameters(key_str);

            var aes_ofb = AESUtils.Create(mode: CipherMode.CFB);

            aes_ofb.Key = key;
            aes_ofb.IV  = iv;

            var data = AESUtils.EncryptToByteArray(aes_ofb, value);

            var value1 = AESUtils.DecryptToString(aes_ofb, data);

            Assert.IsTrue(value == value1);

            var aes_ofb_1 = AESUtils.Create(mode: CipherMode.CFB);

            aes_ofb_1.Key = key;
            aes_ofb_1.IV  = iv;

            var value2 = AESUtils.DecryptToString(aes_ofb, data);
            var value3 = AESUtils.DecryptToString(aes_ofb_1, data);

            Assert.IsTrue(value2 == value3);

            Assert.IsTrue(value1 == value3);
        }
        protected override CefReturnValue OnBeforeResourceLoad(CefBrowser browser, CefFrame frame, CefRequest request, CefRequestCallback callback)
        {
            var sc = DI.Get <CloudServiceClientBase>();

            if (request.Url.StartsWith(sc.ApiBaseUrl, StringComparison.OrdinalIgnoreCase))
            {
                var conn_helper = DI.Get <IApiConnectionPlatformHelper>();
                request.SetHeaderByName(Headers.Request.AppVersion, sc.Settings.AppVersionStr, true);
                if (webView.IsSecurity)
                {
                    if (webView.Aes == null)
                    {
                        webView.Aes = AESUtils.Create();
                    }
                    var skey_bytes = webView.Aes.ToParamsByteArray();
                    var skey_str   = conn_helper.RSA.EncryptToString(skey_bytes);
                    request.SetHeaderByName(Headers.Request.SecurityKey, skey_str, true);
                }
                Func <Task <JWTEntity?> > getAuthTokenAsync = () => conn_helper.Auth.GetAuthTokenAsync().AsTask();
                var authToken       = getAuthTokenAsync.RunSync();
                var authHeaderValue = conn_helper.GetAuthenticationHeaderValue(authToken);
                if (authHeaderValue != null)
                {
                    var authHeaderValueStr = authHeaderValue.ToString();
                    request.SetHeaderByName("Authorization", authHeaderValueStr, true);
                }
            }
            var returnValue = base.OnBeforeResourceLoad(browser, frame, request, callback);

            return(returnValue);
        }
Exemple #3
0
        public void MultipleEncrypt()
        {
            var aes_cbc_1 = AESUtils.Create();
            var aes_cfb_1 = AESUtils.Create(mode: CipherMode.CFB);
            var aes_cbc_2 = AESUtils.Create();

            var value = DateTime.Now.ToString() + " 🔮🎮";

            var bytes_1 = AESUtils.EncryptToByteArray(aes_cbc_1, value);
            var bytes_2 = AESUtils.Encrypt(aes_cfb_1, bytes_1);
            var bytes_3 = AESUtils.Encrypt(aes_cbc_2, bytes_2);
            var bytes_4 = bytes_3;

            var d_bytes_4 = bytes_4;

#pragma warning disable CA1416 // 验证平台兼容性
#if !ANDROID && !__ANDROID__ && !__MOBILE__
            if (DI.Platform == Platform.Windows)
            {
                bytes_4 = ProtectedData.Protect(bytes_3, null, DataProtectionScope.LocalMachine);

                d_bytes_4 = ProtectedData.Unprotect(bytes_4, null, DataProtectionScope.LocalMachine);
            }
#endif
#pragma warning restore CA1416 // 验证平台兼容性

            var d_bytes_3 = AESUtils.Decrypt(aes_cbc_2, d_bytes_4);
            var d_bytes_2 = AESUtils.Decrypt(aes_cfb_1, d_bytes_3);
            var d_value   = AESUtils.DecryptToString(aes_cbc_1, d_bytes_2);

            TestContext.WriteLine(d_value);
        }
Exemple #4
0
 public LocalDataProtectionProviderBase(
     IProtectedData protectedData,
     IDataProtectionProvider dataProtectionProvider)
 {
     this.protectedData          = protectedData;
     this.dataProtectionProvider = dataProtectionProvider;
     if (OperatingSystem2.IsWindows)
     {
         if (OperatingSystem2.IsWindows10AtLeast)
         {
             defaultELocalDataProtectionType = LocalDataProtectionType.Win10WithAesCFB;
         }
         else
         {
             defaultELocalDataProtectionType = LocalDataProtectionType.ProtectedDataWithAesCFB;
         }
     }
     else
     {
         defaultELocalDataProtectionType = LocalDataProtectionType.AesCFB;
     }
     _aes = new Lazy <Aes>(() =>
     {
         (var key, var iv) = MachineSecretKey;
         // https://github.com/dotnet/runtime/issues/42214#issuecomment-698495584
         // AES CFB in Windows 7 catch Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Unknown error (0xc10000bb)
         // AES CFB in Android catch CryptographicException: Bad PKCS7 padding. Invalid length
         var mode = OperatingSystem2.IsAndroid ? CipherMode.CBC : CipherMode.CFB;
         var r    = AESUtils.Create(key, iv, mode, PaddingMode.PKCS7);
         return(r);
     });
 }
Exemple #5
0
        public LocalDataProtectionProviderBase(
            IProtectedData protectedData,
            IDataProtectionProvider dataProtectionProvider)
        {
            this.protectedData          = protectedData;
            this.dataProtectionProvider = dataProtectionProvider;
            switch (DI.Platform)
            {
            case Platform.Windows:
                if (Environment.OSVersion.Version.Major >= 10)
                {
                    defaultELocalDataProtectionType = LocalDataProtectionType.Win10WithAesOFB;
                }
                else
                {
                    defaultELocalDataProtectionType = LocalDataProtectionType.ProtectedDataWithAesOFB;
                }
                break;

            case Platform.Linux:
                defaultELocalDataProtectionType = LocalDataProtectionType.AesOFB;
                break;

            default:
                defaultELocalDataProtectionType = LocalDataProtectionType.None;
                break;
            }
            _aes = new Lazy <Aes>(() =>
            {
                (byte[] key, byte[] iv) = MachineSecretKey;
                var r = AESUtils.Create(key, iv, CipherMode.CFB, PaddingMode.PKCS7);
                return(r);
            });
        }
        /// <summary>
        /// 开始第三方快速登录、注册、绑定
        /// </summary>
        /// <param name="vm"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public static async Task StartAsync(WindowViewModel vm, FastLoginChannel channel, bool isBind)
        {
            var app = IApplication.Instance;

            StartServer(app);
            var conn_helper = DI.Get <IApiConnectionPlatformHelper>();
            var apiBaseUrl  = ICloudServiceClient.Instance.ApiBaseUrl;

#if DEBUG
            if (UseLoopbackTest)
            {
                apiBaseUrl = "https://127.0.0.1:28110";
            }
#endif

            ThirdPartyLoginHelper.isBind = isBind;
            ThirdPartyLoginHelper.vm     = vm;
            Disposable.Create(() =>
            {
                if (vm == ThirdPartyLoginHelper.vm)
                {
                    ThirdPartyLoginHelper.vm = null;
                }
            }).AddTo(vm);
            if (tempAes == null)
            {
                tempAes = AESUtils.Create();                  // 每次创建新的之前的会失效
            }
            var skey_bytes           = tempAes.ToParamsByteArray();
            var skey_str             = conn_helper.RSA.EncryptToString(skey_bytes);
            var csc                  = DI.Get <CloudServiceClientBase>();
            var padding              = RSAUtils.DefaultPadding;
            var access_token         = string.Empty;
            var access_token_expires = string.Empty;
            if (isBind)
            {
                var authToken = await conn_helper.Auth.GetAuthTokenAsync();

                var authHeaderValue = conn_helper.GetAuthenticationHeaderValue(authToken);
                if (authHeaderValue != null)
                {
                    var authHeaderValueStr = authHeaderValue.ToString();
                    access_token = tempAes.Encrypt(authHeaderValueStr);
                    var now = DateTime.UtcNow;
                    access_token_expires = tempAes.Encrypt(now.ToString(DateTimeFormat.RFC1123));
                }
            }
            // &version={version}
            //var version = csc.Settings.AppVersionStr;
            var ver = _ThisAssembly.Version.Base64UrlEncode();
            var url = $"{apiBaseUrl}/ExternalLoginDetection/{(int)channel}?port={port}&sKey={skey_str}&sKeyPadding={padding.OaepHashAlgorithm}&ver={ver}&isBind={isBind}&access_token_expires={access_token_expires}&access_token={access_token}";
            await Browser2.OpenAsync(url);
        }
Exemple #7
0
        protected override CefReturnValue OnBeforeResourceLoad(CefBrowser browser, CefFrame frame, CefRequest request, CefRequestCallback callback)
        {
            var sc = DI.Get <CloudServiceClientBase>();

            if (request.Url.StartsWith(sc.ApiBaseUrl, StringComparison.OrdinalIgnoreCase))
            {
                request.SetHeaderByName(Headers.Request.AppVersion, sc.Settings.AppVersionStr, true);
                if (webView.IsSecurity)
                {
                    if (webView.Aes == null)
                    {
                        webView.Aes = AESUtils.Create();
                    }
                    var skey_bytes  = webView.Aes.ToParamsByteArray();
                    var conn_helper = DI.Get <IApiConnectionPlatformHelper>();
                    var skey_str    = conn_helper.RSA.EncryptToString(skey_bytes);
                    request.SetHeaderByName(Headers.Request.SecurityKey, skey_str, true);
                }
            }
            var returnValue = base.OnBeforeResourceLoad(browser, frame, request, callback);

            return(returnValue);
        }
Exemple #8
0
        public LocalDataProtectionProviderBase(
            IProtectedData protectedData,
            IDataProtectionProvider dataProtectionProvider)
        {
            this.protectedData          = protectedData;
            this.dataProtectionProvider = dataProtectionProvider;
            switch (DI.Platform)
            {
            case Platform.Windows:
                if (Environment.OSVersion.Version.Major >= 10)
                {
                    defaultELocalDataProtectionType = LocalDataProtectionType.Win10WithAesOFB;
                }
                else
                {
                    defaultELocalDataProtectionType = LocalDataProtectionType.ProtectedDataWithAesOFB;
                }
                break;

            case Platform.Linux:
                defaultELocalDataProtectionType = LocalDataProtectionType.AesOFB;
                break;

            default:
                defaultELocalDataProtectionType = LocalDataProtectionType.None;
                break;
            }
            _aes = new Lazy <Aes>(() =>
            {
                (byte[] key, byte[] iv) = MachineSecretKey;
                // https://github.com/dotnet/runtime/issues/42214#issuecomment-698495584
                // AES CFB in Windows 7 catch Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Unknown error (0xc10000bb)
                var r = AESUtils.Create(key, iv, CipherMode.CFB, PaddingMode.PKCS7);
                return(r);
            });
        }