public void TestForMessageDecryptionTweetNaCljsKeyPair()
        {
            Byte[] apk = Convert.FromBase64String("GK4GzNY+fbkRPd5fwYUaca70iENh2A1QRss1KBtpWU4=");
            Byte[] ask = Convert.FromBase64String("HQT4qtjv/3Q0nGYX4DB776e6QeUE40wr71MxNSg0+bc=");

            Byte[] bpk = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] bsk = new Byte[TweetNaCl.BoxSecretKeyBytes];

            String message = "test";

            Byte[] bMessage      = Encoding.UTF8.GetBytes(message);
            Byte[] paddedMessage = new Byte[TweetNaCl.BoxZeroBytes + bMessage.Length];
            Byte[] nonce         = new Byte[TweetNaCl.BoxNonceBytes];
            Byte[] k             = new Byte[TweetNaCl.BoxBeforenmBytes];

            var result = -10;

            apk = TweetNaCl.CryptoBoxKeypair(ask);
            Assert.AreNotEqual(result, -1, "key pair A generation failed.");

            bpk = TweetNaCl.CryptoBoxKeypair(bsk);
            Assert.AreNotEqual(result, -1, "key pair B generation failed.");

            TweetNaCl.RandomBytes(nonce);
            Assert.AreNotEqual(result, -1, "randombytes generation failed.");

            var encMessage = TweetNaCl.CryptoBox(paddedMessage, nonce, bpk, ask);

            Assert.AreNotEqual(result, -1, "encryption failed.");

            var decMessage = TweetNaCl.CryptoBoxOpen(encMessage, nonce, apk, bsk);

            Assert.AreNotEqual(result, -1, "decryption failed.");
        }
Beispiel #2
0
        public void RandomBytesGeneration_Should_Maintain_Length()
        {
            //Arrange
            Byte[] b1   = new Byte[1];
            Byte[] b10  = new Byte[10];
            Byte[] b100 = new Byte[100];
            Byte[] b200 = new Byte[200];

            //Act
            TweetNaCl.RandomBytes(b1);
            TweetNaCl.RandomBytes(b10);
            TweetNaCl.RandomBytes(b100);
            TweetNaCl.RandomBytes(b200);

            //Assert
            String text1   = Encoding.ASCII.GetString(b1);
            String text10  = Encoding.ASCII.GetString(b10);
            String text100 = Encoding.ASCII.GetString(b100);
            String text200 = Encoding.ASCII.GetString(b200);

            Assert.AreEqual(1, text1.Length);
            Assert.AreEqual(10, text10.Length);
            Assert.AreEqual(100, text100.Length);
            Assert.AreEqual(200, text200.Length);
        }
Beispiel #3
0
        public void CryptoBoxOpen_DecryptionDifferentKeyPair_Should_Success()
        {
            //Arrange
            Byte[] apk = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] ask = new Byte[TweetNaCl.BoxSecretKeyBytes];

            Byte[] bpk = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] bsk = new Byte[TweetNaCl.BoxSecretKeyBytes];

            String expectedMessage = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(expectedMessage);
            Byte[] nonce    = new Byte[TweetNaCl.BoxNonceBytes];

            apk = TweetNaCl.CryptoBoxKeypair(ask);
            bpk = TweetNaCl.CryptoBoxKeypair(bsk);

            TweetNaCl.RandomBytes(nonce);

            //Act
            var encMessage = TweetNaCl.CryptoBox(bMessage, nonce, bpk, ask);
            var decMessage = TweetNaCl.CryptoBoxOpen(encMessage, nonce, apk, bsk);

            //Assert
            Assert.AreEqual(encMessage.Length, bMessage.Length + TweetNaCl.BoxBoxZeroBytes, "encryption failed.");
            Assert.AreEqual(decMessage.Length, bMessage.Length, "decryption failed.");

            var resultMessage = Encoding.ASCII.GetString(decMessage);

            Assert.AreEqual(resultMessage, expectedMessage, "decryption failed.");
        }
