public void KeyGenerationNET()
        {
            var parms = new EncryptionParameters
            {
                DecompositionBitCount  = 4,
                NoiseStandardDeviation = 3.19,
                NoiseMaxDeviation      = 35.06
            };
            var coeffModulus = parms.CoeffModulus;

            coeffModulus.Resize(48);
            coeffModulus.Set("FFFFFFFFC001");
            var plainModulus = parms.PlainModulus;

            plainModulus.Resize(7);
            plainModulus.Set(1 << 6);
            var polyModulus = parms.PolyModulus;

            polyModulus.Resize(64, 1);
            polyModulus[0].Set(1);
            polyModulus[63].Set(1);

            var keygen = new KeyGenerator(parms);

            Assert.IsTrue(keygen.PublicKey.IsZero);
            Assert.IsTrue(keygen.SecretKey.IsZero);
            Assert.AreEqual(12, keygen.EvaluationKeys.Count);
            for (int i = 0; i < 12; ++i)
            {
                Assert.IsTrue(keygen.EvaluationKeys[i].IsZero);
            }

            keygen.Generate();
            Assert.IsFalse(keygen.PublicKey.IsZero);
            Assert.IsFalse(keygen.SecretKey.IsZero);
            Assert.AreEqual(12, keygen.EvaluationKeys.Count);
            for (int i = 0; i < 12; ++i)
            {
                Assert.IsFalse(keygen.EvaluationKeys[i].IsZero);
            }

            var publicKey = keygen.PublicKey;
            var secretKey = keygen.SecretKey;

            keygen.Generate();
            Assert.IsFalse(publicKey.Equals(keygen.PublicKey));
            Assert.IsFalse(secretKey.Equals(keygen.SecretKey));
        }
Ejemplo n.º 2
0
        public void FVKeyGenerationNoEVKNET()
        {
            var parms = new EncryptionParameters
            {
                DecompositionBitCount  = 0,
                NoiseStandardDeviation = 3.19,
                NoiseMaxDeviation      = 35.06
            };
            var coeffModulus = parms.CoeffModulus;

            coeffModulus.Resize(48);
            coeffModulus.Set("FFFFFFFFC001");
            var plainModulus = parms.PlainModulus;

            plainModulus.Resize(7);
            plainModulus.Set(1 << 6);
            var polyModulus = parms.PolyModulus;

            polyModulus.Resize(64, 1);
            polyModulus[0].Set(1);
            polyModulus[63].Set(1);

            var keygen = new KeyGenerator(parms);

            keygen.Generate(0);

            Assert.IsFalse(keygen.PublicKey[0].IsZero);
            Assert.IsFalse(keygen.PublicKey[1].IsZero);
            Assert.IsFalse(keygen.SecretKey.IsZero);
        }
