public static byte[] DecodeMessage(byte[] message, byte[] nonce, byte[] senderPublicKey, byte[] recipientPrivateKey) { var curvePublicKey = PublicKeyAuth.ConvertEd25519PublicKeyToCurve25519PublicKey(senderPublicKey); var curveSecretKey = PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(recipientPrivateKey); return(TweetNaCl.CryptoBoxOpen(message, nonce, curvePublicKey, curveSecretKey)); }
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 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."); }
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(); }