示例#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");
        }
示例#2
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 + ")");
            }
        }