Ejemplo n.º 3
0
        private void Get(long numberOfItems)
        {
            var keyGenerator = new KeyGenerator();
            var rand         = new Random();

            while (true)
            {
                try
                {
                    var keyIndex = rand.Next(0, Convert.ToInt32(numberOfItems));

                    _cache.Get(keyGenerator.Generate(keyIndex));

                    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                    {
                        _perfStats.IncrementFetches();
                        _perfStats.IncrementReqs();
                    }
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp);
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                LoadData();
                return(Page());
            }

            if (AcmeCertificate.KeyId < 0)
            {
                var key = _keyGenerator.Generate(AcmeCertificate.Subject, KeyAlgorithm.RS256);
                if (key == null)
                {
                    ModelState.AddModelError(string.Empty, "Error creating key");
                    return(Page());
                }

                AcmeCertificate.KeyId = key.KeyId;
            }

            AcmeCertificate.ApiKey1 = ApiKeyGenerator.CreateApiKey();
            AcmeCertificate.ApiKey2 = ApiKeyGenerator.CreateApiKey();

            _context.AcmeCertificates.Add(AcmeCertificate);
            await _context.SaveChangesAsync();

            StatusMessage = "Certificate created";

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 5
0
        public async Task <JsonResult> Filter(HomeSearchBindingModel model)
        {
            var cacheKey = KeyGenerator.Generate(
                WebConstants.CacheHomeFilterPrepend,
                model.SelectedCarType.ToString(),
                model.SelectedModelType,
                model.SelectedPriceRange,
                model.SelectedYear);

            var cachedModel = await this.cacheService.GetOrDefaultAsync <HomeSearchBindingModel>(cacheKey);

            if (cachedModel != null)
            {
                return(Json(cachedModel));
            }

            var priceRanges      = ParameterParser.ParsePriceRange(model.SelectedPriceRange);
            var filterStrategies = CarFilterStrategyFactory
                                   .GetStrategies(model.SelectedYear, priceRanges[0], priceRanges[1], WebConstants.AllFilterTypeModelValue);
            var mutipleFilterStrategy = CarMultipleFilterStrategyFactory.GetStrategy(new string[] { model.SelectedModelType });

            var filteredCars = this.carsService.GetFiltered <BaseCar>(filterStrategies.ToArray());

            filteredCars = mutipleFilterStrategy.Filter(filteredCars);

            var searchModel = await this.homeService.GetSearchModelAsync(filteredCars, model.SelectedCarType);

            _ = this.cacheService.AddInfinityCacheAsync(searchModel, cacheKey, WebConstants.CacheCarsType);

            return(Json(searchModel));
        }
Ejemplo n.º 6
0
        public override async Task <string> InsertURLAsync(string url)
        {
            using (var db = new LiteDB.LiteDatabase(DB_FileName))
            {
                var collection = db.GetCollection <URLObject>();

                url = url.ToLower();

                var existingResult = collection.FindOne(a => a.URL == url);

                if (existingResult != null)
                {
                    return(existingResult.Key);
                }

                var urlObject = new URLObject
                {
                    URL = url,
                    Key = KeyGenerator.Generate()
                };

                collection.Insert(urlObject);

                return(urlObject.Key);
            }
        }
        public void EncryptExponentiateDecryptNET()
        {
            var parms = new EncryptionParameters
            {
                DecompositionBitCount  = 4,
                NoiseStandardDeviation = 3.19,
                NoiseMaxDeviation      = 35.06
            };
            var coeffModulus = parms.CoeffModulus;

            coeffModulus.Resize(48);
            coeffModulus.Set("FFFFFFFFC001");
            var plainModulus = parms.PlainModulus;

            plainModulus.Resize(7);
            plainModulus.Set(1 << 4);
            var polyModulus = parms.PolyModulus;

            polyModulus.Resize(64, 1);
            polyModulus[0].Set(1);
            polyModulus[63].Set(1);

            var keygen = new KeyGenerator(parms);

            keygen.Generate();

            var Encoder = new BinaryEncoder(parms.PlainModulus);

            var encryptor      = new Encryptor(parms, keygen.PublicKey);
            var evaluator      = new Evaluator(parms, keygen.EvaluationKeys);
            var keygenEvals    = keygen.EvaluationKeys;
            var evaluatorEvals = keygen.EvaluationKeys;

            for (int i = 0; i < keygen.EvaluationKeys.Count; ++i)
            {
                Assert.AreEqual(keygenEvals[i], evaluatorEvals[i]);
            }
            var decryptor = new Decryptor(parms, keygen.SecretKey);

            var encrypted = encryptor.Encrypt(Encoder.Encode(5));
            var power     = evaluator.Exponentiate(encrypted, 1);

            Assert.AreEqual(5, Encoder.DecodeInt32(decryptor.Decrypt(power)));

            encrypted = encryptor.Encrypt(Encoder.Encode(5));
            power     = evaluator.Exponentiate(encrypted, 0);
            Assert.AreEqual(1, Encoder.DecodeInt32(decryptor.Decrypt(power)));

            encrypted = encryptor.Encrypt(Encoder.Encode(5));
            power     = evaluator.Exponentiate(encrypted, 0);
            Assert.AreEqual(1, Encoder.DecodeInt32(decryptor.Decrypt(power)));

            encrypted = encryptor.Encrypt(Encoder.Encode(7));
            power     = evaluator.Exponentiate(encrypted, 2);
            Assert.AreEqual(49, Encoder.DecodeInt32(decryptor.Decrypt(power)));

            encrypted = encryptor.Encrypt(Encoder.Encode(-7));
            power     = evaluator.Exponentiate(encrypted, 3);
            Assert.AreEqual(-343, Encoder.DecodeInt32(decryptor.Decrypt(power)));
        }
Ejemplo n.º 8
0
        public void FVKeyGenerationNoEVKNET()
        {
            var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

            parms.SetDecompositionBitCount(0);
            parms.SetNoiseStandardDeviation(3.19);
            parms.SetNoiseMaxDeviation(35.06);

            var coeffModulus = new BigUInt(48);

            coeffModulus.Set("FFFFFFFFC001");
            parms.SetCoeffModulus(coeffModulus);

            var plainModulus = new BigUInt(7);

            plainModulus.Set(1 << 6);
            parms.SetPlainModulus(plainModulus);

            var polyModulus = new BigPoly(65, 1);

            polyModulus[0].Set(1);
            polyModulus[64].Set(1);
            parms.SetPolyModulus(polyModulus);

            parms.Validate();

            var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew());

            keygen.Generate(0);

            Assert.IsFalse(keygen.PublicKey[0].IsZero);
            Assert.IsFalse(keygen.PublicKey[1].IsZero);
            Assert.IsFalse(keygen.SecretKey.IsZero);
        }
