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."); }
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."); }
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."); }
public void CryptoBoxKeypair_Should_Success() { //Arrange Byte[] pk = new Byte[TweetNaCl.BoxPublicKeyBytes]; Byte[] sk = new Byte[TweetNaCl.BoxSecretKeyBytes]; //Act pk = TweetNaCl.CryptoBoxKeypair(sk); //Assert Assert.AreEqual(Encoding.ASCII.GetString(pk).Length, 32, "key generation failed."); }
public void CryptoBoxBeforenm_MessageEncryption_Should_Success() { //Arrange Byte[] pk = new Byte[TweetNaCl.BoxPublicKeyBytes]; Byte[] sk = new Byte[TweetNaCl.BoxSecretKeyBytes]; pk = TweetNaCl.CryptoBoxKeypair(sk); //Act var k = TweetNaCl.CryptoBoxBeforenm(pk, sk); //Assert Assert.AreEqual(k.Length, TweetNaCl.BoxBeforenmBytes, "generation of K for encryption failed."); }
public void CryptoBox_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]; pk = TweetNaCl.CryptoBoxKeypair(sk); TweetNaCl.RandomBytes(nonce); //Act var encMessage = TweetNaCl.CryptoBox(bMessage, nonce, pk, sk); //Assert Assert.AreEqual(encMessage.Length, bMessage.Length + TweetNaCl.BoxBoxZeroBytes, "encryption failed."); }
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(); }