Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Encrypts data with the current session key.
        /// </summary>
        /// <returns>The encrypted data.</returns>
        /// <param name="message">Data to encrypt.</param>
        public byte[] EncryptWithSessionKey(byte[] message)
        {
            byte[] nonce = IEncryption.ConcatBytes(StreamEncryption.GetRandomBytes(X_NONC_ESIZE - 16), BitConverter.GetBytes(DateTime.UtcNow.Ticks), BitConverter.GetBytes(sessionSendKey.index));
            byte[] cipherTextWithNonce = EncryptData(message, GetCurrentSendKey(), nonce);

            // ratchat forward
            RatchetSendKey();

            return(cipherTextWithNonce);
        }