Example #1
0
        /// <summary>
        /// Extract a KeyParams and CipherDescription from a VolumeKey stream
        /// </summary>
        ///
        /// <param name="KeyStream">The stream containing the VolumeKey</param>
        /// <param name="Index">The index of the key set to extract</param>
        /// <param name="Description">The <see cref="CipherDescription"/> that receives the cipher description</param>
        /// <param name="KeyParam">The <see cref="KeyParams"/> container that receives the key material from the file</param>
        ///
        /// <exception cref="CryptoProcessingException">Thrown if the key file could not be found</exception>
        public void Extract(Stream KeyStream, int Index, out CipherDescription Description, out KeyParams KeyParam)
        {
            if (KeyStream == null || KeyStream.Length < 96)
            {
                throw new CryptoProcessingException("VolumeFactory:Extract", "The key file could not be loaded! Check the stream.", new FileNotFoundException());
            }

            VolumeKey vkey = new VolumeKey(KeyStream);

            Description = vkey.Description;
            KeyParam    = VolumeKey.AtIndex(KeyStream, Index);
        }
Example #2
0
        /// <summary>
        /// Encrypt 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 Encrypt(string[] FilePaths)
        {
            if (FilePaths.Length < 1)
            {
                throw new CryptoProcessingException("VolumeCipher:Transform", "The file paths list is empty!", new ArgumentException());
            }
            if (m_volumeKey.KeyCount() < FilePaths.Length)
            {
                throw new CryptoProcessingException("VolumeCipher:Transform", "Not enough keys in the volume key to encrypt this directory!", 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)
            {
                int       index = m_volumeKey.NextSubKey();
                KeyParams key   = VolumeKey.AtIndex(m_keyStream, index);

                if (key == null)
                {
                    if (ErrorNotification != null)
                    {
                        ErrorNotification(this, string.Format("The file {0}; has no key assigned", FilePaths[i]));
                    }
                }
                else
                {
                    FileStream inpStream = GetStream(FilePaths[i], true);
                    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[index] = (byte)VolumeKeyStates.Encrypted;
                        m_cipherStream.Initialize(true, key);
                        m_cipherStream.Write(inpStream, outStream);

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

                        prgCtr += inpStream.Position;
                        CalculateProgress(prgCtr);
                        inpStream.Dispose();
                        outStream.Dispose();
                        UpdateKey();
                    }
                }
            }
        }