Exemplo n.º 1
0
 public static int Decrypt(BigInteger val, PrivateKey privateKey)
 {
     var m = val % privateKey.Key;
     return (int) (m % privateKey.MaxNum);
 }
Exemplo n.º 2
0
 public static int[] DecryptVector(BigInteger[] vector, PrivateKey privateKey)
 {
     return vector.Select(bi => Decrypt(bi, privateKey)).ToArray();
 }
Exemplo n.º 3
0
 private static BigInteger Decrypt(BigInteger c, PrivateKey privateKey)
 {
     var m = c % privateKey.Key;
     return m % privateKey.MaxNum;
 }
Exemplo n.º 4
0
        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};
        }
Exemplo n.º 5
0
 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();
 }
Exemplo n.º 6
0
        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());
        }
Exemplo n.º 7
0
        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;
            }));
        }