Пример #1
0
        /// <summary>
        /// 客户端申请AES密码
        /// </summary>
        /// <param name="DeviceCode">设备号</param>
        /// <param name="Modulus">RSA公钥模</param>
        /// <param name="Exponent">RSA公钥指数</param>
        /// <param name="CryptAESKey">ASE密钥(密文)</param>
        /// <param name="CryptAESIV">ASE向量(密文)</param>
        /// <returns>0:成功 -100:设备号未在可登录的列表中登记</returns>
        public static int ApplyAESEncryptKey(string DeviceCode, string Modulus, string Exponent, out string CryptAESKey, out string CryptAESIV)
        {
            LogWriter.WriteLog("UserLogin.ApplyAESEncryptKey:DeviceCode=" + DeviceCode + ",Modulus=" + Modulus + ",Exponent=" + Exponent);
            CryptAESKey = ""; CryptAESIV = "";
            if (ConfigHelper.GetConfigBool("CheckDeviceCode"))
            {
                //if (!DeviceCode.StartsWith("iOS"))
                {
                    if (User_RegisterMACBLL.GetModelList("MacAddr='" + DeviceCode + "'").Count() == 0)
                    {
                        LogWriter.WriteLog("UserLogin.ApplyAESEncryptKey Error! DeviceCode not in allow lists! DeviceCode=" + DeviceCode);
                        return(-1003);
                    }
                }
            }

            string         cachekey = "EBMIF_DeviceCryptKey-" + DeviceCode;
            DeviceCryptKey key      = null;

            #region 从数据库中加载保存的密钥
            if (key == null)
            {
                string _keystr = "";

                if (UserBLL.AppCryptKey_LoadKey(DeviceCode, out _keystr) == 0 && !string.IsNullOrEmpty(_keystr))
                {
                    try
                    { key = JsonConvert.DeserializeObject <DeviceCryptKey>(_keystr); }
                    catch { }
                }
            }
            #endregion

            if (key == null)
            {
                //生成AES加密密钥
                key = new DeviceCryptKey(DeviceCode, Modulus, Exponent);
                key.GenerateAESKey();
                DataCache.SetCache(cachekey, key, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);

                //密钥保存至数据库
                UserBLL.AppCryptKey_SaveKey(DeviceCode, JsonConvert.SerializeObject(key));
            }

            //将密钥RSA加密
            RSAProvider.EncryptText(key.AESKey, Modulus, Exponent, out CryptAESKey);
            RSAProvider.EncryptText(key.AESIV, Modulus, Exponent, out CryptAESIV);

            if (ConfigHelper.GetConfigBool("DebugMode"))
            {
                LogWriter.WriteLog("UserLogin.ApplyAESEncryptKeyA:DeviceCode=" + DeviceCode + ",AESKey=" + key.AESKey + ",AESIV=" + key.AESIV);
            }
            LogWriter.WriteLog("UserLogin.ApplyAESEncryptKeyB:DeviceCode=" + DeviceCode + ",CryptAESKey=" + CryptAESKey + ",CryptAESIV=" + CryptAESIV);
            return(0);
        }