/// <summary>Creates a Box</summary> /// <param name="message">The message.</param> /// <param name="nonce">The 24 byte nonce.</param> /// <param name="secretKey">The secret key to sign message with.</param> /// <param name="publicKey">The recipient's public key.</param> /// <returns>The encrypted message.</returns> /// <exception cref="KeyOutOfRangeException"></exception> /// <exception cref="NonceOutOfRangeException"></exception> /// <exception cref="CryptographicException"></exception> public static byte[] Create(byte[] message, byte[] nonce, byte[] secretKey, byte[] publicKey) { //validate the length of the secret key if (secretKey == null || secretKey.Length != SecretKeyBytes) { throw new KeyOutOfRangeException("secretKey", secretKey == null ? 0 : secretKey.Length, string.Format("key must be {0} bytes in length.", SecretKeyBytes)); } //validate the length of the public key if (publicKey == null || publicKey.Length != PublicKeyBytes) { throw new KeyOutOfRangeException("publicKey", publicKey == null ? 0 : secretKey.Length, string.Format("key must be {0} bytes in length.", PublicKeyBytes)); } //validate the length of the nonce if (nonce == null || nonce.Length != NONCE_BYTES) { throw new NonceOutOfRangeException("nonce", nonce == null ? 0 : nonce.Length, string.Format("nonce must be {0} bytes in length.", NONCE_BYTES)); } var buffer = new byte[message.Length + MAC_BYTES]; var ret = SodiumLibrary.crypto_box_easy(buffer, message, message.Length, nonce, publicKey, secretKey); if (ret != 0) { throw new CryptographicException("Failed to create PublicKeyBox"); } return(buffer); }