Exemplo n.º 1
0
        /// <summary>
        /// Encrypts a string
        /// </summary>
        /// <param name="s">string to encrypt</param>
        /// <param name="encryption">which encryption to use.</param>
        /// <returns></returns>
        public static string Encrypt(string s, Encryption encryption)
        {
            CryptoHelper c = new CryptoHelper();

            switch (encryption)
            {
            case Encryption.none:
                return(s);

            case Encryption.AES:
                return(c.AesEncrypt(s, key));

            case Encryption.base64:
                return(c.Base64Encrypt(s));

            case Encryption.base64Reversed:
                return(c.Base64ReverseEncrypt(s));

            case Encryption.Salsa20:
                return(c.salsa20Encrypt(s, key));

            case Encryption.XOR:
                return(c.XorEncryptDecrypt(s, key));

            default:
                return(s);
            }
        }
 public static string GetQueryString(string body)
 {
     CryptoHelper.MiguCryptoKeys keys = CryptoHelper.CreateMiguCryptoKeys();
     byte[] aesEncrypted = CryptoHelper.AesEncrypt(Encoding.UTF8.GetBytes(body), CipherMode.CBC, keys.Key, keys.IV);
     byte[] rsaEncrypted = CryptoHelper.RsaEncrypt(keys.Password);
     return($"dataType=2&data={WebUtility.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes("Salted__").Concat(keys.Salt).Concat(aesEncrypted).ToArray()))}&secKey={WebUtility.UrlEncode(Convert.ToBase64String(rsaEncrypted))}");
 }
        public void Bench()
        {
            var kp = CryptoHelper.GenerateRsaKeyPair();



            var start = DateTime.UtcNow;

            for (int i = 0; i < 100000; i++)
            {
                var e = CryptoHelper.AesEncrypt("", "", CryptoHelper.GetSalt(16));
            }
            var duration2 = DateTime.UtcNow - start;


            start = DateTime.UtcNow;
            for (int i = 0; i < 10000; i++)
            {
                var e = CryptoHelper.RsaEncrypt(kp.PublicKey, CryptoHelper.GetSalt(16));
            }

            var duration1 = DateTime.UtcNow - start;

            Console.WriteLine("d");
        }
Exemplo n.º 4
0
        private static void BuildAssetBundles()
        {
            DLLMgr.Delete("Assets/HotUpdateResources/Dll/HotUpdateScripts.bytes");
            DLLMgr.Delete(Directory.GetParent(Application.dataPath) + "/Assets/XAsset/ScriptableObjects/Rules.asset");
            DLLMgr.Delete(Directory.GetParent(Application.dataPath) + "/Assets/XAsset/ScriptableObjects/Manifest.asset");


            CryptoWindow.ShowWindow();
            CryptoWindow.Build = s =>
            {
                var watch = new Stopwatch();
                watch.Start();
                var bytes  = DLLMgr.FileToByte(DLLMgr.DllPath);
                var result = DLLMgr.ByteToFile(CryptoHelper.AesEncrypt(bytes, s), "Assets/HotUpdateResources/Dll/HotUpdateScripts.bytes");
                watch.Stop();
                Log.Print("Convert Dlls in: " + watch.ElapsedMilliseconds + " ms.");
                if (!result)
                {
                    Log.PrintError("DLL转Byte[]出错!");
                }

                watch = new Stopwatch();
                watch.Start();
                BuildScript.ApplyBuildRules();
                watch.Stop();
                Log.Print("ApplyBuildRules in: " + watch.ElapsedMilliseconds + " ms.");

                watch = new Stopwatch();
                watch.Start();
                BuildScript.BuildAssetBundles();
                watch.Stop();
                Log.Print("BuildAssetBundles in: " + watch.ElapsedMilliseconds + " ms.");
            };
        }
Exemplo n.º 5
0
        private static void BuildAssetBundles()
        {
            DLLMgr.Delete("Assets/HotUpdateResources/Dll/HotUpdateScripts.bytes");
            // DLLMgr.Delete(Directory.GetParent(Application.dataPath)+"/Assets/XAsset/ScriptableObjects/Rules.asset");
            // DLLMgr.Delete(Directory.GetParent(Application.dataPath)+"/Assets/XAsset/ScriptableObjects/Manifest.asset");

            Action <string> buildAct = async s =>
            {
                var watch = new Stopwatch();
                watch.Start();
                var bytes  = DLLMgr.FileToByte(DLLMgr.DllPath);
                var result = DLLMgr.ByteToFile(CryptoHelper.AesEncrypt(bytes, s), "Assets/HotUpdateResources/Dll/HotUpdateScripts.bytes");
                watch.Stop();
                Log.Print("Convert Dlls in: " + watch.ElapsedMilliseconds + " ms.");
                if (!result)
                {
                    Log.PrintError("DLL转Byte[]出错!");
                    return;
                }

                Setting.EncryptPassword = s;

                await Task.Delay(3);

                AssetDatabase.Refresh();

                watch = new Stopwatch();
                watch.Start();
                BuildScript.ApplyBuildRules();
                watch.Stop();
                Log.Print("ApplyBuildRules in: " + watch.ElapsedMilliseconds + " ms.");

                watch = new Stopwatch();
                watch.Start();
                BuildScript.BuildAssetBundles();
                watch.Stop();
                Log.Print("BuildAssetBundles in: " + watch.ElapsedMilliseconds + " ms.");
            };

            if (string.IsNullOrEmpty(Setting.EncryptPassword))
            {
                CryptoWindow.ShowWindow();
                CryptoWindow.Build = buildAct;
            }
            else
            {
                buildAct.Invoke(Setting.EncryptPassword);
            }
        }
