Ejemplo n.º 1
0
        public int ReadAndDecrypt(byte[] buffer, int offset, int count)
        {
            int queueSize  = _data.Count;
            int sizeToRead = count - queueSize;

            if (sizeToRead > 0)
            {
                int         alignedSize = sizeToRead + ((~sizeToRead + 1) & 0xf);
                Span <byte> cipherText  = stackalloc byte[RarRijndael.CRYPTO_BLOCK_SIZE];
                for (int i = 0; i < alignedSize / 16; i++)
                {
                    //long ax = System.currentTimeMillis();
                    _actualStream.Read(cipherText);

                    var readBytes = _rijndael.ProcessBlock(cipherText);
                    foreach (var readByte in readBytes)
                    {
                        _data.Enqueue(readByte);
                    }
                }

                for (int i = 0; i < count; i++)
                {
                    buffer[offset + i] = _data.Dequeue();
                }
            }
            return(count);
        }
        private byte[] ReadAndDecryptBytes(int count)
        {
            int queueSize  = data.Count;
            int sizeToRead = count - queueSize;

            if (sizeToRead > 0)
            {
                int alignedSize = sizeToRead + ((~sizeToRead + 1) & 0xf);
                for (int i = 0; i < alignedSize / 16; i++)
                {
                    //long ax = System.currentTimeMillis();
                    byte[] cipherText = ReadBytesNoCrc(16);
                    var    readBytes  = rijndael.ProcessBlock(cipherText);
                    foreach (var readByte in readBytes)
                    {
                        data.Enqueue(readByte);
                    }
                }
            }

            var decryptedBytes = new byte[count];

            for (int i = 0; i < count; i++)
            {
                var b = data.Dequeue();
                decryptedBytes[i] = b;
                UpdateCrc(b);
            }
            this.readCount += count;
            return(decryptedBytes);
        }