Example #1
0
        public void ApplyDrm()
        {
            if (EncryptionProvider == null)
            {
                throw new ArgumentNullException("EncryptionProvider");
            }

            if (Encrypted)
            {
                throw new Exception("EPUB is already encrypted");
            }

            if (!HasRights)
            {
                throw new Exception("No rights specified");
            }

            // encrypt all elements except directory names and exempt files (content.opf, toc.ncx, cover image, etc)
            var entries = _archive.Entries
                          .Where(x => !string.IsNullOrEmpty(x.Name) &&
                                 !DrmExemptParts.Contains(x.FullName)).ToList();

            foreach (var entry in entries)
            {
                var data = GetArchiveEntryBytes(entry.FullName);
                entry.Delete();

                var newEntry = _archive.CreateEntry(entry.FullName);
                using (var stream = newEntry.Open())
                    EncryptionProvider.EncryptToStream(data, stream);
            }

            _encrypted = true;
        }