Ejemplo n.º 9
0
        public void FVEncryptDecryptNET()
        {
            var parms = new EncryptionParameters
            {
                DecompositionBitCount  = 4,
                NoiseStandardDeviation = 3.19,
                NoiseMaxDeviation      = 35.06
            };
            var coeffModulus = parms.CoeffModulus;

            coeffModulus.Resize(48);
            coeffModulus.Set("FFFFFFFFC001");
            var plainModulus = parms.PlainModulus;

            plainModulus.Resize(7);
            plainModulus.Set(1 << 6);
            var polyModulus = parms.PolyModulus;

            polyModulus.Resize(64, 1);
            polyModulus[0].Set(1);
            polyModulus[63].Set(1);

            var Encoder = new BinaryEncoder(parms.PlainModulus);

            var keygen = new KeyGenerator(parms);

            keygen.Generate();

            var encryptor = new Encryptor(parms, keygen.PublicKey);

            Assert.AreEqual(encryptor.PublicKey[0], keygen.PublicKey[0]);
            Assert.AreEqual(encryptor.PublicKey[1], keygen.PublicKey[1]);

            var decryptor = new Decryptor(parms, keygen.SecretKey);

            Assert.AreEqual(decryptor.SecretKey, keygen.SecretKey);

            var encrypted = encryptor.Encrypt(Encoder.Encode(0x12345678));

            Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0));
            Assert.AreEqual(0U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(1));
            Assert.AreEqual(1U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(2));
            Assert.AreEqual(2U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFDUL));
            Assert.AreEqual(0x7FFFFFFFFFFFFFFDUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFEUL));
            Assert.AreEqual(0x7FFFFFFFFFFFFFFEUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFFUL));
            Assert.AreEqual(0x7FFFFFFFFFFFFFFFUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted)));
        }
Ejemplo n.º 10
0
        private void SendData()
        {
            var key   = _keyGenerator.Generate();
            var timer = _metric.StartMeasure();

            _metric.AddResult(_adapter.Send(key, _iterator.Current));
            _metric.StopMeasure(timer);
        }
