Ejemplo n.º 1
0
        private byte[] SerializeAndEncrypt(IEnumerable <int> safeByteIdList, byte[] encryptionKey)
        {
            var serialization = Serialize(safeByteIdList);
            var encrypted     = _encryptor.Encrypt(serialization, encryptionKey);

            return(encrypted);
        }
Ejemplo n.º 2
0
        public void Protect(byte[] userData)
        {
            if (userData == null)
            {
                throw new ArgumentNullException(nameof(userData));
            }
            var encryptedBytes = _encryptor.Encrypt(userData, _key);

            SetBytesToByteArray(
                source: encryptedBytes,
                target: ref userData);
        }
Ejemplo n.º 3
0
        private void Encrypt(byte b)
        {
            //Mix the with arbitrary bytes
            _realBytePosition = _fastRandom.GetInt(0, SaltSize);
            var arbitraryBytes = _fastRandom.GetBytes(SaltSize);

            RuntimeHelper.ExecuteCodeWithGuaranteedCleanup(
                //Action
                () =>
            {
                arbitraryBytes[_realBytePosition] = b;
                //Get key
                _encryptionKey = _fastRandom.GetBytes(KeySize);
                //Encrypt
                var encryptedBuffer = _encryptor.Encrypt(arbitraryBytes, _encryptionKey);
                RuntimeHelper.ExecuteCodeWithGuaranteedCleanup(
                    //Action
                    () =>
                {
                    //Add arbitrary bytes
                    _encryptedByteLength = encryptedBuffer.Length;
                    _encryptedByte       = GetMemoryProtectableSizedBytes(encryptedBuffer);
                },
                    //Cleanup
                    () =>
                {
                    if (encryptedBuffer != null)
                    {
                        Array.Clear(encryptedBuffer, 0, encryptedBuffer.Length);
                    }
                });
            },
                //Cleanup
                () =>
            {
                Array.Clear(arbitraryBytes, 0, arbitraryBytes.Length);
            });
        }