public static void TestTextEncryption(string message = "Programming .NET Security", int keySize = 1024, ElGamalPaddingMode padding = ElGamalPaddingMode.Zeros) { Console.WriteLine(); Console.WriteLine("-- Testing string encryption ---"); var plaintext = Encoding.Default.GetBytes(message); ElGamal algorithm = new ElGamalManaged(); algorithm.KeySize = keySize; algorithm.Padding = padding; string parametersXML = algorithm.ToXmlString(true); Console.WriteLine("\n{0}\n", PrettifyXML(parametersXML)); ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); byte[] ciphertext = encryptAlgorithm.EncryptData(plaintext); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); byte[] candidatePlaintext = decryptAlgorithm.DecryptData(ciphertext); 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(); }
public void TestTextEncryption() { int keySize; ElGamalPaddingMode padding = ElGamalPaddingMode.Zeros; string message = "Programming .NET Security"; var plaintext = Encoding.Default.GetBytes(message); ElGamal algorithm = new ElGamalManaged(); algorithm.Padding = padding; for (keySize = 384; keySize <= 1088; keySize += 8) { algorithm.KeySize = keySize; string parametersXML = algorithm.ToXmlString(true); ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); byte[] ciphertext = encryptAlgorithm.EncryptData(plaintext); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); byte[] candidatePlaintext = decryptAlgorithm.DecryptData(ciphertext); CollectionAssert.AreEqual(plaintext, candidatePlaintext, "Failed at keysize: " + keySize.ToString()); } }
public void TestZero_DifferentPaddingMode() { ElGamal algorithm = new ElGamalManaged(); algorithm.KeySize = 384; ElGamalPaddingMode[] paddingModes = { ElGamalPaddingMode.LeadingZeros, ElGamalPaddingMode.Zeros }; foreach (var paddingMode in paddingModes) { algorithm.Padding = paddingMode; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); 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, "Failed on padding mode: " + paddingMode.ToString()); } }
public void TestRandomBI() { // Failed test because of zeroes ElGamal algorithm = new ElGamalManaged(); algorithm.Padding = ElGamalPaddingMode.LeadingZeros; for (algorithm.KeySize = 384; algorithm.KeySize <= 1088; algorithm.KeySize += 8) { ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); 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); } }
public void TestZero() { ElGamal algorithm = new ElGamalManaged(); algorithm.Padding = ElGamalPaddingMode.Zeros; for (int keySize = 384; keySize <= 1088; keySize += 8) { algorithm.KeySize = keySize; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); 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); } }
//[Fact] TODO: Fix text encryption and re-enable the test (implement ANSIX923 or PKCS97 padding) public void TestTextEncryption() { int keySize; var padding = ElGamalPaddingMode.TrailingZeros; var message = "Programming .NET Security"; var plaintext = Encoding.Default.GetBytes(message); ElGamal algorithm = new ElGamalManaged() { Padding = padding }; for (keySize = 384; keySize <= 1088; keySize += 8) { algorithm.KeySize = keySize; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); var ciphertext = encryptAlgorithm.EncryptData(plaintext); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var candidatePlaintext = decryptAlgorithm.DecryptData(ciphertext); Assert.True(plaintext.Equals(candidatePlaintext), $"Failed at keysize: {keySize}"); } }
public void TestZero() { ElGamalPaddingMode[] paddingModes = { ElGamalPaddingMode.LeadingZeros, ElGamalPaddingMode.TrailingZeros }; foreach (var paddingMode in paddingModes) { for (var keySize = 384; keySize <= 1088; keySize += 8) { ElGamal algorithm = new ElGamalManaged { Padding = paddingMode, KeySize = keySize }; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var z = new BigInteger(0); var z_bytes = z.ToByteArray(); var z_enc_bytes = encryptAlgorithm.EncryptData(z_bytes); var z_dec_bytes = decryptAlgorithm.DecryptData(z_enc_bytes); var z_dec = new BigInteger(z_dec_bytes); Assert.Equal(z, z_dec); } } }
public byte[] Decrypt(byte[] cipherText) { var decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(Algorithm.ToXmlString(true)); var candidatePlainText = decryptAlgorithm.DecryptData(cipherText); return(candidatePlainText); }
public byte[] Encrypt(byte[] plainText) { var encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(Algorithm.ToXmlString(false)); var cipherText = encryptAlgorithm.EncryptData(plainText); return(cipherText); }
public static void TestMultiplication_Batch() { Console.WriteLine(); Console.WriteLine("-- Testing multiplication in batch ------"); var rnd = new Random(); for (int i = 0; i < 3; i++) // testing for 3 sets of keys { Console.WriteLine("- Testing for key No.{0} -", i + 1); ElGamal algorithm = new ElGamalManaged(); algorithm.KeySize = 384; algorithm.Padding = ElGamalPaddingMode.LeadingZeros; string parametersXML = algorithm.ToXmlString(true); ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); int error_counter = 0; for (int j = 0; j < 50; j++) // testing for 50 pairs of random numbers { var a = new BigInteger(rnd.Next()); var b = new BigInteger(rnd.Next()); var a_bytes = encryptAlgorithm.EncryptData(a.getBytes()); var b_bytes = encryptAlgorithm.EncryptData(b.getBytes()); var c_bytes = encryptAlgorithm.Multiply(a_bytes, b_bytes); var dec_c = new BigInteger(decryptAlgorithm.DecryptData(c_bytes)); var dec_a = new BigInteger(decryptAlgorithm.DecryptData(a_bytes)); var dec_b = new BigInteger(decryptAlgorithm.DecryptData(b_bytes)); var ab_result = a * b; if (dec_c != ab_result) { error_counter++; Console.WriteLine("Failure #{0}", error_counter); Console.WriteLine("Key = {0}", PrettifyXML(parametersXML)); Console.WriteLine("Encrypted: {0} * {1} = {2}", dec_a.ToString(), dec_b.ToString(), dec_c.ToString()); Console.WriteLine("Plaintext: {0} * {1} = {2}", a.ToString(), b.ToString(), ab_result.ToString()); Console.WriteLine(); } } Console.WriteLine("There are {0}/50 cases that do not pass the test", error_counter); Console.WriteLine(); } }
public void TestMultiplication_Batch() { var rnd = new Random(); for (int keySize = 384; keySize <= 1088; keySize += 8) { for (int i = 0; i < 3; i++) // testing for 3 sets of keys { ElGamal algorithm = new ElGamalManaged(); algorithm.KeySize = keySize; algorithm.Padding = ElGamalPaddingMode.LeadingZeros; string parametersXML = algorithm.ToXmlString(true); ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); int error_counter = 0; for (int j = 0; j < 50; j++) // testing for 50 pairs of random numbers { var a = new BigInteger(rnd.Next()); var b = new BigInteger(rnd.Next()); var a_bytes = encryptAlgorithm.EncryptData(a.getBytes()); var b_bytes = encryptAlgorithm.EncryptData(b.getBytes()); var c_bytes = encryptAlgorithm.Multiply(a_bytes, b_bytes); var dec_c = new BigInteger(decryptAlgorithm.DecryptData(c_bytes)); var dec_a = new BigInteger(decryptAlgorithm.DecryptData(a_bytes)); var dec_b = new BigInteger(decryptAlgorithm.DecryptData(b_bytes)); var ab_result = a * b; if (dec_c != ab_result) { error_counter++; } Assert.AreEqual(dec_c, ab_result); } } } }
public void TestMultiplication_Batch() { var iterations = 3; var rnd = new Random(); for (var keySize = 384; keySize <= 1088; keySize += 8) { for (var i = 0; i < iterations; i++) { ElGamal algorithm = new ElGamalManaged { KeySize = keySize, Padding = ElGamalPaddingMode.BigIntegerPadding }; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var a = new BigInteger(rnd.Next()); var b = new BigInteger(rnd.Next()); var a_bytes = encryptAlgorithm.EncryptData(a.ToByteArray()); var b_bytes = encryptAlgorithm.EncryptData(b.ToByteArray()); var c_bytes = encryptAlgorithm.Multiply(a_bytes, b_bytes); var dec_c = new BigInteger(decryptAlgorithm.DecryptData(c_bytes)); var ab_result = a * b; Assert.True(dec_c == ab_result, $"{Environment.NewLine}{Environment.NewLine}" + $"Algorithm parameters (TRUE):{Environment.NewLine}" + $"{algorithm.ToXmlString(true)}{Environment.NewLine}{Environment.NewLine}" + $"Algorithm parameters (FALSE):{Environment.NewLine}" + $"{algorithm.ToXmlString(false)}{Environment.NewLine}{Environment.NewLine}" + $"a : {a}{Environment.NewLine}{Environment.NewLine}" + $"b : {b}{Environment.NewLine}{Environment.NewLine}" + $"a*b : {ab_result}{Environment.NewLine}{Environment.NewLine}" + $"dec_c : {dec_c}"); } } }
private static long ProfileEncryptedMUL(int iterations, int keyl) { // clean up GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); var rnd = new Random(); // prepare and warm up ElGamal algorithm = new ElGamalManaged(); algorithm.KeySize = keyl; algorithm.Padding = ElGamalPaddingMode.LeadingZeros; string parametersXML = algorithm.ToXmlString(true); ElGamal encryptAlgorithm = new ElGamalManaged(); 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.Multiply(a_bytes, b_bytes); var watch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { c_bytes = encryptAlgorithm.Multiply(a_bytes, b_bytes); } watch.Stop(); return(watch.Elapsed.Ticks); }
public void TestRandomBigInteger() { var rnd = new Random(); var rng = new RNGCryptoServiceProvider(); for (var keySize = 384; keySize <= 1088; keySize += 8) { ElGamal algorithm = new ElGamalManaged { Padding = ElGamalPaddingMode.BigIntegerPadding, KeySize = keySize }; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var z = new BigInteger(); // Plaintext that is bigger than one block needs different padding, // and the encryption loses homomorphic properties z = z.GenRandomBits(rnd.Next(1, ((ElGamalManaged)algorithm).KeyStruct.getPlaintextBlocksize()), rng); var z_enc_bytes = encryptAlgorithm.EncryptData(z.ToByteArray()); var z_dec_bytes = decryptAlgorithm.DecryptData(z_enc_bytes); var z_dec = new BigInteger(z_dec_bytes); Assert.True(z == z_dec, $"{Environment.NewLine}{Environment.NewLine}" + $"Algorithm parameters (TRUE):{Environment.NewLine}" + $"{algorithm.ToXmlString(true)}{Environment.NewLine}{Environment.NewLine}" + $"Algorithm parameters (FALSE):{Environment.NewLine}" + $"{algorithm.ToXmlString(false)}{Environment.NewLine}{Environment.NewLine}" + $"z: {z}{Environment.NewLine}{Environment.NewLine}" + $"z_dec: {z_dec}"); } }
public void TestSpecificCases() { { var algorithmParamsTRUE = "<ElGamalKeyValue><P>hVWJyw9KZna4jNwese2+r6D4li7yXRG6/YIwsmPYY66NGpAigpYJR02IamdmHFe4+UznfJ3G4JZ3dpiI+o6PcHHEMPr88UlxoF9ZDAZ1XWmP7e7MpPPD/U1SK0hBy1bDy0O3Vx7ZAxD842hE</P><G>zjhHbkeMnG5SeApYj2+FDKWMx/+TN4rNrqk51e11C+uWo86oLCvTKqTOWDYAnbN15PrFuAr/bF35Alh3U2qytZCQmagh3E9VaqaRysS5aUvXfnBxlL0x+FvNtAK92r+eNMGE1UPZxVpk1DEu</G><Y>Vu0lxxhOIqluDQary3wx7A8J/cZ5B2xrtXQm8IiZz7b85yjcBr4I5T46JcbCJrLdheRzqdFH+/yE1Hegc/AJRU8mpAeikGTeuZ9wbLN0/QKfu67s6WtjVcjJNPCj+2LkKsN0T4f41+yEtho5</Y><Padding>LeadingZeros</Padding><X>1herKmqCq0sL+4DztH/DNbT27rylaa8L3a0Lb+z1r1aRih6Z+Rq3TtR6N0Q+kKDR8rzifq2G5xBs1MTYLOzcobmQYyXPJDHct/thTKsgaWD7J8P9Yrjj4hKP6iO6RHs1g4t2vuewLcPikFsn</X></ElGamalKeyValue>"; var algorithmParamsFALSE = "<ElGamalKeyValue><P>hVWJyw9KZna4jNwese2+r6D4li7yXRG6/YIwsmPYY66NGpAigpYJR02IamdmHFe4+UznfJ3G4JZ3dpiI+o6PcHHEMPr88UlxoF9ZDAZ1XWmP7e7MpPPD/U1SK0hBy1bDy0O3Vx7ZAxD842hE</P><G>zjhHbkeMnG5SeApYj2+FDKWMx/+TN4rNrqk51e11C+uWo86oLCvTKqTOWDYAnbN15PrFuAr/bF35Alh3U2qytZCQmagh3E9VaqaRysS5aUvXfnBxlL0x+FvNtAK92r+eNMGE1UPZxVpk1DEu</G><Y>Vu0lxxhOIqluDQary3wx7A8J/cZ5B2xrtXQm8IiZz7b85yjcBr4I5T46JcbCJrLdheRzqdFH+/yE1Hegc/AJRU8mpAeikGTeuZ9wbLN0/QKfu67s6WtjVcjJNPCj+2LkKsN0T4f41+yEtho5</Y><Padding>LeadingZeros</Padding></ElGamalKeyValue>"; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithmParamsFALSE); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithmParamsTRUE); var z = new BigInteger(); BigInteger.TryParse("478878612704556930", out z); var z_enc_bytes = encryptAlgorithm.EncryptData(z.ToByteArray()); var z_dec_bytes = decryptAlgorithm.DecryptData(z_enc_bytes); var z_dec = new BigInteger(z_dec_bytes); Assert.Equal(z, z_dec); } { ElGamal algorithm = new ElGamalManaged { KeySize = 384, Padding = ElGamalPaddingMode.TrailingZeros }; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithm.ToXmlString(false)); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithm.ToXmlString(true)); var z = new BigInteger(); BigInteger.TryParse("427514703128554143101621639680", out z); var z_enc_bytes = encryptAlgorithm.EncryptData(z.ToByteArray()); var z_dec_bytes = decryptAlgorithm.DecryptData(z_enc_bytes); var z_dec = new BigInteger(z_dec_bytes); Assert.Equal(z, z_dec); } { var algorithmParamsTRUE = "<ElGamalKeyValue><P>xWq9Kme204HeAuTBZenPYad7JYuqoSeKHveDlluGxOA3huROBtgA1LKT7GvHaohB</P><G>+kaqpuuPc4ziGbOftkw7HkSgOiovsPHvtPVLnVTsDmxLHDWA+0l08HFNz0RPQaAm</G><Y>D3oD1ePykN0L+h389YzXQRvYgKfQwgbPT6lP3sze75A7sOTfw4Y8qMQeGmM/yIcz</Y><Padding>TrailingZeros</Padding><X>c/v9OR4JwIUblP/xluK+6RErZTyLuJRwIb9gTjuW/1zzJkcjUu2HOchXbSW9zLw+</X></ElGamalKeyValue>"; var algorithmParamsFALSE = "<ElGamalKeyValue><P>xWq9Kme204HeAuTBZenPYad7JYuqoSeKHveDlluGxOA3huROBtgA1LKT7GvHaohB</P><G>+kaqpuuPc4ziGbOftkw7HkSgOiovsPHvtPVLnVTsDmxLHDWA+0l08HFNz0RPQaAm</G><Y>D3oD1ePykN0L+h389YzXQRvYgKfQwgbPT6lP3sze75A7sOTfw4Y8qMQeGmM/yIcz</Y><Padding>TrailingZeros</Padding></ElGamalKeyValue>"; ElGamal encryptAlgorithm = new ElGamalManaged(); encryptAlgorithm.FromXmlString(algorithmParamsFALSE); ElGamal decryptAlgorithm = new ElGamalManaged(); decryptAlgorithm.FromXmlString(algorithmParamsTRUE); var a = new BigInteger(248284864); var b = new BigInteger(674886484); var a_bytes = encryptAlgorithm.EncryptData(a.ToByteArray()); var b_bytes = encryptAlgorithm.EncryptData(b.ToByteArray()); var c_bytes = encryptAlgorithm.Multiply(a_bytes, b_bytes); var dec_c = new BigInteger(decryptAlgorithm.DecryptData(c_bytes)); var ab_result = a * b; Assert.Equal(ab_result, dec_c); } { ElGamal algorithm = new ElGamalManaged { KeySize = 384, Padding = ElGamalPaddingMode.BigIntegerPadding }; var a = new BigInteger(2048); var a_bytes = algorithm.EncryptData(a.ToByteArray()); var dec_a = new BigInteger(algorithm.DecryptData(a_bytes)); Assert.Equal(a, dec_a); } { ElGamal algorithm = new ElGamalManaged { KeySize = 384, Padding = ElGamalPaddingMode.BigIntegerPadding }; var a = new BigInteger(138); var a_bytes = algorithm.EncryptData(a.ToByteArray()); var dec_a = new BigInteger(algorithm.DecryptData(a_bytes)); Assert.Equal(a, dec_a); } }