Beispiel #4
0
        public void CryptoBoxOpenAfternm_Decryption_Should_Success()
        {
            //Arrange
            String expectedMessage = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(expectedMessage);
            Byte[] pk       = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] sk       = new Byte[TweetNaCl.BoxSecretKeyBytes];
            Byte[] nonce    = new Byte[TweetNaCl.BoxNonceBytes];

            pk = TweetNaCl.CryptoBoxKeypair(sk);
            TweetNaCl.RandomBytes(nonce);
            var k = TweetNaCl.CryptoBoxBeforenm(pk, sk);

            //Act
            var encMessage = TweetNaCl.CryptoBoxAfternm(bMessage, nonce, k);
            var decMessage = TweetNaCl.CryptoBoxOpenAfternm(encMessage, nonce, k);

            //Assert
            Assert.AreEqual(decMessage.Length, bMessage.Length, "decryption failed.");
            Assert.AreEqual(decMessage, bMessage, "decryption failed.");

            var resultMessage = Encoding.ASCII.GetString(decMessage);

            Assert.AreEqual(resultMessage, expectedMessage, "decryption failed.");
        }
Beispiel #5
0
        public void CryptoScalarmult_Should_Success()
        {
            //Arrange
            Byte[] n = new Byte[TweetNaCl.ScalarmultBytes];
            Byte[] p = new Byte[TweetNaCl.ScalarBytes];
            Byte[] q = new Byte[TweetNaCl.ScalarmultBytes];

            TweetNaCl.RandomBytes(n);
            TweetNaCl.RandomBytes(p);

            //Act
            q = TweetNaCl.CryptoScalarmult(n, p);

            //Assert
            Assert.AreEqual(Encoding.ASCII.GetString(q).Length, 32, "wrong size for resulting group element q.");
        }
Beispiel #6
0
        public void CryptoSecretBox_Should_Success()
        {
            //Arrange
            String message = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(message);
            Byte[] sk       = new Byte[TweetNaCl.SecretBoxKeyBytes];
            Byte[] nonce    = new Byte[TweetNaCl.SecretBoxNonceBytes];
            TweetNaCl.RandomBytes(sk);
            TweetNaCl.RandomBytes(nonce);

            //Act
            var encMessage = TweetNaCl.CryptoSecretBox(bMessage, nonce, sk);

            //Assert
            Assert.AreEqual(encMessage.Length, bMessage.Length + TweetNaCl.BoxBoxZeroBytes, "encryption failed.");
        }
Beispiel #7
0
        public void CryptoBoxAfternm_MessageEncryption_Should_Success()
        {
            //Arrange
            String message = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(message);
            Byte[] pk       = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] sk       = new Byte[TweetNaCl.BoxSecretKeyBytes];
            Byte[] nonce    = new Byte[TweetNaCl.BoxNonceBytes];
            TweetNaCl.RandomBytes(nonce);

            pk = TweetNaCl.CryptoBoxKeypair(sk);

            var k = TweetNaCl.CryptoBoxBeforenm(pk, sk);

            //Act
            var encMessage = TweetNaCl.CryptoBoxAfternm(bMessage, nonce, k);

            //Assert
            Assert.AreEqual(encMessage.Length, bMessage.Length + TweetNaCl.BoxBoxZeroBytes, "encryption failed.");
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            Timer timer = Metric.Timer("Requests", Unit.Requests);

            for (var i = 0; i < 10; i++)
            {
                Byte[] apk = new Byte[TweetNaCl.BOX_PUBLICKEYBYTES];
                Byte[] ask = new Byte[TweetNaCl.BOX_SECRETKEYBYTES];

                Byte[] bpk = new Byte[TweetNaCl.BOX_PUBLICKEYBYTES];
                Byte[] bsk = new Byte[TweetNaCl.BOX_SECRETKEYBYTES];

                String message  = "test";
                Byte[] bMessage = Encoding.UTF8.GetBytes(message);
                Byte[] nonce    = new Byte[TweetNaCl.BOX_NONCEBYTES];
                Byte[] k        = new Byte[TweetNaCl.BOX_BEFORENMBYTES];

                apk = TweetNaCl.CryptoBoxKeypair(ask);
                bpk = TweetNaCl.CryptoBoxKeypair(bsk);

                TweetNaCl.RandomBytes(nonce);

                using (var context = timer.NewContext("Encryption"))
                {
                    var encMessage = TweetNaCl.CryptoBox(bMessage, nonce, bpk, ask);
                    var decMessage = TweetNaCl.CryptoBoxOpen(encMessage, nonce, apk, bsk);
                }

                using (var context = timer.NewContext("Decryption"))
                {
                    var encMessage = TweetNaCl.CryptoBox(bMessage, nonce, bpk, ask);
                    var decMessage = TweetNaCl.CryptoBoxOpen(encMessage, nonce, apk, bsk);
                }

                //Console.WriteLine(timer.);
            }

            Console.ReadKey();
        }