Пример #1
0
        public void DecodeTest()
        {
            VoteDecoder decoder = new VoteDecoder("db77d62fc88726f1c5a6b79b003b3bca83349e334337de84bd17344ab601db74".ParseHexEncodedValue());
            // Голос номер 778136
            //"{""message"":""7a2baaff59b5f862cff20fc059af42d9110bb5f6"",""nonce"":""6fe1a5f2052c79252a01bcb1f1019c7a92fd8e45c9504171"",""public_key"":""69e7b55df3370efd381e5a6c5c86002f3194183c3559f48705401152b6304313""}";"2212294583";"#2697378";"17:24:58";"5328b24e0deef4c8e6e48ed5ddc486058c8f77931cafebfe6a8b04a048011f3e0000e90306000a4064613835666166383466616562306539306566333161326537323532343135316662616330653266366536633439383336303133643634353239343639383864104d1a560a147a2baaff59b5f862cff20fc059af42d9110bb5f6121a0a186fe1a5f2052c79252a01bcb1f1019c7a92fd8e45c95041711a220a2069e7b55df3370efd381e5a6c5c86002f3194183c3559f48705401152b630431382f14251ad28b9ff582e1dc2b4e06020a8473b3022836a1151d957bf89f978e35dd4d37819e72d269053fa1797b80feb5dcb8798197d22626d19917571b4af09"
            EncryptedVote evote = new EncryptedVote("7a2baaff59b5f862cff20fc059af42d9110bb5f6", "6fe1a5f2052c79252a01bcb1f1019c7a92fd8e45c9504171", "69e7b55df3370efd381e5a6c5c86002f3194183c3559f48705401152b6304313");

            Assert.IsTrue(decoder.DecodeEndVerify(evote, out byte[] decoded));
            //"778136";77;"0x2bddd3d05570be9d417bd0b28d54e7adcf1f3c19b7ff74ae011ba9f31b7da7b7";"#606026";"16:56:59";"{""message"":""7a2baaff59b5f862cff20fc059af42d9110bb5f6"",""nonce"":""6fe1a5f2052c79252a01bcb1f1019c7a92fd8e45c9504171"",""public_key"":""69e7b55df3370efd381e5a6c5c86002f3194183c3559f48705401152b6304313""}"
            Assert.AreEqual(2212294583, BitConverter.ToUInt32(decoded));
        }
Пример #2
0
        public IActionResult Post([FromForm] VoteModel content)
        {
            using var batchEncoder = _sealService.CreateBatchEncoder();
            using var encryptor    = _sealService.CreateEncryptor();

            //create a vector with data
            var voteMatrix = new ulong[2];

            voteMatrix[0] = content.IsBiden;
            voteMatrix[1] = content.IsTrump;

            using var plainMatrix = new Plaintext();
            //encode the vector
            batchEncoder.Encode(voteMatrix, plainMatrix);
            var voteMatrixCipher = new Ciphertext();

            //encrypt the vector with HE
            encryptor.Encrypt(plainMatrix, voteMatrixCipher);
            //serialize encrypted data
            var encryptedMatrixBytes = SealUtils.CiphertextToArray(voteMatrixCipher);

            var encryptedVote = new EncryptedVote
            {
                Data       = encryptedMatrixBytes,
                DateOfVote = DateTimeOffset.UtcNow,
                Voter      = content.DisplayName,
            };

            var dataToSign = JsonConvert.SerializeObject(encryptedVote, Formatting.None);
            var signature  = RCASignString(dataToSign);

            //generate a vote id and store the encrypted vote. In the real system that would be an azure storage account or file system.
            var voteId = Guid.NewGuid().ToString("N");

            _votes.TryAdd(voteId, encryptedVote);

            // return the encrypted vote to the user.
            return(Ok(new
            {
                EncryptedVote = encryptedVote,
                Signature = signature,
                voteId,
            }));
        }