public static int Decrypt(BigInteger val, PrivateKey privateKey) { var m = val % privateKey.Key; return (int) (m % privateKey.MaxNum); }
public static int[] DecryptVector(BigInteger[] vector, PrivateKey privateKey) { return vector.Select(bi => Decrypt(bi, privateKey)).ToArray(); }
private static BigInteger Decrypt(BigInteger c, PrivateKey privateKey) { var m = c % privateKey.Key; return m % privateKey.MaxNum; }
public static PublicKey GenPublicKey(PrivateKey privateKey, int bitsCount = DefaultBitsCount) { var buff = new BigInteger[DefaultSetSize]; byte[] rand = new byte[bitsCount / 8]; for(int i = 0; i < buff.Length; i++) { Singleton.Random.NextBytes(rand); buff[i] = (BigInteger.Abs(new BigInteger(rand)) * privateKey.Key) + (privateKey.MaxNum * Singleton.Random.Next(10, 100)); } return new PublicKey { KeyParts = buff, MaxNum = privateKey.MaxNum}; }
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(); }
public void SaveKey(Guid electionId, PrivateKey privateKey) { if(keysWriter == null) { lock(keysFilePath) { if(keysWriter == null) keysWriter = new StreamWriter(new FileStream(keysFilePath, FileMode.Append)) { AutoFlush = true }; } } keysWriter.WriteLine(new KeyValuePair<Guid, PrivateKey>(electionId, privateKey).ToJsonString()); }
private static string ExtractFlag(Election election, PrivateKey privateKey, int flagLen) { if(election.Votes == null) throw new ServiceException(ExitCode.CORRUPT, "Election has no votes"); if(election.Candidates == null) throw new ServiceException(ExitCode.CORRUPT, "Election has no candidates"); return string.Join("", election.Votes.Take(flagLen).Select(vote => { var decryptedVote = vote.DecryptVote(privateKey); int candidateNum = decryptedVote.IndexOf(i => i > 0); if(candidateNum < 0 || candidateNum >= election.Candidates.Count) throw new ServiceException(ExitCode.CORRUPT, string.Format("Election has no candidate corresponding to vector {0}", string.Join(",", decryptedVote))); return election.Candidates[candidateNum].PublicMessage; })); }