Esempio n. 1
0
        //Helpmethod to output the streams in the storage
        //private void WriteDoc(CompoundDocument.StoragePart storagePart, string p)
        //{
        //    foreach (var store in storagePart.SubStorage)
        //    {
        //        string sdir=p + store.Key.Replace((char)6,'x') + "\\";
        //        Directory.CreateDirectory(sdir);
        //        WriteDoc(store.Value, sdir);
        //    }
        //    foreach (var str in storagePart.DataStreams)
        //    {
        //        File.WriteAllBytes(p + str.Key.Replace((char)6, 'x') + ".bin", str.Value);
        //    }
        //}
        /// <summary>
        /// Read the package from the OLE document and decrypt it using the supplied password
        /// </summary>
        /// <param name="stream">The memory stream. </param>
        /// <param name="encryption">The encryption object from the Package</param>
        /// <returns></returns>
        internal MemoryStream DecryptPackage(MemoryStream stream, ExcelEncryption encryption)
        {
            //Create the lockBytes object.
            CompoundDocument.ILockBytes lb = null;
            try
            {
                lb = CompoundDocument.GetLockbyte(stream);

                if (CompoundDocument.IsStorageILockBytes(lb) == 0)
                {
                    var doc = new CompoundDocument(lb);
                    return(GetStreamFromPackage(doc, encryption));
                }
                else
                {
                    Marshal.ReleaseComObject(lb);
                    throw (new InvalidDataException("The stream is not an valid/supported encrypted document."));
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                Marshal.ReleaseComObject(lb);
                lb = null;
            }
        }
        /// <summary>
        /// Check the file is encrpyted.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static bool IsEncrypted(Stream stream)
        {
            Stream encrStream = new MemoryStream();

            CopyStream(stream, ref encrStream);
            CompoundDocument.ILockBytes lb = (CompoundDocument.GetLockbyte((MemoryStream)encrStream));


            if (CompoundDocument.IsStorageILockBytes(lb) == 0)
            {
                return(true);
            }
            return(false);
        }