Ejemplo n.º 1
0
        static async Task SendNewRun()
        {
            // Get distance from user
            Console.Write("Enter the new running distance (km): ");
            var newRunningDistance = Convert.ToInt32(Console.ReadLine());

            if (newRunningDistance < 0)
            {
                Console.WriteLine("Running distance must be greater than 0.");
                return;
            }

            // Encrypt distance
            // We will convert the Int value to Hexadecimal using the ToString("X") method
            var plaintext          = new Plaintext($"{newRunningDistance.ToString("X")}");
            var ciphertextDistance = new Ciphertext();

            _encryptor.Encrypt(plaintext, ciphertextDistance);


            // Convert value to base64 string
            var base64Distance = SEALUtils.CiphertextToBase64String(ciphertextDistance);


            // Get time from user
            Console.Write("Enter the new running time (hours): ");
            var newRunningTime = Convert.ToInt32(Console.ReadLine());

            if (newRunningTime < 0)
            {
                Console.WriteLine("Running time must be greater than 0.");
                return;
            }

            // Encrypt time
            // We will convert the Int value to Hexadecimal using the ToString("X") method
            var plaintextTime  = new Plaintext($"{newRunningTime.ToString("X")}");
            var ciphertextTime = new Ciphertext();

            _encryptor.Encrypt(plaintextTime, ciphertextTime);


            // Convert value to base64 string
            var base64Time = SEALUtils.CiphertextToBase64String(ciphertextTime);


            var metricsRequest = new RunItem
            {
                Distance = base64Distance,
                Time     = base64Time
            };

            LogUtils.RunItemInfo("CLIENT", "SendNewRun", metricsRequest);

            // Send new run to api
            await FitnessTrackerClient.AddNewRunningDistance(metricsRequest);
        }
        private static void PrintAnswer(EncAnswerItem answer)
        {
            Console.WriteLine(string.Empty);
            Console.WriteLine("********* Factors *********");
            //Console.WriteLine($"Factor1:{answer.Factor1}");
            //Console.WriteLine($"Factor2:{answer.Factor2}");
            Console.WriteLine(string.Empty);

            var context           = SEALUtils.GetContext();
            var ciphertextPrime   = SEALUtils.BuildCiphertextFromBase64String(answer.Prime, context);
            var ciphertextFactor1 = SEALUtils.BuildCiphertextFromBase64String(answer.Factor1, context);
            var ciphertextFactor2 = SEALUtils.BuildCiphertextFromBase64String(answer.Factor2, context);
            var publicKey         = SEALUtils.BuildPublicKeyFromBase64String(answer.PublicKey, context);
            var secretKey         = SEALUtils.BuildSecretKeyFromBase64String(answer.SecretKey, context);

            Ciphertext temp       = new Ciphertext();
            Evaluator  _evaluator = new Evaluator(context);
            Encryptor  encryptor  = new Encryptor(context, publicKey);

            _evaluator.Multiply(ciphertextFactor1, ciphertextFactor2, temp);
            var tempstring = SEALUtils.CiphertextToBase64String(temp);

            if (tempstring.Equals(answer.Prime))
            {
                Console.WriteLine("the answer is right!");
            }
            else
            {
                var plain = new Plaintext();
                Console.WriteLine("the answer is wrong");
                Decryptor _decryptor = new Decryptor(context, secretKey);
                _decryptor.Decrypt(ciphertextPrime, plain);
                PrintAnswer(plain.ToString());
                encryptor.Encrypt(plain, temp);
                if (!SEALUtils.CiphertextToBase64String(temp).Equals(SEALUtils.CiphertextToBase64String(ciphertextPrime)))
                {
                    Console.WriteLine(SEALUtils.CiphertextToBase64String(ciphertextFactor2).Substring(0, 100));
                    Console.WriteLine(SEALUtils.CiphertextToBase64String(ciphertextFactor1).Substring(0, 100));
                }
                Console.WriteLine(_decryptor.InvariantNoiseBudget(temp));
                _decryptor.Decrypt(temp, plain);
                PrintAnswer(plain.ToString());
                //_decryptor.Decrypt(ciphertextFactor1, plain);
                //PrintAnswer(plain.ToString());
                //_decryptor.Decrypt(ciphertextFactor2, plain);
                //PrintAnswer(plain.ToString());
            }
        }
