コード例 #1
0
        /// <inheritdoc />
        /// <inheritdoc cref="EnsureParameter" />
        public async Task UnprotectAsync(byte[] encryptedData)
        {
            this.EnsureParameter(encryptedData);
            var decryptedBytes = await _encryptor.DecryptAsync(encryptedData, _key)
                                 .ConfigureAwait(false);

            SetBytesToByteArray(
                source: decryptedBytes,
                target: ref encryptedData);
        }
コード例 #2
0
        private async Task <byte[]> DecryptAsync(byte[] encryptedBytes, int saltLength, byte[] encryptionKey)
        {
            var saltedBytes = await _encryptor.DecryptAsync(encryptedBytes, encryptionKey)
                              .ConfigureAwait(false);

            try
            {
                return(saltedBytes
                       .Take(saltedBytes.Length - saltLength)
                       .ToArray());
            }
            finally
            {
                if (saltedBytes != null)
                {
                    Array.Clear(saltedBytes, 0, saltedBytes.Length);
                }
            }
        }
コード例 #3
0
        /// <inheritdoc />
        /// <inheritdoc cref="EnsureByteIsSet"/>
        /// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/>
        public async Task <byte> RevealDecryptedByteAsync()
        {
            ThrowIfDisposed();
            EnsureByteIsSet();
            byte[] byteBuffer = null;
            try
            {
                var encryptedBuffer = new byte[_encryptedByteLength];
                try
                {
                    using (var @byte = await _encryptedByte.RevealDecryptedBytesAsync().ConfigureAwait(false))
                    {
                        Buffer.BlockCopy(@byte.PlainBytes, 0, encryptedBuffer, 0, _encryptedByteLength);
                    }

                    using (var encryptionKey = await _encryptionKey.RevealDecryptedBytesAsync().ConfigureAwait(false))
                    {
                        byteBuffer = await _encryptor.DecryptAsync(encryptedBuffer, encryptionKey.PlainBytes)
                                     .ConfigureAwait(false);
                    }

                    //Extract the byte from arbitrary bytes
                    return(byteBuffer[_realBytePosition]);
                }
                finally
                {
                    Array.Clear(encryptedBuffer, 0, _encryptedByteLength);
                }
            }
            finally
            {
                if (byteBuffer != null)
                {
                    Array.Clear(byteBuffer, 0, byteBuffer.Length);
                }
            }
        }