Ejemplo n.º 11
0
 public void Generate_KeyLength_DefinesStringLength()
 {
     for (var i = 1; i <= 100; i++)
     {
         using var target = new KeyGenerator(i);
         Assert.Equal(i, target.Generate().Length);
     }
 }
        public void EncryptAddDecryptNET()
        {
            var parms = new EncryptionParameters
            {
                DecompositionBitCount  = 4,
                NoiseStandardDeviation = 3.19,
                NoiseMaxDeviation      = 35.06
            };
            var coeffModulus = parms.CoeffModulus;

            coeffModulus.Resize(48);
            coeffModulus.Set("FFFFFFFFC001");
            var plainModulus = parms.PlainModulus;

            plainModulus.Resize(7);
            plainModulus.Set(1 << 6);
            var polyModulus = parms.PolyModulus;

            polyModulus.Resize(64, 1);
            polyModulus[0].Set(1);
            polyModulus[63].Set(1);

            var keygen = new KeyGenerator(parms);

            keygen.Generate();

            var Encoder = new BinaryEncoder(parms.PlainModulus);

            var encryptor = new Encryptor(parms, keygen.PublicKey);
            var evaluator = new Evaluator(parms, keygen.EvaluationKeys);
            var decryptor = new Decryptor(parms, keygen.SecretKey);

            var encrypted1 = encryptor.Encrypt(Encoder.Encode(0x12345678));
            var encrypted2 = encryptor.Encrypt(Encoder.Encode(0x54321));
            var sum        = evaluator.Add(encrypted1, encrypted2);

            Assert.AreEqual(0x12399999UL, Encoder.DecodeUInt64(decryptor.Decrypt(sum)));

            encrypted1 = encryptor.Encrypt(Encoder.Encode(0));
            encrypted2 = encryptor.Encrypt(Encoder.Encode(0));
            sum        = evaluator.Add(encrypted1, encrypted2);
            Assert.AreEqual(0UL, Encoder.DecodeUInt64(decryptor.Decrypt(sum)));

            encrypted1 = encryptor.Encrypt(Encoder.Encode(0));
            encrypted2 = encryptor.Encrypt(Encoder.Encode(5));
            sum        = evaluator.Add(encrypted1, encrypted2);
            Assert.AreEqual(5UL, Encoder.DecodeUInt64(decryptor.Decrypt(sum)));

            encrypted1 = encryptor.Encrypt(Encoder.Encode(5));
            encrypted2 = encryptor.Encrypt(Encoder.Encode(-3));
            sum        = evaluator.Add(encrypted1, encrypted2);
            Assert.AreEqual(2, Encoder.DecodeInt32(decryptor.Decrypt(sum)));

            encrypted1 = encryptor.Encrypt(Encoder.Encode(-7));
            encrypted2 = encryptor.Encrypt(Encoder.Encode(2));
            sum        = evaluator.Add(encrypted1, encrypted2);
            Assert.AreEqual(-5, Encoder.DecodeInt32(decryptor.Decrypt(sum)));
        }
Ejemplo n.º 13
0
        public void TransformEncryptedToFromNTTNET()
        {
            var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

            parms.SetDecompositionBitCount(4);
            parms.SetNoiseStandardDeviation(3.19);
            parms.SetNoiseMaxDeviation(35.06);

            var coeffModulus = new BigUInt(48);

            coeffModulus.Set("FFFFFFFFC001");
            parms.SetCoeffModulus(coeffModulus);

            var plainModulus = new BigUInt(7);

            plainModulus.Set(1 << 6);
            parms.SetPlainModulus(plainModulus);

            var polyModulus = new BigPoly(65, 1);

            polyModulus[0].Set(1);
            polyModulus[64].Set(1);
            parms.SetPolyModulus(polyModulus);

            parms.Validate();

            var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew());

            keygen.Generate();

            var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew());
            var evaluator = new Evaluator(parms, MemoryPoolHandle.AcquireNew());
            var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew());

            var plain  = new BigPoly(65, 1);
            var cipher = new BigPolyArray(2, 65, 1);

            plain.Set("0");
            encryptor.Encrypt(plain, cipher);
            evaluator.TransformToNTT(cipher);
            evaluator.TransformFromNTT(cipher);
            decryptor.Decrypt(cipher, plain);
            Assert.IsTrue(plain.ToString() == "0");

            plain.Set("1");
            encryptor.Encrypt(plain, cipher);
            evaluator.TransformToNTT(cipher);
            evaluator.TransformFromNTT(cipher);
            decryptor.Decrypt(cipher, plain);
            Assert.IsTrue(plain.ToString() == "1");

            plain.Set("Fx^10 + Ex^9 + Dx^8 + Cx^7 + Bx^6 + Ax^5 + 1x^4 + 2x^3 + 3x^2 + 4x^1 + 5");
            encryptor.Encrypt(plain, cipher);
            evaluator.TransformToNTT(cipher);
            evaluator.TransformFromNTT(cipher);
            decryptor.Decrypt(cipher, plain);
            Assert.IsTrue(plain.ToString() == "Fx^10 + Ex^9 + Dx^8 + Cx^7 + Bx^6 + Ax^5 + 1x^4 + 2x^3 + 3x^2 + 4x^1 + 5");
        }
Ejemplo n.º 14
0
        public void FVEncryptSquareDecryptNET()
        {
            var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

            parms.SetDecompositionBitCount(4);
            parms.SetNoiseStandardDeviation(3.19);
            parms.SetNoiseMaxDeviation(35.06);

            var coeffModulus = new BigUInt(48);

            coeffModulus.Set("FFFFFFFFC001");
            parms.SetCoeffModulus(coeffModulus);

            var plainModulus = new BigUInt(7);

            plainModulus.Set(1 << 6);
            parms.SetPlainModulus(plainModulus);

            var polyModulus = new BigPoly(65, 1);

            polyModulus[0].Set(1);
            polyModulus[64].Set(1);
            parms.SetPolyModulus(polyModulus);

            parms.Validate();

            var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew());

            keygen.Generate();

            var encoder = new BalancedEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew());

            var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew());
            var evaluator = new Evaluator(parms, MemoryPoolHandle.AcquireNew());
            var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew());

            var encrypted1 = encryptor.Encrypt(encoder.Encode(1));
            var product    = evaluator.Square(encrypted1);

            Assert.AreEqual(1UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(0));
            product    = evaluator.Square(encrypted1);
            Assert.AreEqual(0UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(-5));
            product    = evaluator.Square(encrypted1);
            Assert.AreEqual(25UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(-1));
            product    = evaluator.Square(encrypted1);
            Assert.AreEqual(1UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));

            encrypted1 = encryptor.Encrypt(encoder.Encode(123));
            product    = evaluator.Square(encrypted1);
            Assert.AreEqual(15129UL, encoder.DecodeUInt64(decryptor.Decrypt(product)));
        }
Ejemplo n.º 15
0
        protected override string GenerateKey()
        {
            if (string.IsNullOrWhiteSpace(Username))
            {
                throw new EntityKeyGenerationException(GetType(), "Username is empty");
            }

            return(KeyGenerator.Generate(Username));
        }
Ejemplo n.º 16
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Check basic validation first and bail out early before anything gets updated.
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (Uri.CheckHostName(Setup.SiteHostname) != UriHostNameType.Dns)
            {
                ModelState.AddModelError("Setup.Domain", "Invalid domain name");
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _httpServerOptions.Update(x =>
            {
                x.SiteHostname = Setup.SiteHostname;
                x.HttpsPort    = Setup.HttpsPort;
            });

            var acmeAccount = await _dataContext.AcmeAccounts.FirstAsync(x => x.IsAcmeStaging == Setup.UseAcmeStaging);

            var acmeCert = await _dataContext.AcmeCertificates
                           .Include(x => x.AcmeAccount)
                           .ThenInclude(x => x.Key)
                           .FirstOrDefaultAsync(x => x.Subject == Setup.SiteHostname &&
                                                x.AcmeAccountId == acmeAccount.AcmeAccountId);

            if (acmeCert == null)
            {
                var certKey = await _dataContext.Keys.FirstOrDefaultAsync(x => x.Name == Setup.SiteHostname);

                if (certKey == null)
                {
                    certKey = _keyGenerator.Generate(Setup.SiteHostname, Certes.KeyAlgorithm.RS256,
                                                     "certera certificate (this site)");
                }

                acmeCert = new Data.Models.AcmeCertificate
                {
                    ChallengeType = "http-01",
                    DateCreated   = DateTime.UtcNow,
                    Name          = Setup.SiteHostname,
                    Subject       = Setup.SiteHostname,
                    AcmeAccountId = acmeAccount.AcmeAccountId,
                    KeyId         = certKey.KeyId
                };
                _dataContext.AcmeCertificates.Add(acmeCert);
                await _dataContext.SaveChangesAsync();
            }

            return(RedirectToPage("./Certificate"));
        }
        public void EncryptNegateDecryptNET()
        {
            var parms = new EncryptionParameters
            {
                DecompositionBitCount  = 4,
                NoiseStandardDeviation = 3.19,
                NoiseMaxDeviation      = 35.06
            };
            var coeffModulus = parms.CoeffModulus;

            coeffModulus.Resize(48);
            coeffModulus.Set("FFFFFFFFC001");
            var plainModulus = parms.PlainModulus;

            plainModulus.Resize(7);
            plainModulus.Set(1 << 6);
            var polyModulus = parms.PolyModulus;

            polyModulus.Resize(64, 1);
            polyModulus[0].Set(1);
            polyModulus[63].Set(1);

            var keygen = new KeyGenerator(parms);

            keygen.Generate();

            var Encoder = new BinaryEncoder(parms.PlainModulus);

            var encryptor = new Encryptor(parms, keygen.PublicKey);
            var evaluator = new Evaluator(parms, keygen.EvaluationKeys);
            var decryptor = new Decryptor(parms, keygen.SecretKey);

            var encrypted = encryptor.Encrypt(Encoder.Encode(0x12345678));
            var negated   = evaluator.Negate(encrypted);

            Assert.AreEqual(-0x12345678, Encoder.DecodeInt32(decryptor.Decrypt(negated)));

            encrypted = encryptor.Encrypt(Encoder.Encode(0));
            negated   = evaluator.Negate(encrypted);
            Assert.AreEqual(0, Encoder.DecodeInt32(decryptor.Decrypt(negated)));

            encrypted = encryptor.Encrypt(Encoder.Encode(1));
            negated   = evaluator.Negate(encrypted);
            Assert.AreEqual(-1, Encoder.DecodeInt32(decryptor.Decrypt(negated)));

            encrypted = encryptor.Encrypt(Encoder.Encode(-1));
            negated   = evaluator.Negate(encrypted);
            Assert.AreEqual(1, Encoder.DecodeInt32(decryptor.Decrypt(negated)));

            encrypted = encryptor.Encrypt(Encoder.Encode(2));
            negated   = evaluator.Negate(encrypted);
            Assert.AreEqual(-2, Encoder.DecodeInt32(decryptor.Decrypt(negated)));

            encrypted = encryptor.Encrypt(Encoder.Encode(-5));
            negated   = evaluator.Negate(encrypted);
            Assert.AreEqual(5, Encoder.DecodeInt32(decryptor.Decrypt(negated)));
        }
Ejemplo n.º 18
0
        public override bool OneDataProcess()
        {
            var key   = _keyGenerator.Generate();
            var timer = _metric.StartMeasure();

            _metric.AddResult(_adapter.Read(key));
            _metric.StopMeasure(timer);

            return(true);
        }
Ejemplo n.º 19
0
 static void Main(string[] args)
 {
     if (IsValid(args))
     {
         var length            = int.Parse(args[0]);
         var passwordGenerator = new KeyGenerator(length);
         passwordGenerator.Generate();
         TextCopy.Clipboard.SetText(passwordGenerator.Password);
     }
 }
Ejemplo n.º 20
0
        protected override string GenerateKey()
        {
            if (string.IsNullOrWhiteSpace(Name))
            {
                // TODO: Localize
                throw new EntityKeyGenerationException(GetType(), "Name is empty");
            }

            return(KeyGenerator.Generate(Name));
        }
        public void GenerateTest(int length)
        {
            //Setup
            sut = new KeyGenerator(length);

            //Act
            sut.Generate();

            //Assert
            Assert.AreEqual(length, sut.Password.Length);
        }
Ejemplo n.º 22
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            bool deleteOnError = false;

            if (AcmeAccount.KeyId < 0)
            {
                var stg     = AcmeAccount.IsAcmeStaging ? "-staging" : string.Empty;
                var keyName = $"acme-account{stg}";

                var key = _keyGenerator.Generate(keyName, KeyAlgorithm.ES256);
                if (key == null)
                {
                    ModelState.AddModelError(string.Empty, "Error creating key");
                    return(Page());
                }

                deleteOnError   = true;
                AcmeAccount.Key = key;
            }

            AcmeAccount.ApplicationUser = await _userManager.GetUserAsync(User);

            try
            {
                _context.AcmeAccounts.Add(AcmeAccount);
                await _context.SaveChangesAsync();

                // Create account with key
                await _certesAcmeProvider.CreateAccount(AcmeAccount.AcmeContactEmail,
                                                        AcmeAccount.Key.RawData, AcmeAccount.IsAcmeStaging);
            }
            catch (Exception e)
            {
                // Delete created key if there's a failure in creating the acme account
                if (deleteOnError)
                {
                    _context.Keys.Remove(AcmeAccount.Key);
                    await _context.SaveChangesAsync();
                }

                _logger.LogError(e, "Error creating ACME account");
                ModelState.AddModelError(string.Empty, "Error creating ACME account");
                return(Page());
            }

            StatusMessage = "Account created";

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 23
0
        private void AddToCache(long lowerIndex, long upperIndex, int payLoad)
        {
            var keyGenerator = new KeyGenerator();

            for (long i = lowerIndex; i < upperIndex; i++)
            {
                var key = keyGenerator.Generate(i);

                _cache.Update(key, GetPayload(payLoad));
            }
        }
Ejemplo n.º 24
0
    private SymmetricAlgorithm Algorithm(string salt)
    {
        using var key = KeyGenerator.Generate(_key, salt);

        var algorithm = Aes.Create();

        algorithm.Key = key.GetBytes(algorithm.KeySize / 8);

        algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);

        return(algorithm);
    }
Ejemplo n.º 25
0
        public void KeyGenerator_GeneratesOneHundredUniqueKeys_WithValidFormat()
        {
            var hexRegex = new Regex(@"^[0-9a-z\-]*$", RegexOptions.Compiled);
            var set      = new HashSet <string>();

            for (int i = 0; i < 100; i++)
            {
                var key = KeyGenerator.Generate();
                Assert.IsTrue(hexRegex.IsMatch(key));
                Assert.IsTrue(set.Add(key));
            }
        }
