public byte[] Encrypt(byte[] message, long nonce)
    {
        byte[] buffer = new byte[message.Length];
        int    ret    = NativeLibsodium.crypto_stream_chacha20_xor(buffer, message, message.Length, BitConverter.GetBytes(nonce), Convert.FromBase64String(encryptionConfig.ChaChaEncryptionKey));

        if (ret != 0)
        {
            throw new Exception("Error encrypting message.");
        }

        return(buffer);
    }
コード例 #2
0
        public byte[] Decrypt(byte[] message, long nonce)
        {
            byte[] nonceByteArr = BitConverter.GetBytes(nonce);
            byte[] key          = Convert.FromBase64String(ChaChaEncryptionKey);
            byte[] buffer       = new byte[message.Length];
            int    ret          = NativeLibsodium.crypto_stream_chacha20_xor(buffer, message, message.Length, nonceByteArr, key);

            if (ret == 0)
            {
                return(buffer);
            }
            return(null);
        }
    public byte[] Decrypt(byte[] msg, long nonce)
    {
        byte[] nonceBytes = BitConverter.GetBytes(nonce);

        byte[] decrypted = new byte[msg.Length];
        int    ret       = NativeLibsodium.crypto_stream_chacha20_xor(decrypted, msg, msg.Length, nonceBytes, _ChaChaEncryptionKey);

        if (ret != 0)
        {
            throw new Exception("Error derypting message.");
        }

        return(decrypted);
    }