Ejemplo n.º 3
0
        public ActionResult ComputePrime([FromBody] PrimeItem request)
        {
            var prime = request.Prime;

            _prime = Convert.ToInt64(prime);
            for (long i = 2; i < _prime / 2; i++)
            {
                if (_prime % i == 0)
                {
                    _answer[0] = i;
                    _answer[1] = _prime / i;
                    break;
                }
            }

            // TODO 此处添加对_answer和 _prime加密的功能
            var plainprime = new Plaintext($"{_prime.ToString("X")}");

            Console.WriteLine(_prime);
            var ciphertextprime = new Ciphertext();

            _encryptor.Encrypt(plainprime, ciphertextprime);
            var base64Prime = SEALUtils.CiphertextToBase64String(ciphertextprime);

            var plainfactor1      = new Plaintext($"{_answer[0].ToString("X")}");
            var ciphertextfactor1 = new Ciphertext();

            _encryptor.Encrypt(plainfactor1, ciphertextfactor1);
            var base64Factor1 = SEALUtils.CiphertextToBase64String(ciphertextfactor1);

            var plainfactor2      = new Plaintext($"{_answer[1].ToString("X")}");
            var ciphertextfactor2 = new Ciphertext();

            _encryptor.Encrypt(plainfactor2, ciphertextfactor2);
            var base64Factor2 = SEALUtils.CiphertextToBase64String(ciphertextfactor2);

            var publicKey = SEALUtils.PublicKeyToBase64String(_keyGenerator.PublicKey);
            var secretKey = SEALUtils.SecretKeyToBase64String(_keyGenerator.SecretKey);

            _base64Prime   = base64Prime;
            _base64Factor1 = base64Factor1;
            _base64Factor2 = base64Factor2;
            _publicKey     = publicKey;
            _secretKey     = secretKey;


            return(Ok());
        }
Ejemplo n.º 4
0
        public ActionResult <SummaryItem> GetMetrics()
        {
            Ciphertext totalDistance = new Ciphertext();
            int        zero          = 0;
            Plaintext  plainTextZero = new Plaintext($"{zero.ToString("X")}");

            _encryptor.Encrypt(plainTextZero, totalDistance);



            foreach (var dString in _distances)
            {
                var cipherString = SEALUtils.BuildCiphertextFromBase64String(dString, _sealContext);
                _evaluator.Add(totalDistance, cipherString, totalDistance);
            }

            Ciphertext totalHours = new Ciphertext();

            _encryptor.Encrypt(plainTextZero, totalHours);

            foreach (var timeString in _times)
            {
                var cipherTimeString = SEALUtils.BuildCiphertextFromBase64String(timeString, _sealContext);
                _evaluator.Add(totalHours, cipherTimeString, totalHours);
            }

            Ciphertext totalRuns          = new Ciphertext();
            Plaintext  plainTextTotalRuns = new Plaintext($"{_distances.Count.ToString("X")}");

            _encryptor.Encrypt(plainTextTotalRuns, totalRuns);


            var summaryItem = new SummaryItem
            {
                TotalRuns     = SEALUtils.CiphertextToBase64String(totalRuns),
                TotalDistance = SEALUtils.CiphertextToBase64String(totalDistance),
                TotalHours    = SEALUtils.CiphertextToBase64String(totalHours)
            };

            LogUtils.SummaryStatisticInfo("API", "GetMetrics", summaryItem);
            LogUtils.SummaryStatisticInfo("API", "GetMetrics", summaryItem, true);

            return(summaryItem);
        }