-
-
Save pomarc/f16c15536b9b7bfb88a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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