public void TestTextEncryption() { // Test failed somehow. // UPDATE: This test unexpectedly passed. I guess it's due to the BigInteger package update. string message = "This is to test Paillier encryption and hopefully this message contains more than 2 blocks please please please please please please please please please please please pleaseplease please please pleaseplease please please please "; PaillierPaddingMode padding = PaillierPaddingMode.Zeros; var plaintext = Encoding.Default.GetBytes(message); Paillier algorithm = new PaillierManaged(); for (int keySize = 384; keySize <= 1088; keySize += 8) { algorithm.KeySize = keySize; algorithm.Padding = padding; Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); var ciphertext = encryptAlgorithm.EncryptData(plaintext); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var candidatePlaintext = decryptAlgorithm.DecryptData(ciphertext); var strip_zeros = StripTrailingZeros(candidatePlaintext, plaintext.Length); CollectionAssert.AreEqual(plaintext, strip_zeros); } }
public void TestZero() { Paillier algorithm = new PaillierManaged(); algorithm.Padding = PaillierPaddingMode.LeadingZeros; for (int keySize = 384; keySize <= 1088; keySize += 8) { algorithm.KeySize = keySize; Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var z = new BigInteger(0); var z_enc = encryptAlgorithm.EncryptData(z.getBytes()); var z_dec = decryptAlgorithm.DecryptData(z_enc); var zero_array = new byte[z_dec.Length]; Array.Clear(zero_array, 0, zero_array.Length - 1); CollectionAssert.AreEqual(zero_array, z_dec); } }
public void TestRandomBI() { // Failed test because of zeroes Paillier algorithm = new PaillierManaged(); algorithm.Padding = PaillierPaddingMode.LeadingZeros; for (algorithm.KeySize = 384; algorithm.KeySize <= 1088; algorithm.KeySize += 8) { Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var z = new BigInteger(); z.genRandomBits(new Random().Next(1, 2241), new RNGCryptoServiceProvider()); var z_enc = encryptAlgorithm.EncryptData(z.getBytes()); var z_dec = decryptAlgorithm.DecryptData(z_enc); CollectionAssert.AreEqual(z.getBytes(), z_dec); } }
private static void TestZero() { Paillier algorithm = new PaillierManaged(); algorithm.KeySize = 384; algorithm.Padding = PaillierPaddingMode.LeadingZeros; Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var z = new BigInteger(0); var z_enc = encryptAlgorithm.EncryptData(z.getBytes()); for (int i = 0; i < z_enc.Length; i++) { Console.Write(z_enc[i]); } Console.WriteLine(); var z_dec = decryptAlgorithm.DecryptData(z_enc); for (int i = 0; i < z_dec.Length; i++) { Console.Write(z_dec[i]); } Console.WriteLine(); }
public static void TestTextEncryption(string message = "This is to test Paillier encryption and hopefully this message contains more than 2 blocks please please please please please please please please please please please pleaseplease please please pleaseplease please please please ", int keySize = 384, PaillierPaddingMode padding = PaillierPaddingMode.Zeros) { Console.WriteLine(); Console.WriteLine("-- Testing string encryption ---"); var plaintext = Encoding.Default.GetBytes(message); Paillier algorithm = new PaillierManaged(); algorithm.KeySize = keySize; algorithm.Padding = padding; var parametersXML = algorithm.ToXmlString(true); Console.WriteLine("\n{0}\n", PrettifyXML(parametersXML)); Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); var ciphertext = encryptAlgorithm.EncryptData(plaintext); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var candidatePlaintext = decryptAlgorithm.DecryptData(ciphertext); var strip_zeros = StripTrailingZeros(candidatePlaintext, plaintext.Length); Console.WriteLine("Original string: '{0}'", message); Console.WriteLine("Decrypted string: '{0}'", Encoding.Default.GetString(candidatePlaintext)); //Console.WriteLine("Byte arrays equal: {0}", plaintext.SequenceEqual(candidatePlaintext)); Console.WriteLine("Byte arrays equal: {0}", plaintext.SequenceEqual(strip_zeros)); Console.WriteLine(); }
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); }
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 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 void TestRandomBI() { // To inspect the output, no idea why random number of zeroes appear in between decrypted bytes // The original bytes and decrypted bytes are the same without zeroes // Run several times, the appearance of zeroes seems unpredictale Paillier algorithm = new PaillierManaged(); algorithm.Padding = PaillierPaddingMode.LeadingZeros; algorithm.KeySize = 384; Paillier encryptAlgorithm = new PaillierManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); Paillier decryptAlgorithm = new PaillierManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var z = new BigInteger(); var randomBit = new Random().Next(1, 2241); z.genRandomBits(randomBit, new RNGCryptoServiceProvider()); var z_enc = encryptAlgorithm.EncryptData(z.getBytes()); var z_dec = decryptAlgorithm.DecryptData(z_enc); for (int i = 0; i < z.getBytes().Length; i++) { Console.Write(z.getBytes()[i]); } Console.WriteLine(); int zeroCounter = z_dec.Length - z.getBytes().Length; for (int i = 0; i < z_dec.Length; i++) { Console.Write(z_dec[i]); } Console.WriteLine(); Console.WriteLine ("Number of zero: {0}" + "\nNumber of original bytes: {1}" + "\nNumber of decrypted bytes: {2}" + "\nNumber of bits used to gen random bytes: {3}" ,zeroCounter, z.getBytes().Length, z_dec.Length, randomBit); }
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); }