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()); }
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); }
static async Task SendPrime() { Console.WriteLine("Enter the the number u want to compute: "); var newPrime = Convert.ToInt64(Console.ReadLine()); if (newPrime < 0) { Console.WriteLine("The number u want to check must be > 0."); return; } var PrimeString = newPrime.ToString(); var metricsRequest = new PrimeItem { Prime = PrimeString }; await FitnessTrackerClient.AddNewPrime(metricsRequest); }
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; } } }
private static async Task GetAnswer() { var answer = await FitnessTrackerClient.GetAnswer(); PrintAnswer(answer); }