Example #1
0
        private static async Task GetMetrics()
        {
            // Get encrypted metrics
            var metrics = await FitnessTrackerClient.GetMetrics();

            LogUtils.SummaryStatisticInfo("CLIENT", "GetMetrics", metrics);

            // Decrypt the data
            var ciphertextTotalRuns = SEALUtils.BuildCiphertextFromBase64String(metrics.TotalRuns, _context);
            var plaintextTotalRuns  = new Plaintext();

            _decryptor.Decrypt(ciphertextTotalRuns, plaintextTotalRuns);

            var ciphertextTotalDistance = SEALUtils.BuildCiphertextFromBase64String(metrics.TotalDistance, _context);
            var plaintextTotalDistance  = new Plaintext();

            _decryptor.Decrypt(ciphertextTotalDistance, plaintextTotalDistance);

            var ciphertextTotalHours = SEALUtils.BuildCiphertextFromBase64String(metrics.TotalHours, _context);
            var plaintextTotalHours  = new Plaintext();

            _decryptor.Decrypt(ciphertextTotalHours, plaintextTotalHours);


            // Print metrics in console
            PrintMetrics(plaintextTotalRuns.ToString(), plaintextTotalDistance.ToString(), plaintextTotalHours.ToString());
        }
Example #2
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);
        }
Example #3
0
 private static void PrintMetrics(SummaryItem summary)
 {
     Console.WriteLine(string.Empty);
     Console.WriteLine("********* Metrics *********");
     Console.WriteLine($"Total runs: {SEALUtils.Base64Decode(summary.TotalRuns)}");
     Console.WriteLine($"Total distance: {SEALUtils.Base64Decode(summary.TotalDistance)}");
     Console.WriteLine($"Total hours: {SEALUtils.Base64Decode(summary.TotalHours)}");
     Console.WriteLine(string.Empty);
 }
 public ActionResult <KeysModel> GetKeys()
 {
     Debug.WriteLine("[API]: GetKeys - return SEAL public and secret keys to client");
     return(new KeysModel
     {
         PublicKey = SEALUtils.PublicKeyToBase64String(_keyGenerator.PublicKey),
         SecretKey = SEALUtils.SecretKeyToBase64String(_keyGenerator.SecretKey)
     });
 }
        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());
            }
        }
Example #6
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());
        }
        public MetricsController()
        {
            // Initialize context
            // Getting context from Commons project
            _sealContext = SEALUtils.GetContext();


            // Initialize key generator and encryptor
            // Initialize key Generator that will be use to get the Public and Secret keys
            _keyGenerator = new KeyGenerator(_sealContext);
            // Initializing encryptor
            _encryptor = new Encryptor(_sealContext, _keyGenerator.PublicKey);

            // Initialize evaluator
            _evaluator = new Evaluator(_sealContext);
        }
        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);
        }
Example #9
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("SEAL LAB");
            Console.WriteLine("Setting up encryption...\n");

            // Add Initialization code here
            _context = SEALUtils.GetContext();

            // Add keys code here
            var keys = await FitnessTrackerClient.GetKeys();

            // Create encryptor

            var publicKey = SEALUtils.BuildPublicKeyFromBase64String(keys.PublicKey, _context);

            _encryptor = new Encryptor(_context, publicKey);


            // Create decryptor
            var secretKey = SEALUtils.BuildSecretKeyFromBase64String(keys.SecretKey, _context);

            _decryptor = new Decryptor(_context, secretKey);


            while (true)
            {
                PrintMenu();
                var option = Convert.ToInt32(Console.ReadLine());

                switch (option)
                {
                case 1:
                    await SendNewRun();

                    break;

                case 2:
                    await GetMetrics();

                    break;
                }
            }
        }
Example #10
0
 public PrimeController()
 {
     _sealContext  = SEALUtils.GetContext();
     _keyGenerator = new KeyGenerator(_sealContext);
     _encryptor    = new Encryptor(_sealContext, _keyGenerator.PublicKey);
 }