Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            SecretKeyHelper secret = new SecretKeyHelper();
            var             key    = secret.GetKey();
            var             p      = secret.EncryptByPrivateKey("naUAk2KXMfzK5JEN", key.PrivateKey);
            var             d      = secret.DecryptByPublicKey(p, "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQCg6YUB4EuUyVQ3IokQBHOiLKrah9Jo1A57TbU1DDnCcqiBa7ziC+NP2GZEjssrAnhQfVuMstICH1QlMS3CH07gSD5e+AtAyzaBEP1gbGqO7mtHQ0fs+rxwNN8Z+cAUeoItjd6FNBidJKKTIHG5Leo69+UGLnfCAg97e/qyQ+m9twIBAw==");

            CreateHostBuilder(args).Build().Run();
        }
Ejemplo n.º 2
0
        public async Task <ServiceInvokeResult <AppConfigViewModel> > CreateAppAsync(AddAppConfig appConfig)
        {
            var existAppInfo = await _appConfigRepository.QueryAsQueryable(a => a.AppCode == appConfig.AppCode).FirstAsync();

            if (existAppInfo != null)
            {
                return(PrintInvokeResult(new AppConfigViewModel()
                {
                    AppId = existAppInfo.AppId, AppKey = existAppInfo.AppKey
                }, "此应用已申请"));
            }

            var maxAppId = await _appConfigRepository.QueryAsQueryable(a => true).OrderBy(a => a.AppId, SqlSugar.OrderByType.Desc).Select(a => a.AppId).FirstAsync();

            var newAppId = maxAppId == 0 ? 1000 : maxAppId + 1;
            var appKey   = SecretKeyHelper.GetRandomKey();
            var unixTime = DateTime.Now.ToUnixTime(true);
            var app      = new AppConfig(newAppId, appConfig.AppCode, appKey, appConfig.AppName, unixTime);

            //领域模型验证
            var validationResult = new AppConfigValidation().Validate(app);

            if (!validationResult.IsValid)
            {
                return(PrintInvokeResult <AppConfigViewModel>(null, "模型检验失败"));
            }

            await _appConfigRepository.Add(app);

            //写入缓存
            var key             = string.Format(CacheKeyConstant.AppConfig, newAppId);
            var cacheingCommand = new CacheingCommand(key, app, 120);
            await _bus.SendCommand(cacheingCommand);

            var viewModel = new AppConfigViewModel()
            {
                AppId = newAppId, AppKey = appKey
            };

            return(PrintInvokeResult(viewModel, "应用创建成功"));
        }
Ejemplo n.º 3
0
        public void testSecretKey()
        {
            var mingwen = "Hello world";

            string key = "8bdc1382852f11ea940dd4ae52c963a9";

            string prikey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>";
            string pubkey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

            var aesE = SecretKeyHelper.AESEncrypt(mingwen, key);
            var aesD = SecretKeyHelper.AESDEncrypt(aesE, key);

            //RSAEncrypt
            var result = SecretKeyHelper.RSAEncrypt(pubkey, mingwen);

            //RSADecrypt
            var decrypt = SecretKeyHelper.RSADecrypt(prikey, result);

            //check result
            bool Isok = mingwen == decrypt;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generate secret keys
        /// </summary>
        /// <param name="performContext"></param>
        /// <param name="dbContext"></param>
        public static void GenerateSecretKeys(PerformContext performContext, DbAppContext dbContext)
        {
            try
            {
                performContext.WriteLine("*** Generating New Secret Keys ***");
                Debug.WriteLine("Generating New Secret Keys");

                int    ii = 0;
                string _oldTableProgress = "SecretKeys_Progress";
                string _newTable         = "SecretKeys";

                // check if the secret keys have already been completed
                int startPoint = ImportUtility.CheckInterMapForStartPoint(dbContext, _oldTableProgress, BcBidImport.SigId, _newTable);

                if (startPoint == BcBidImport.SigId)    // this means the assignment job is complete
                {
                    performContext.WriteLine("*** Generating New Secret Key is complete from the former process ***");
                    return;
                }

                // get records
                List <HetOwner> owners = dbContext.HetOwner.AsNoTracking()
                                         .Where(x => x.BusinessId == null)
                                         .ToList();

                int i = 0;

                foreach (HetOwner owner in owners)
                {
                    i++;
                    string key = SecretKeyHelper.RandomString(8, owner.OwnerId);

                    string temp = owner.OwnerCode;

                    if (string.IsNullOrEmpty(temp))
                    {
                        temp = SecretKeyHelper.RandomString(4, owner.OwnerId);
                    }

                    key = temp + "-" + DateTime.UtcNow.Year + "-" + key;

                    // get owner and update
                    HetOwner ownerRecord = dbContext.HetOwner.First(x => x.OwnerId == owner.OwnerId);
                    ownerRecord.SharedKey = key;

                    if (i % 500 == 0)
                    {
                        dbContext.SaveChangesForImport();
                    }

                    // save change to database
                    if (ii++ % 100 == 0)
                    {
                        try
                        {
                            Debug.WriteLine("Generating New Secret Keys - Index: " + ii);
                            ImportUtility.AddImportMapForProgress(dbContext, _oldTableProgress, ii.ToString(), BcBidImport.SigId, _newTable);
                            dbContext.SaveChangesForImport();
                        }
                        catch (Exception e)
                        {
                            performContext.WriteLine("Error saving data " + e.Message);
                        }
                    }
                }

                // ************************************************************
                // save final set of updates
                // ************************************************************
                try
                {
                    performContext.WriteLine("*** Done generating New Secret Keys ***");
                    Debug.WriteLine("Generating New Secret Keys is Done");
                    ImportUtility.AddImportMapForProgress(dbContext, _oldTableProgress, BcBidImport.SigId.ToString(), BcBidImport.SigId, _newTable);
                    dbContext.SaveChangesForImport();
                }
                catch (Exception e)
                {
                    string temp = string.Format("Error saving data (Record: {0}): {1}", ii, e.Message);
                    performContext.WriteLine(temp);
                    throw new DataException(temp);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }