コード例 #1
0
ファイル: VolumeCipher.cs プロジェクト: todoasap/CEX-NET
        /// <summary>
        /// Decrypt the files in the specified directory
        /// </summary>
        ///
        /// <param name="FilePaths">A list of the files to be processed</param>
        ///
        /// <exception cref="CryptoProcessingException">Thrown if the VolumeKey does not contain enough keys to encrypt all the files in the directory</exception>
        public void Decrypt(string[] FilePaths)
        {
            if (FilePaths.Length < 1)
            {
                throw new CryptoProcessingException("VolumeCipher:Transform", "The file paths list is empty!", new ArgumentException());
            }

            InitializeProgress(FilePaths);

            if (m_progressTotal < 1)
            {
                throw new CryptoProcessingException("VolumeCipher:Initialize", "The files are all zero bytes!", new ArgumentException());
            }

            long prgCtr = 0;

            for (int i = 0; i < FilePaths.Length; ++i)
            {
                FileStream   inpStream = GetStream(FilePaths[i], true);
                VolumeHeader vh        = GetHeader(inpStream);
                KeyParams    key       = VolumeKey.FromId(m_keyStream, vh.FileId);

                // user dropped a file in, notify or log
                if (key == null)
                {
                    if (ErrorNotification != null)
                    {
                        ErrorNotification(this, string.Format("The file {0}; has no key assigned", FilePaths[i]));
                    }
                }
                else
                {
                    FileStream outStream = GetStream(FilePaths[i], false);

                    if (inpStream == null || outStream == null)
                    {
                        if (ErrorNotification != null)
                        {
                            ErrorNotification(this, string.Format("The file {0}; could not be written to", FilePaths[i]));
                        }
                    }
                    else
                    {
                        m_volumeKey.State[m_volumeKey.GetIndex(vh.FileId)] = (byte)VolumeKeyStates.Decrypted;
                        m_cipherStream.Initialize(false, key);
                        m_cipherStream.Write(inpStream, outStream);
                        outStream.SetLength(outStream.Length - VolumeHeader.GetHeaderSize);

                        prgCtr += inpStream.Position;
                        CalculateProgress(prgCtr);
                        inpStream.Dispose();
                        outStream.Dispose();
                        UpdateKey();
                    }
                }
            }
        }
コード例 #2
0
ファイル: VolumeCipher.cs プロジェクト: todoasap/CEX-NET
        /// <summary>
        /// Encrypt a file with a specific key
        /// </summary>
        ///
        /// <param name="FilePath">The full path to the file</param>
        /// <param name="FileId">The files key id</param>
        public void Encrypt(string FilePath, int FileId)
        {
            if (m_progressTotal < 1)
            {
                throw new CryptoProcessingException("VolumeCipher:Initialize", "The files are all zero bytes!", new ArgumentException());
            }

            KeyParams key = VolumeKey.FromId(m_keyStream, FileId);

            if (key == null)
            {
                if (ErrorNotification != null)
                {
                    ErrorNotification(this, string.Format("The file {0}; has no key assigned", FilePath));
                }
            }
            else
            {
                FileStream inpStream = GetStream(FilePath, true);
                FileStream outStream = GetStream(FilePath, false);

                if (inpStream == null || outStream == null)
                {
                    if (ErrorNotification != null)
                    {
                        ErrorNotification(this, string.Format("The file {0}; could not be written to", FilePath));
                    }
                }
                else
                {
                    int index = m_volumeKey.GetIndex(FileId);
                    m_volumeKey.State[index] = (byte)VolumeKeyStates.Encrypted;
                    m_cipherStream.Initialize(true, key);
                    m_cipherStream.ProgressPercent += OnCipherProgress;
                    m_cipherStream.Write(inpStream, outStream);
                    m_cipherStream.ProgressPercent -= OnCipherProgress;

                    // write the header
                    VolumeHeader vh = new VolumeHeader(m_volumeKey.Tag, m_volumeKey.FileId[index]);
                    outStream.Write(vh.ToBytes(), 0, VolumeHeader.GetHeaderSize);

                    inpStream.Dispose();
                    outStream.Dispose();
                    UpdateKey();
                }
            }
        }
コード例 #3
0
ファイル: VolumeCipher.cs プロジェクト: todoasap/CEX-NET
        /// <summary>
        /// Decrypt a single file in the volume
        /// </summary>
        ///
        /// <param name="InputPath">The path to the encrypted file</param>
        /// <param name="OututPath">The path to the new decrypted file</param>
        public void Decrypt(string InputPath, string OututPath)
        {
            FileStream   inpStream = GetStream(InputPath, true);
            VolumeHeader vh        = GetHeader(inpStream);
            KeyParams    key       = VolumeKey.FromId(m_keyStream, vh.FileId);

            if (key == null)
            {
                if (ErrorNotification != null)
                {
                    ErrorNotification(this, string.Format("The file {0}; has no key assigned", InputPath));
                }
            }
            else
            {
                FileStream outStream = GetStream(OututPath, false);

                if (inpStream == null || outStream == null)
                {
                    if (ErrorNotification != null)
                    {
                        ErrorNotification(this, string.Format("The file {0}; could not be written to", OututPath));
                    }
                }
                else
                {
                    m_volumeKey.State[m_volumeKey.GetIndex(vh.FileId)] = (byte)VolumeKeyStates.Decrypted;
                    m_cipherStream.ProgressPercent += OnCipherProgress;
                    m_cipherStream.Initialize(false, key);
                    m_cipherStream.Write(inpStream, outStream);
                    m_cipherStream.ProgressPercent -= OnCipherProgress;
                    outStream.SetLength(outStream.Length - VolumeHeader.GetHeaderSize);
                    inpStream.Dispose();
                    outStream.Dispose();
                    UpdateKey();
                }
            }
        }