Exemplo n.º 1
0
        static void TestSwitchOAEP()
        {
            // Arrange
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            byte[] authKey = new byte[16];
            rng.GetBytes(authKey);

            byte[] message = new byte[1];
            rng.GetBytes(message);
            Console.WriteLine("Message was " + Environment.NewLine + ByteArrayToString(message) + " (length=" + message.Length + ")");

            SwitchOAEP sc = new SwitchOAEP();

            sc.AuthenticationKey = authKey;

            // Act
            byte[] ciphertext = sc.CreateEncryptor().TransformFinalBlock(message, 0, message.Length);
            Console.WriteLine("Ciphertext was ");
            Console.WriteLine("Etm: " + ByteArrayToString(ciphertext, 0, 32));
            Console.WriteLine("MAC seed: " + ByteArrayToString(ciphertext, 32, 16));
            Console.WriteLine("MAC bits: " + ByteArrayToString(ciphertext, 48, 16));
            Console.WriteLine("Swapped Encoded Msg: " + ByteArrayToString(ciphertext, 64, ciphertext.Length - 64));
            Console.WriteLine("Ciphertext length: " + ciphertext.Length);
            byte[] plaintext = sc.CreateDecryptor().TransformFinalBlock(ciphertext, 0, ciphertext.Length);
            Console.WriteLine("Plantext was" + Environment.NewLine + ByteArrayToString(plaintext) + " (length=" + plaintext.Length + ")");

            bool equal = plaintext.SequenceEqual(message);

            Console.WriteLine(Environment.NewLine + "Plaintext " + (equal ? "was" : "was NOT") + " equal to the message");
        }
Exemplo n.º 2
0
        static void ProduceLengthCSVFile()
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            SwitchOAEP sc     = new SwitchOAEP();
            AesManaged aes128 = new AesManaged();

            aes128.KeySize   = 128;
            aes128.BlockSize = 128;
            aes128.GenerateIV();
            aes128.GenerateKey();
            RijndaelManaged rijndael256 = new RijndaelManaged();

            rijndael256.KeySize   = 256;
            rijndael256.BlockSize = 256;
            rijndael256.GenerateIV();
            rijndael256.GenerateKey();

            //int[] lengths = new int[] { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 };
            //int[] lengths = new int[] { 10, 100, 1000, 10000, 100000, 1000000, 1000001 };
            int x = 256;

            int[] lengths = new int[] { x - 1, x, x + 1, x + 2, x + 3, x + 4, x + 5, x + 6, x + 7, x + 8, x + 9, x + 10, x + 11, x + 12, x + 13, x + 14, x + 15, x + 16 };

            string SwitchOAEPInputOutputLengths = "Message Length, SwitchOAEP Ciphertext Length, AES128 Ciphertext Length, RIJ256 Ciphertext Length" + Environment.NewLine;

            for (int i = 0; i < lengths.Length; i++)
            {
                // Create message of appropriate length
                byte[] message = new byte[lengths[i]];
                rng.GetBytes(message);

                byte[] ciphertextSwitchOAEP = sc.CreateEncryptor().TransformFinalBlock(message, 0, message.Length);
                byte[] ciphertextAes128     = aes128.CreateEncryptor().TransformFinalBlock(message, 0, message.Length);
                byte[] ciphertextAes256     = rijndael256.CreateEncryptor().TransformFinalBlock(message, 0, message.Length);

                string output = message.Length + "," + ciphertextSwitchOAEP.Length + "," + (aes128.IV.Length + ciphertextAes128.Length + 32) + "," + (rijndael256.IV.Length + ciphertextAes256.Length + 32);
                Console.WriteLine(output);
                SwitchOAEPInputOutputLengths += output + Environment.NewLine;
            }

            File.WriteAllText("CipherLengths.csv", SwitchOAEPInputOutputLengths);
        }
Exemplo n.º 3
0
        private void EncryptDecryptMsgLengthTest(int messageLength)
        {
            // Arrange
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            SwitchOAEP sc = new SwitchOAEP();

            byte[] authKey = new byte[16];
            rng.GetBytes(authKey);

            byte[] message = new byte[messageLength];
            rng.GetBytes(message);

            sc.AuthenticationKey = authKey;

            // Act
            byte[] ciphertext = new byte[0];
            byte[] plaintext  = new byte[0];

            try
            {
                // Act
                ciphertext = sc.CreateEncryptor().TransformFinalBlock(message, 0, message.Length);
                plaintext  = sc.CreateDecryptor().TransformFinalBlock(ciphertext, 0, ciphertext.Length);

                // Assert
                CollectionAssert.AreEqual(message, plaintext,
                                          "Message was " + Environment.NewLine + Helper.ByteArrayToString(message) + " (length=" + message.Length + ")" +
                                          Environment.NewLine + "Ciphertext was " + Environment.NewLine + Helper.ByteArrayToString(ciphertext) + " (length=" + ciphertext.Length + ")" +
                                          Environment.NewLine + "Plantext was" + Environment.NewLine + Helper.ByteArrayToString(plaintext) + " (length=" + plaintext.Length + ")");
            }
            catch (Exception e)
            {
                Assert.Fail("Exception: " + e.ToString() + Environment.NewLine +
                            "Message was " + Environment.NewLine + Helper.ByteArrayToString(message) + " (length=" + message.Length + ")" +
                            Environment.NewLine + "Ciphertext was " + Environment.NewLine + Helper.ByteArrayToString(ciphertext) + " (length=" + ciphertext.Length + ")" +
                            Environment.NewLine + "Plantext was" + Environment.NewLine + Helper.ByteArrayToString(plaintext) + " (length=" + plaintext.Length + ")");
            }
        }