private static long ProfileEncryptedADD(int iterations, int keyl) { // clean up GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); var rnd = new Random(); // prepare and warm up Paillier algorithm = new PaillierManaged(); algorithm.KeySize = keyl; algorithm.Padding = PaillierPaddingMode.LeadingZeros; //string parametersXML = algorithm.ToXmlString(true); Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); var a = new BigInteger(rnd.Next(65536)); var a_bytes = encryptAlgorithm.EncryptData(a.getBytes()); var b = new BigInteger(rnd.Next(65536)); var b_bytes = encryptAlgorithm.EncryptData(b.getBytes()); var c_bytes = encryptAlgorithm.Addition(a_bytes, b_bytes); var watch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { c_bytes = encryptAlgorithm.Addition(a_bytes, b_bytes); } watch.Stop(); return watch.Elapsed.Ticks; }
public static bool TestAddition(int keySize) { Paillier algorithm = new PaillierManaged(); algorithm.KeySize = keySize; algorithm.Padding = PaillierPaddingMode.LeadingZeros; string parametersXML = algorithm.ToXmlString(true); Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var random = new Random(); var A = new BigInteger(random.Next(32768)); var B = new BigInteger(random.Next(32768)); var A_bytes = A.getBytes(); var B_bytes = B.getBytes(); //encrypt A and B var A_enc_bytes = encryptAlgorithm.EncryptData(A_bytes); var B_enc_bytes = encryptAlgorithm.EncryptData(B_bytes); // decrypt A and B var A_dec_bytes = decryptAlgorithm.DecryptData(A_enc_bytes); var B_dec_bytes = decryptAlgorithm.DecryptData(B_enc_bytes); // getting homomorphic addition result var C_enc_bytes = encryptAlgorithm.Addition(A_enc_bytes, B_enc_bytes); var C_dec_bytes = decryptAlgorithm.DecryptData(C_enc_bytes); // convert to BigInteger var A_dec = new BigInteger(A_dec_bytes); var B_dec = new BigInteger(B_dec_bytes); var C_dec = new BigInteger(C_dec_bytes); if (C_dec != A + B) { return(false); } return(true); }
public static void Rerun_SameNumbers(BigInteger A, BigInteger B) { Paillier algorithm = new PaillierManaged(); algorithm.KeySize = 384; algorithm.Padding = PaillierPaddingMode.LeadingZeros; string parametersXML = algorithm.ToXmlString(true); //Console.WriteLine("\n{0}\n", PrettifyXML(parametersXML)); Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); byte[] A_bytes = A.getBytes(); byte[] B_bytes = B.getBytes(); //encrypt A and B byte[] A_enc_bytes = encryptAlgorithm.EncryptData(A.getBytes()); byte[] B_enc_bytes = encryptAlgorithm.EncryptData(B.getBytes()); // decrypt A and B byte[] A_dec_bytes = decryptAlgorithm.DecryptData(A_enc_bytes); byte[] B_dec_bytes = decryptAlgorithm.DecryptData(B_enc_bytes); //getting homomorphic addition result byte[] C_enc_bytes = encryptAlgorithm.Addition(A_enc_bytes, B_enc_bytes); byte[] C_dec_bytes = decryptAlgorithm.DecryptData(C_enc_bytes); // convert to BigInteger BigInteger A_dec = new BigInteger(A_dec_bytes); BigInteger B_dec = new BigInteger(B_dec_bytes); BigInteger C_dec = new BigInteger(C_dec_bytes); // printing out Console.WriteLine("Plaintext: {0} + {1} = {2}", A.ToString(), B.ToString(), (A + B).ToString()); Console.WriteLine("Encrypted: {0} + {1} = {2}", A_dec.ToString(), B_dec.ToString(), C_dec.ToString()); }
public static Boolean TestAddition() { Paillier algorithm = new PaillierManaged(); algorithm.KeySize = 384; algorithm.Padding = PaillierPaddingMode.LeadingZeros; string parametersXML = algorithm.ToXmlString(true); //Console.WriteLine("\n{0}\n", PrettifyXML(parametersXML)); Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); Random random = new Random(); BigInteger A = new BigInteger(random.Next(32768)); BigInteger B = new BigInteger(random.Next(32768)); byte[] A_bytes = A.getBytes(); byte[] B_bytes = B.getBytes(); //encrypt A and B byte[] A_enc_bytes = encryptAlgorithm.EncryptData(A.getBytes()); byte[] B_enc_bytes = encryptAlgorithm.EncryptData(B.getBytes()); // decrypt A and B byte[] A_dec_bytes = decryptAlgorithm.DecryptData(A_enc_bytes); byte[] B_dec_bytes = decryptAlgorithm.DecryptData(B_enc_bytes); // getting homomorphic addition result byte[] C_enc_bytes = encryptAlgorithm.Addition(A_enc_bytes, B_enc_bytes); byte[] C_dec_bytes = decryptAlgorithm.DecryptData(C_enc_bytes); // strip off trailing zeros //byte[] A_dec_stripped = StripTrailingZeros(A_dec_bytes, A_bytes.Length); //byte[] B_dec_stripped = StripTrailingZeros(B_dec_bytes, B_bytes.Length); //byte[] C_dec_stripped = StripTrailingZeros(C_dec_bytes); // convert to BigInteger BigInteger A_dec = new BigInteger(A_dec_bytes); BigInteger B_dec = new BigInteger(B_dec_bytes); BigInteger C_dec = new BigInteger(C_dec_bytes); if (C_dec != A + B) { Console.WriteLine(); Console.WriteLine("***********Error Encountered!!***"); Console.WriteLine("\n{0}\n", PrettifyXML(parametersXML)); // printing out Console.WriteLine("Plaintext: {0} + {1} = {2}", A.ToString(), B.ToString(), (A + B).ToString()); Console.WriteLine("Encrypted: {0} + {1} = {2}", A_dec.ToString(), B_dec.ToString(), C_dec.ToString()); Console.WriteLine(); Console.WriteLine("Re-run the numbers with different key.."); Rerun_SameNumbers(A, B); Console.WriteLine(); Console.WriteLine("Re-run the same key with different numbers.."); Rerun_SameKey(encryptAlgorithm, decryptAlgorithm); Console.WriteLine(); Console.WriteLine("Re-run with same key and same numbers.."); Rerun_SamekeyNumber(encryptAlgorithm, decryptAlgorithm, A, B); Console.WriteLine(); return(false); } return(true); }