//<summary>
        // Encrypts the file without the possibility of recovery
        //<summary>
        public static void EncryptWithoutKey(string path, int bufferLength = BufferLength)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            using (var provider = new RNGCryptoServiceProvider())
            {
                var readWriter = new SynchronizedReadWriter(path);
                var bytes      = new byte[bufferLength];
                while (!readWriter.WriteComplete)
                {
                    readWriter.Read();
                    if (bytes.Length > readWriter.GetBuffer().Length)
                    {
                        bytes = new byte[readWriter.GetBuffer().Length];
                    }

                    provider.GetNonZeroBytes(bytes);
                    readWriter.SetBuffer(Xor(readWriter.GetBuffer(), bytes));
                    readWriter.Write();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        public static void DecryptFile(AesKey aesKey, string filePath, int bufferLength = BufferLength)
        {
            if (aesKey == null)
            {
                throw new ArgumentNullException(nameof(aesKey));
            }

            var key       = aesKey.GetKey();
            var iv        = aesKey.GetIv();
            var keyLength = aesKey.KeySize;

            if (!key.Any() || keyLength <= 0)
            {
                throw new KeyLengthException(Resources.key);
            }
            if (iv == null)
            {
                throw new ArgumentNullException(Resources.iv);
            }
            if (!iv.Any())
            {
                throw new ByteArrayLengthException(Resources.iv);
            }

            var readWriter = new SynchronizedReadWriter(filePath);

            using (var rif = new RijndaelManaged())
            {
                rif.Key       = key;
                rif.IV        = iv;
                rif.KeySize   = keyLength;
                rif.BlockSize = aesKey.BlockSize;
                rif.Mode      = aesKey.Mode;
                rif.Padding   = aesKey.Padding;

                using (var decryptor = rif.CreateDecryptor(key, iv))
                {
                    var bytes = new byte[bufferLength];
                    while (!readWriter.WriteComplete)
                    {
                        readWriter.Read();
                        using (var ms = new MemoryStream(readWriter.GetBuffer()))
                        {
                            if (ms.Length < bytes.Length)
                            {
                                bytes = new byte[ms.Length];
                            }

                            using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                            {
                                cs.Read(bytes, 0, bytes.Length);
                            }
                            readWriter.SetBuffer(bytes);
                            readWriter.Write();
                        }
                    }
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        public static void Transform(string path, string keyPath, int bufferLength = BufferLength)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (string.IsNullOrEmpty(keyPath))
            {
                throw new ArgumentNullException(nameof(keyPath));
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }
            if (!File.Exists(keyPath))
            {
                throw new FileNotFoundException(keyPath);
            }

            if (new FileInfo(path).Length > new FileInfo(keyPath).Length)
            {
                throw new FileLengthGreaterThanKeyLengthException();
            }

            using (var keyStream = new FileStream(keyPath, FileMode.Open))
            {
                var readWriter = new SynchronizedReadWriter(path);
                var keyBytes   = new byte[bufferLength];
                while (!readWriter.WriteComplete)
                {
                    readWriter.Read(keyBytes.Length);
                    if (readWriter.GetBuffer().Length < keyBytes.Length)
                    {
                        keyBytes = new byte[readWriter.GetBuffer().Length];
                    }

                    keyStream.Read(keyBytes, 0, keyBytes.Length);

                    readWriter.SetBuffer(Xor(readWriter.GetBuffer(), keyBytes));
                    readWriter.Write();
                }
            }
        }