public static int[] DecryptVote(this Vote vote, PrivateKey key) { if (vote == null || key == null || vote.EncryptedVector == null) { throw new Exception("Can't decrypt vote. Null key or vote"); } return(vote.EncryptedVector.Select(arg => HomoCrypto.Decrypt(arg, key)).ToArray()); }
private static void Vote(string host, KeyValuePair <User, int[]>[] voters, Guid id, PublicKey publicKey) { log.Info("Voting in parallel..."); var candidateTasks = voters.Select(kvp => ElectroClient.VoteAsync(host, Program.PORT, kvp.Key.Cookies, id, HomoCrypto.EncryptVector(kvp.Value, publicKey))).ToArray(); try { Task.WaitAll(); log.InfoFormat("Voted by {0} users", voters.Length); } catch (Exception e) { throw new ServiceException(ExitCode.DOWN, string.Format("Failed to vote by {0} users in parallel: {1}", candidateTasks.Length, e)); } }
public static int ProcessPut(string host, string id, string flag) { log.Info("Processing Vuln2.Put"); var candidateUsers = RegisterCandidates(host, flag.Distinct().Select(c => UsersManager.GenUser(null, c.ToString())).OrderBy(user => user.Login).ToArray()); var election = StartElection(host, candidateUsers[0], false, nominateTimeInSec, voteTimeInSec); var privateKey = election.PrivateKeyForCandidates; var sw = Stopwatch.StartNew(); election = NominateUsers(host, election, candidateUsers.Skip(1).ToArray()); sw.Stop(); var tts = nominateTimeInSec * 1000 - sw.ElapsedMilliseconds; if (tts > 0) { log.InfoFormat("Sleeping for {0}ms before voting (nomination duration is {1}s)", tts, nominateTimeInSec); Thread.Sleep((int)tts); } var votes = GenVotes(flag, election); var voters = RegisterVoters(host, votes, candidateUsers); log.InfoFormat("Voting by {0} voters", voters.Length); foreach (var voter in voters) { ElectroClient.Vote(host, Program.PORT, voter.Key.Cookies, election.Id, HomoCrypto.EncryptVector(voter.Value, election.PublicKey)); } log.Info("Voted"); var state = new Vuln2State { ElectionId = election.Id.ToString(), Voter = voters[0].Key, PrivateKey = privateKey }; log.Info("Flag put"); Console.Out.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(state.ToJsonString()))); return((int)ExitCode.OK); }
public bool TryDecryptElectionResultIfFinished(Election election) { if (!election.IsFinished) { return(false); } PrivateKey privateKey; if (!electionPrivateKeys.TryGetValue(election.Id, out privateKey)) { return(false); } election.DecryptedResult = (election.EncryptedResult ?? Enumerable.Repeat(BigInteger.Zero, election.Candidates.Count)).Select(voteElement => HomoCrypto.Decrypt(voteElement, privateKey)).ToArray(); return(true); }