Exemplo n.º 6
0
        public static void MakeBytes()
        {
            var watch = new Stopwatch();

            watch.Start();
            var bytes  = FileToByte("Assets/HotUpdateResources/Dll/HotUpdateScripts.dll");
            var result = ByteToFile(CryptoHelper.AesEncrypt(bytes, "DevelopmentMode."), "Assets/HotUpdateResources/Dll/HotUpdateScripts.bytes");

            watch.Stop();
            Log.Print("Convert Dlls in: " + watch.ElapsedMilliseconds + " ms.");
            if (!result)
            {
                Log.PrintError("DLL转Byte[]出错!");
            }
        }
Exemplo n.º 7
0
        public async Task SimulateSendSecretMessage()
        {
            await Scatter.Connect();

            var fromKey = await Scatter.GetPublicKey(Scatter.Blockchains.EOSIO);

            var toKey = await Scatter.GetPublicKey(Scatter.Blockchains.EOSIO);

            var r              = new Random();
            var nonce          = (UInt64)r.Next();
            var text           = "Hello crypto secret message!";
            var encryptionKeyA = await Scatter.GetEncryptionKey(fromKey, toKey, nonce);

            var encryptionKeyABytes = UtilsHelper.HexStringToByteArray(encryptionKeyA);

            Console.WriteLine("FromKey:    {0}", fromKey);
            Console.WriteLine("ToKey:      {0}", toKey);
            Console.WriteLine("Original:   {0}", text);

            var encrypted = CryptoHelper.AesEncrypt(encryptionKeyABytes, text);

            Console.WriteLine("Encrypted:  {0}", Encoding.UTF8.GetString(encrypted));

            //...Send over the wire...

            var encryptionKeyB = await Scatter.GetEncryptionKey(toKey, fromKey, nonce);

            var encryptionKeyBBytes = UtilsHelper.HexStringToByteArray(encryptionKeyB);

            Console.WriteLine("A_PVT_KEY + B_PUB_KEY:    {0}", encryptionKeyA);
            Console.WriteLine("B_PVT_KEY + A_PUB_KEY:    {0}", encryptionKeyB);

            Assert.IsTrue(encryptionKeyA == encryptionKeyB);

            var roundtrip = CryptoHelper.AesDecrypt(encryptionKeyBBytes, encrypted);

            Console.WriteLine("Round Trip: {0}", roundtrip);
        }
Exemplo n.º 8
0
        public async Task OneWayEncryptDecrypt()
        {
            await Scatter.Connect();

            var fromKey = await Scatter.GetPublicKey(Scatter.Blockchains.EOSIO);

            var toKey = await Scatter.GetPublicKey(Scatter.Blockchains.EOSIO);

            var r             = new Random();
            var encryptionKey = await Scatter.GetEncryptionKey(fromKey, toKey, (UInt64)r.Next());

            var encryptionKeyBytes = UtilsHelper.HexStringToByteArray(encryptionKey);

            string text      = "Hello crypto secret message!";
            var    encrypted = CryptoHelper.AesEncrypt(encryptionKeyBytes, text);
            var    roundtrip = CryptoHelper.AesDecrypt(encryptionKeyBytes, encrypted);

            Console.WriteLine("FromKey:    {0}", fromKey);
            Console.WriteLine("ToKey:      {0}", toKey);
            Console.WriteLine("Original:   {0}", text);
            Console.WriteLine("Encrypted:  {0}", Encoding.UTF8.GetString(encrypted));
            Console.WriteLine("Round Trip: {0}", roundtrip);
        }
