Skip to content

Instantly share code, notes, and snippets.

@pomarc
Created July 31, 2014 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pomarc/f16c15536b9b7bfb88a7 to your computer and use it in GitHub Desktop.
Save pomarc/f16c15536b9b7bfb88a7 to your computer and use it in GitHub Desktop.
class KeyPair
{
internal byte[] PublicKey = new byte[CryptoBox.PublicKeyBytes];
internal byte[] SecretKey = new byte[CryptoBox.SecretKeyBytes];
}
static void TestAsymmetric()
{
// First create the keypairs for both Alice and Bob
var Alice = new KeyPair();
var Bob = new KeyPair();
CryptoBox.ComputeKeyPair(Alice.PublicKey, Alice.SecretKey);
CryptoBox.ComputeKeyPair(Bob.PublicKey, Bob.SecretKey);
// Then specify the nonce and fill it with random values
var nonce = new byte[CryptoBox.NonceBytes];
RandomNumberGenerator rng = RandomNumberGenerator.Create();
rng.GetBytes(nonce);
// Alice sends a message to Bob
var message = "Hear Me Roar!";
byte[] secretMessage = EncryptAsymmetric(message, Bob.PublicKey, Alice.SecretKey, nonce);
// Bob receives the encrypted message and decrypts it
string decryptedMessage = DecryptAsymmetric(secretMessage, Alice.PublicKey, Bob.SecretKey, nonce);
//test:
if (decryptedMessage == message)
{
Debug.Print("OK! the text matches");
}
else
{
Debug.Print("ERROR: this should NOT happen, ouch, the decyphered text is wrong!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment