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()); }
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()); } }
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); }