コード例 #1
0
ファイル: Program.cs プロジェクト: relusion/HeVotingSystem
        static async Task Main(string[] args)
        {
            Console.WriteLine("Connecting to the vote system...");
            var responseMessage = await client.GetAsync("https://localhost:44319/api/vote");

            Console.WriteLine("Encrypted votes received...");
            var stringResponse = await responseMessage.Content.ReadAsStringAsync();

            List <EncryptedVote> encryptedVotes = JsonConvert.DeserializeObject <List <EncryptedVote> >(stringResponse);

            SealService          sealService = new SealService();
            List <List <ulong> > valuesList  = new List <List <ulong> >();
            var items = encryptedVotes.Select(encryptedVote => SealUtils.BuildCiphertextFromBytes(encryptedVote.Data, sealService.SealContext)).ToArray();

            Console.WriteLine("Create Seal evaluator... no private key needed...");
            using var evaluator = sealService.CreateEvaluator();
            Ciphertext results = new Ciphertext();

            Console.WriteLine("Executing add function on all encrypted votes...");
            evaluator.AddMany(items, results);
            Console.WriteLine("Finished...");
            var resultsArray = SealUtils.CiphertextToArray(results);

            Console.WriteLine("Encrypted results  serialized");
            var jsonPayload = JsonConvert.SerializeObject(new
            {
                Content = resultsArray,
            });

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            await client.PostAsync("https://localhost:44319/api/vote/results", new StringContent(jsonPayload, Encoding.UTF8, "application/json"));

            Console.WriteLine("Encrypted results uploaded to the voting system");
        }
コード例 #2
0
        public IActionResult PostResults([FromBody] VoteCalcModel content)
        {
            _computationResults = SealUtils.BuildCiphertextFromBytes(content.Content, _sealService.SealContext);
            using var decryptor = _sealService.CreateDecryptor();
            Plaintext plaintext = new Plaintext();

            decryptor.Decrypt(_computationResults, plaintext);
            using var batchEncoder = _sealService.CreateBatchEncoder();
            List <ulong> decodedValues = new List <ulong>();

            batchEncoder.Decode(plaintext, decodedValues);
            decodedValues.RemoveRange(2, decodedValues.Count - 2);
            CalculatedVotes[0] = decodedValues[0];
            CalculatedVotes[1] = decodedValues[1];
            return(Ok());
        }