Beispiel #1
0
        /// <summary>
        /// Add a new file id, and key/iv pair to the VolumeKey
        /// </summary>
        ///
        /// <param name="KeyStream">The stream containing the VolumeKey</param>
        /// <param name="Key">The key</param>
        /// <param name="Iv">The vector</param>
        ///
        /// <returns>Returns true if the file id is known, otherwizse false</returns>
        public static void Add(Stream KeyStream, byte[] Key, byte[] Iv)
        {
            using (MemoryStream keyMem = new MemoryStream())
            {
                // get the header
                VolumeKey vkey   = new VolumeKey(KeyStream);
                int       offset = ID_SEEK + (vkey.Count * ID_SIZE);
                KeyStream.Seek(offset, SeekOrigin.Begin);
                int kmlen = vkey.Count * (vkey.Description.KeySize + vkey.Description.IvSize);

                // adjust the header params
                vkey.Count++;
                ArrayUtils.AddAt(ref vkey.FileId, vkey.FileId[vkey.FileId.Length - 1]++, vkey.Count);
                ArrayUtils.AddAt(ref vkey.State, (byte)0, vkey.Count);
                // copy header to mem
                vkey.ToStream().WriteTo(keyMem);

                byte[] data = new byte[kmlen];
                // copy existing key/iv pairs
                KeyStream.Read(data, 0, data.Length);
                keyMem.Write(data, 0, data.Length);
                // write new
                keyMem.Write(Key, 0, Key.Length);
                keyMem.Write(Iv, 0, Iv.Length);

                // overwrite stream w/ new
                KeyStream.Seek(0, SeekOrigin.Begin);
                keyMem.WriteTo(KeyStream);
            }
        }