Ejemplo n.º 26
0
        public void FVEncryptAddsNoiseNET()
        {
            var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

            parms.SetDecompositionBitCount(4);
            parms.SetNoiseStandardDeviation(3.19);
            parms.SetNoiseMaxDeviation(35.06);

            var coeffModulus = new BigUInt(48);

            coeffModulus.Set("FFFFFFFFC001");
            parms.SetCoeffModulus(coeffModulus);

            var plainModulus = new BigUInt(7);

            plainModulus.Set(1 << 6);
            parms.SetPlainModulus(plainModulus);

            var polyModulus = new BigPoly(65, 1);

            polyModulus[0].Set(1);
            polyModulus[64].Set(1);
            parms.SetPolyModulus(polyModulus);

            parms.Validate();

            var Encoder = new BinaryEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew());

            var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew());

            keygen.Generate();

            var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew());

            // however, this line is fine
            Assert.AreEqual(encryptor.PublicKey[0], keygen.PublicKey[0]);
            Assert.AreEqual(encryptor.PublicKey[1], keygen.PublicKey[1]);

            var encrypted1 = encryptor.Encrypt(Encoder.Encode(0x12345678));
            var encrypted2 = encryptor.Encrypt(Encoder.Encode(0x12345678));

            // this is what we want to check
            Assert.AreNotEqual(encrypted1[0], encrypted2[0]);
            Assert.AreNotEqual(encrypted1[1], encrypted2[1]);


            var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew());

            Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted1)));
            Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted2)));
        }
Ejemplo n.º 27
0
        public void Generate_MultipleCalls_DontReturnSameResult()
        {
            using var target = new KeyGenerator();

            const int expectedCount = 100;
            var       set           = new HashSet <string>();

            for (var i = 0; i < expectedCount; i++)
            {
                set.Add(target.Generate());
            }

            Assert.Equal(expectedCount, set.Count);
        }
Ejemplo n.º 28
0
        private async Task <Key> CreateKeyIfNotExists(ApplicationUser user, string keyContents, bool staging)
        {
            var stg     = staging ? "-staging" : string.Empty;
            var keyName = $"user-{user.Id}-acme-account{stg}";
            var key     = await _dataContext.Keys.FirstOrDefaultAsync(x => x.Name == keyName);

            if (key == null)
            {
                var desc = $"Let's Encrypt {(staging ? "Staging " : string.Empty)}Account Key";
                key = _keyGenerator.Generate(keyName, KeyAlgorithm.ES256, desc, keyContents);
            }

            return(key);
        }
Ejemplo n.º 29
0
        public void FVEncryptExponentiateDecryptNET()
        {
            var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew());

            parms.SetDecompositionBitCount(4);
            parms.SetNoiseStandardDeviation(3.19);
            parms.SetNoiseMaxDeviation(35.06);

            var coeffModulus = new BigUInt(48);

            coeffModulus.Set("FFFFFFFFC001");
            parms.SetCoeffModulus(coeffModulus);

            var plainModulus = new BigUInt(7);

            plainModulus.Set(1 << 6);
            parms.SetPlainModulus(plainModulus);

            var polyModulus = new BigPoly(65, 1);

            polyModulus[0].Set(1);
            polyModulus[64].Set(1);
            parms.SetPolyModulus(polyModulus);

            parms.Validate();

            var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew());

            keygen.Generate(1);

            var encoder = new BinaryEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew());

            var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew());
            var evaluator = new Evaluator(parms, keygen.EvaluationKeys, MemoryPoolHandle.AcquireNew());
            var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew());

            var encrypted = encryptor.Encrypt(encoder.Encode(5));
            var power     = evaluator.Exponentiate(encrypted, 1);

            Assert.AreEqual(5, encoder.DecodeInt32(decryptor.Decrypt(power)));

            encrypted = encryptor.Encrypt(encoder.Encode(7));
            power     = evaluator.Exponentiate(encrypted, 2);
            Assert.AreEqual(49, encoder.DecodeInt32(decryptor.Decrypt(power)));

            encrypted = encryptor.Encrypt(encoder.Encode(-7));
            power     = evaluator.Exponentiate(encrypted, 3);
            Assert.AreEqual(-343, encoder.DecodeInt32(decryptor.Decrypt(power)));
        }
Ejemplo n.º 30
0
        static void Main(string[] args)
        {
            while (true)
            {
                string key = KeyGenerator.Generate();

                Console.WriteLine(key);
                Console.WriteLine(KeyValidator.IsValid(key));

                if (Console.ReadKey().Key == ConsoleKey.Escape)
                {
                    return;
                }
            }
        }