コード例 #1
0
        /// <summary> Creates a SealedPublicKeyBox</summary>
        /// <param name="message">The message.</param>
        /// <param name="recipientPublicKey">The 32 byte recipient's public key.</param>
        /// <returns>The anonymously encrypted message.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] Create(byte[] message, byte[] recipientPublicKey)
        {
            //validate the length of the recipient public key
            if (recipientPublicKey == null || recipientPublicKey.Length != RecipientPublicKeyBytes)
            {
                throw new KeyOutOfRangeException("recipientPublicKey",
                                                 (recipientPublicKey == null) ? 0 : recipientPublicKey.Length,
                                                 string.Format("recipientPublicKey must be {0} bytes in length.", RecipientPublicKeyBytes));
            }

            var buffer = new byte[message.Length + CryptoBoxSealbytes];
            var ret    = SodiumLibrary.crypto_box_seal(buffer, message, message.Length, recipientPublicKey);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to create SealedBox");
            }

            return(buffer);
        }