public byte[] EncryptMessage(byte[] message, byte[] nonce, PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey recipientPublicKey) { // verify arguments if (nonce == null) { throw new ArgumentNullException("nonce"); } if (nonce.Length != NonceSize) { throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", NonceSize)); } // pad the message (prepend zero bytes) var paddedMessage = new byte[_zeroBytes + message.Length]; Buffer.BlockCopy(message, 0, paddedMessage, (int)_zeroBytes, message.Length); // encrypt var encryptedMessage = new byte[paddedMessage.Length]; var result = PlatformInvoke32.crypto_box(encryptedMessage, paddedMessage, paddedMessage.Length, nonce, recipientPublicKey.PlainBytes, key.SecretKeyBytes); if (result != 0) { throw new CryptographicException("Failed"); } return(encryptedMessage); }
public byte[] VerifyAndDecryptMessage(byte[] encryptedMessage, byte[] nonce, PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey senderPublicKey) { // verify arguments if (nonce == null) { throw new ArgumentNullException("nonce"); } if (nonce.Length != NonceSize) { throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", NonceSize)); } // decrypt var paddedMessage = new byte[encryptedMessage.Length]; var result = PlatformInvoke32.crypto_box_open(paddedMessage, encryptedMessage, encryptedMessage.Length, nonce, senderPublicKey.PlainBytes, key.SecretKeyBytes); if (result != 0) { throw new CryptographicException("Failed"); } var message = new byte[paddedMessage.Length - _zeroBytes]; Buffer.BlockCopy(paddedMessage, (int)_zeroBytes, message, 0, message.Length); return(message); }
public byte[] Encrypt(byte[] message, byte[] nonce) { // verify arguments if (nonce == null) { throw new ArgumentNullException("nonce"); } if (nonce.Length != _parent.NonceSize) { throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", _parent.NonceSize)); } // pad the message (prepend zero bytes) var paddedMessage = new byte[_parent._zeroBytes + message.Length]; Buffer.BlockCopy(message, 0, paddedMessage, (int)_parent._zeroBytes, message.Length); // encrypt var encryptedMessage = new byte[paddedMessage.Length]; var result = PlatformInvoke32.crypto_box_afternm(encryptedMessage, paddedMessage, paddedMessage.Length, nonce, _sharedSecret); if (result != 0) { throw new CryptographicException("Failed"); } return(encryptedMessage); }
public byte[] VerifyAndDecrypt(byte[] encryptedMessage, byte[] nonce) { // verify arguments if (nonce == null) { throw new ArgumentNullException("nonce"); } if (nonce.Length != _parent.NonceSize) { throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", _parent.NonceSize)); } // decrypt var paddedMessage = new byte[encryptedMessage.Length]; var result = PlatformInvoke32.crypto_box_open_afternm(paddedMessage, encryptedMessage, encryptedMessage.Length, nonce, _sharedSecret); if (result != 0) { throw new CryptographicException("Failed"); } var message = new byte[paddedMessage.Length - _parent._zeroBytes]; Buffer.BlockCopy(paddedMessage, (int)_parent._zeroBytes, message, 0, message.Length); return(message); }
public byte[] CreateRandomNonce() { var nonce = new byte[NonceSize]; PlatformInvoke32.randombytes_buf(nonce, new UIntPtr((uint)nonce.Length)); return(nonce); }
public AuthenticatedPublicKeyCrypto32() { _publicKeyBytes = PlatformInvoke32.crypto_box_publickeybytes().ToUInt32(); _secretKeyBytes = PlatformInvoke32.crypto_box_secretkeybytes().ToUInt32(); _zeroBytes = PlatformInvoke32.crypto_box_zerobytes().ToUInt32(); _beforeNmBytes = PlatformInvoke32.crypto_box_beforenmbytes().ToUInt32(); NonceSize = PlatformInvoke32.crypto_box_noncebytes().ToUInt32(); }
private PrimitiveCryptoConstructs() { if (Environment.Is64BitProcess) { PlatformInvoke64.sodium_init(); } else { PlatformInvoke32.sodium_init(); } }
public PublicKeyCryptoKeyPair CreateRandomKeyPair() { var publicKey = new byte[_publicKeyBytes]; var secretKey = new byte[_secretKeyBytes]; var result = PlatformInvoke32.crypto_box_keypair(publicKey, secretKey); if (result != 0) { throw new CryptographicException("Failed"); } return(new PublicKeyCryptoKeyPair(secretKey, publicKey)); }
public IAuthenticatedPublicKeyCryptoContext CreateCrypterInstance(PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey publicKey) { // compute the shared secret var sharedSecret = new byte[_beforeNmBytes]; var result = PlatformInvoke32.crypto_box_beforenm(sharedSecret, publicKey.PlainBytes, key.SecretKeyBytes); if (result != 0) { throw new CryptographicException("Failed"); } return(new AuthenticatedPublicKeyCryptoContext(this, sharedSecret)); }
public byte[] VerifySignedMessage(byte[] signedMessage, SignerPublicKey publicKey) { var message = new byte[signedMessage.Length]; long messageRealLength = 0; var result = PlatformInvoke32.crypto_sign_open(message, ref messageRealLength, signedMessage, signedMessage.Length, publicKey.PlainBytes); if (result != 0) { throw new CryptographicException("Failed"); } Array.Resize(ref message, (int)messageRealLength); return(message); }
public byte[] SignMessage(byte[] message, SignerKeyPair key) { var signedMessage = new byte[message.Length + _bytes]; long signedMessageRealLength = 0; var result = PlatformInvoke32.crypto_sign(signedMessage, ref signedMessageRealLength, message, message.Length, key.SecretKeyBytes); if (result != 0) { throw new CryptographicException("Failed"); } Array.Resize(ref signedMessage, (int)signedMessageRealLength); return(signedMessage); }
public void GetRandomBytes(byte[] buffer) { PlatformInvoke32.randombytes_buf(buffer, new UIntPtr((uint)buffer.Length)); }
public MessageSigner32() { _publicKeyBytes = PlatformInvoke32.crypto_sign_publickeybytes().ToUInt32(); _secretKeyBytes = PlatformInvoke32.crypto_sign_secretkeybytes().ToUInt32(); _bytes = PlatformInvoke32.crypto_sign_bytes().ToUInt32(); }