예제 #1
0
        public void DecryptFile(string inFileName, string outFileName, long splitSize, SplitFileMode mode)
        {
            Contract.Requires(splitSize > 0L);
            base.CheckDisposed();

            long i = 1, fileLength;

            while (File.Exists(String.Concat(inFileName, "_Part", i, ".temp")))
            {
                i++;
            }
            FileStream lastInFileStream = new FileStream(String.Concat(inFileName, "_Part", --i, ".temp"), FileMode.Open, FileAccess.ReadWrite);
            long       lastInStreamLength = lastInFileStream.Length - 8;

            lastInFileStream.Position = lastInStreamLength;
            using (var reader = new BinaryReader(lastInFileStream))
            {
                fileLength = reader.ReadInt64();
            }
            lastInFileStream.Position = 0L;
            lastInFileStream.SetLength(lastInStreamLength);
            long oddSize, avgSize = Math.DivRem(fileLength, splitSize, out oddSize),
                 count, size;

            i = 0L;
            switch (mode)
            {
            case SplitFileMode.InputFileLength:
                count = avgSize;
                size  = splitSize;
                break;

            default:
                count = splitSize;
                size  = avgSize;
                break;
            }
            using (ICryptoTransform decrypto = _rijndael.CreateDecryptor(_legalKey, _legalIV))
                using (CryptoStream outStream = new CryptoStream(new FileStream(outFileName, FileMode.Create, FileAccess.Write), decrypto, CryptoStreamMode.Write))
                {
                    while (i < count)
                    {
                        long length = (++i) == count ? size + oddSize : size;
                        using (FileStream inStream = i == count ? lastInFileStream : new FileStream(String.Concat(inFileName, "_Part", i, ".temp"), FileMode.Open, FileAccess.Read))
                        {
                            inStream.FixedCopyTo(outStream, length);
                            if (i == count)
                            {
                                inStream.FixedCopyTo(outStream);
                            }
                        }
                    }
                }
        }
예제 #2
0
        public void EncryptFile(string inFileName, string outFileName, long splitSize, SplitFileMode mode)
        {
            Contract.Requires(splitSize > 0L);
            base.CheckDisposed();

            FileStream inFileStream = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
            long       fileLength = inFileStream.Length, oddSize, avgSize = Math.DivRem(fileLength, splitSize, out oddSize),
                       i = 0L, count, size;

            switch (mode)
            {
            case SplitFileMode.InputFileLength:
                count = avgSize;
                size  = splitSize;
                break;

            default:
                count = splitSize;
                size  = avgSize;
                break;
            }
            using (ICryptoTransform encrypto = _rijndael.CreateEncryptor(_legalKey, _legalIV))
                using (CryptoStream inStream = new CryptoStream(inFileStream, encrypto, CryptoStreamMode.Read))
                {
                    while (i < count)
                    {
                        long length = (++i) == count ? size + oddSize : size;
                        using (FileStream outStream = new FileStream(String.Concat(outFileName, "_Part", i, ".temp"), FileMode.Create, FileAccess.Write))
                        {
                            inStream.FixedCopyTo(outStream, length);
                            if (i == count)
                            {
                                inStream.FixedCopyTo(outStream);
                                using (var writer = new BinaryWriter(outStream))
                                {
                                    writer.Write(fileLength);
                                }
                            }
                        }
                    }
                }
        }