Exemplo n.º 9
0
    void LoadHotFixAssembly()
    {
        Appdomain = new AppDomain();
        _pdb      = null;

        byte[] buffer;

        //开发模式
        #if XASSET_PRO
        if (Assets.development)
        #else
        if (!Assets.runtimeMode)
        #endif
        {
            if (File.Exists(DLLPath))//直接读DLL
            {
                buffer = DLLMgr.FileToByte(DLLPath);

                //模拟加密
                buffer = CryptoHelper.AesEncrypt(buffer, key);
            }
            else
            {
                Log.PrintError("DLL文件不存在");
                return;
            }

            //查看是否有PDB文件
            if (File.Exists(PdbPath) && usePdb && (File.GetLastWriteTime(DLLPath) - File.GetLastWriteTime(PdbPath)).Seconds < 30)
            {
                _pdb = new MemoryStream(DLLMgr.FileToByte(PdbPath));
            }
        }
        else//真机模式解密加载
        {
            var dllAsset = Assets.LoadAsset("HotUpdateScripts.bytes", typeof(TextAsset));
            if (dllAsset.error != null)
            {
                Log.PrintError(dllAsset.error);
                return;
            }
            var dll = (TextAsset)dllAsset.asset;
            buffer = new byte[dll.bytes.Length];
            Array.Copy(dll.bytes, buffer, dll.bytes.Length);
            dllAsset.Release();//释放掉不需要再用的dll
        }
        try
        {
            // var original = CryptoHelper.AesDecrypt(dll.bytes, Key);以前的用法,过时了

            _fs = new JStream(buffer, key);

            /*
             * 如果一定要先解密,可以这样:
             * var original = CryptoHelper.AesDecrypt(dll.bytes, Key);
             * fs = new JStream(original, Key);
             * fs.Encrypted = false;
             */

            Appdomain.LoadAssembly(_fs, _pdb, new PdbReaderProvider());
        }
        catch (Exception e)
        {
            Log.PrintError("加载热更DLL错误:\n" + e);
            if (!usePdb)
            {
                Log.PrintError("加载热更DLL失败,请确保HotUpdateResources/Dll里面有HotUpdateScripts.bytes文件,并且Build Bundle后将DLC传入服务器");
            }
            else
            {
                Log.PrintError("PDB不可用,可能是DLL和PDB版本不一致,可能DLL是Release,如果是Release出包,请取消UsePdb选项,本次已跳过使用PDB");
                usePdb = false;
                LoadHotFixAssembly();
            }
            return;
        }

        Success = true;
        LoadILRuntime.InitializeILRuntime(Appdomain);
    }
Exemplo n.º 10
0
    void LoadHotFixAssembly()
    {
        Appdomain = new AppDomain();
        _pdb      = null;

        byte[] dll;

#if UNITY_EDITOR
        //开发模式
        if (!AssetMgr.RuntimeMode)
        {
            if (File.Exists(DLLPath)) //直接读DLL
            {
                dll = DLLMgr.FileToByte(DLLPath);

                //模拟加密
                dll = CryptoHelper.AesEncrypt(dll, key);
            }
            else
            {
                Log.PrintError("DLL文件不存在");
                return;
            }

            //查看是否有PDB文件
            if (File.Exists(PdbPath) && usePdb &&
                (File.GetLastWriteTime(DLLPath) - File.GetLastWriteTime(PdbPath)).Seconds < 30)
            {
                _pdb = new MemoryStream(DLLMgr.FileToByte(PdbPath));
            }
        }
        else //真机模式解密加载
#endif
        {
            var dllFile = (TextAsset)AssetMgr.Load(DllName, typeof(TextAsset));
            if (dllFile == null)
            {
                return;
            }

            dll = dllFile.bytes;
        }

        var buffer = new byte[dll.Length];
        Array.Copy(dll, buffer, dll.Length);
        AssetMgr.Unload(DllName, true);

        try
        {
            //这里默认用分块解密,JStream
            _fs = new JStream(buffer, key);

            /*
             * 如果一定要直接解密然后不进行分块解密加载Dll,可以这样:
             * var original = CryptoHelper.AesDecrypt(dll.bytes, Key);
             * _fs = new JStream(original, Key);
             * _fs.Encrypted = false;
             */

            Appdomain.LoadAssembly(_fs, _pdb, new PdbReaderProvider());
        }
        catch (Exception e)
        {
            Log.PrintError("加载热更DLL错误:\n" + e);
            if (!usePdb)
            {
                Log.PrintError(
                    "加载热更DLL失败,请确保HotUpdateResources/Dll里面有HotUpdateScripts.bytes文件,并且Build Bundle后将DLC传入服务器");
                Log.PrintError("也有可能是密码不匹配或密码包含特殊字符导致的");
            }
            else
            {
                Log.PrintError("PDB不可用,可能是DLL和PDB版本不一致,可能DLL是Release,如果是Release出包,请取消UsePdb选项,本次已跳过使用PDB");
                usePdb = false;
                LoadHotFixAssembly();
            }

            return;
        }

        Success = true;
        LoadILRuntime.InitializeILRuntime(Appdomain);
    }