Example #1
0
        public static byte[] EncryptData(byte[] data, byte[] key)
        {
            long cipherTextLength = data.Length + 16;

            byte[] cipherText = new byte[cipherTextLength];
            byte[] nonce      = StreamEncryption.GetRandomBytes(X_NONC_ESIZE);

            int result = NativeLibsodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
                cipherText,
                out cipherTextLength,
                data,
                data.Length,
                null, 0, null,
                nonce,
                key);

            if (result != 0)
            {
                throw new EncryptionError();
            }

            byte[] cipherTextWithNonce = ConcatNonceAndCipherText(nonce, cipherText);

            return(cipherTextWithNonce);
        }