Ejemplo n.º 1
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);
                                }
                            }
                        }
                    }
                }
        }
Ejemplo n.º 2
0
        public void DecryptTo(Stream inStream, Stream outStream)
        {
            Contract.Requires(inStream != null && outStream != null);
            base.CheckDisposed();

            var crypto = new CryptoStream(inStream, _rijndael.CreateDecryptor(_legalKey, _legalIV), CryptoStreamMode.Read);

            crypto.FixedCopyTo(outStream);
            if (!crypto.HasFlushedFinalBlock)
            {
                crypto.FlushFinalBlock();
            }
        }
Ejemplo n.º 3
0
        public MemoryStream Decrypt(Stream input)
        {
            Contract.Requires(input != null);
            base.CheckDisposed();

            var output = new MemoryStream();
            var crypto = new CryptoStream(input, _rijndael.CreateDecryptor(_legalKey, _legalIV), CryptoStreamMode.Read);

            crypto.FixedCopyTo(output);
            if (!crypto.HasFlushedFinalBlock)
            {
                crypto.FlushFinalBlock();
            }
            output.Position = 0L;
            return(output);
        }