Пример #1
0
 private void saveSToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (doc != null)
     {
         doc.Save();
     }
 }
Пример #2
0
        private void EncryptPackageBinary(Stream package, ExcelEncryption encryption, Stream outputStream)
        {
            byte[] encryptionKey;
            //Create the Encryption Info. This also returns the Encryptionkey
            var encryptionInfo = CreateEncryptionInfo(encryption.Password,
                                                      encryption.Algorithm == EncryptionAlgorithm.AES128 ?
                                                      AlgorithmID.AES128 :
                                                      encryption.Algorithm == EncryptionAlgorithm.AES192 ?
                                                      AlgorithmID.AES192 :
                                                      AlgorithmID.AES256, out encryptionKey);

            //ILockBytes lb;
            //var iret = CreateILockBytesOnHGlobal(IntPtr.Zero, true, out lb);

            //IStorage storage = null;
            //MemoryStream ret = null;

            var doc = new CompoundDocument(tempFolder);

            CreateDataSpaces(doc);

            doc.Storage.DataStreams.Add("EncryptionInfo", new MemoryStream(encryptionInfo.WriteBinary()));

            //Encrypt the package
            byte[]       encryptedPackage = EncryptData(encryptionKey, package, false);
            MemoryStream ms = new MemoryStream();

            ms.Write(BitConverter.GetBytes((ulong)package.Length), 0, 8);
            ms.Write(encryptedPackage, 0, encryptedPackage.Length);
            doc.Storage.DataStreams.Add("EncryptedPackage", ms);

            doc.Save(outputStream);
        }
Пример #3
0
        public void SaveToStream(Stream stream)
        {
            CompoundDocument doc     = CompoundDocument.CreateFromStream(stream);
            MemoryStream     mstream = new MemoryStream();

            WorkbookEncoder.Encode(this, mstream);
            doc.WriteStreamData(new string[] { "Workbook" }, mstream.ToArray());
            doc.Save();
        }
Пример #4
0
        public void Save(string file)
        {
            CompoundDocument doc    = CompoundDocument.Create(file);
            MemoryStream     stream = new MemoryStream();

            WorkbookEncoder.Encode(this, stream);
            doc.WriteStreamData(new string[] { "Workbook" }, stream.ToArray());
            doc.Save();
            doc.Close();
        }
Пример #5
0
        internal void Save()
        {
            if (Validate())
            {
                CompoundDocument doc = new CompoundDocument();
                doc.Storage = new CompoundDocument.StoragePart();
                var store = new CompoundDocument.StoragePart();
                doc.Storage.SubStorage.Add("VBA", store);

                store.DataStreams.Add("_VBA_PROJECT", CreateVBAProjectStream());
                store.DataStreams.Add("dir", CreateDirStream());
                foreach (var module in Modules)
                {
                    store.DataStreams.Add(module.Name, CompoundDocument.CompressPart(Encoding.GetEncoding(CodePage).GetBytes(module.Attributes.GetAttributeText() + module.Code)));
                }

                //Copy streams from the template, if used.
                if (Document != null)
                {
                    foreach (var ss in Document.Storage.SubStorage)
                    {
                        if (ss.Key != "VBA")
                        {
                            doc.Storage.SubStorage.Add(ss.Key, ss.Value);
                        }
                    }
                    foreach (var s in Document.Storage.DataStreams)
                    {
                        if (s.Key != "dir" && s.Key != "PROJECT" && s.Key != "PROJECTwm")
                        {
                            doc.Storage.DataStreams.Add(s.Key, s.Value);
                        }
                    }
                }

                doc.Storage.DataStreams.Add("PROJECT", CreateProjectStream());
                doc.Storage.DataStreams.Add("PROJECTwm", CreateProjectwmStream());

                if (Part == null)
                {
                    Uri  = new Uri(PartUri, UriKind.Relative);
                    Part = _pck.CreatePart(Uri, ExcelPackage.schemaVBA);
                    var rel = _wb.Part.CreateRelationship(Uri, TargetMode.Internal, schemaRelVba);
                }
                var vbaBuffer = doc.Save();
                var st        = Part.GetStream(FileMode.Create);
                st.Write(vbaBuffer, 0, vbaBuffer.Length);
                st.Flush();
                st.Close();
                //Save the digital signture
                Signature.Save(this);
            }
        }
Пример #6
0
 /// <summary>
 /// Save workbook to a file.
 /// </summary>
 /// <param name="file"></param>
 public void Save(string file)
 {
     using (CompoundDocument doc = CompoundDocument.Create(file))
     {
         using (MemoryStream memStream = new MemoryStream())
         {
             WorkbookEncoder.Encode(this, memStream);
             doc.WriteStreamData(new string[] { "Workbook" }, memStream.ToArray());
             doc.Save();
         }
     }
 }
Пример #7
0
        private void _btSave_Click(object sender, EventArgs e)
        {
            if (doc == null)
            {
                return;
            }
            string file = FileSelector.BrowseFileForSave(FileType.All);

            if (file == null)
            {
                return;
            }

            using (CompoundDocument newDoc = CompoundDocument.Create(file))
            {
                foreach (string streamName in doc.RootStorage.Members.Keys)
                {
                    newDoc.WriteStreamData(new string[] { streamName }, doc.GetStreamData(streamName));
                }

                byte[] bookdata = doc.GetStreamData("Workbook");
                if (bookdata != null)
                {
                    if (workbook == null)
                    {
                        workbook = WorkbookDecoder.Decode(new MemoryStream(bookdata));
                    }
                    MemoryStream stream = new MemoryStream();
                    //WorkbookEncoder.Encode(workbook, stream);

                    BinaryWriter writer = new BinaryWriter(stream);
                    foreach (Record record in workbook.Records)
                    {
                        record.Write(writer);
                    }
                    writer.Close();
                    newDoc.WriteStreamData(new string[] { "Workbook" }, stream.ToArray());
                }
                newDoc.Save();
            }
        }
Пример #8
0
        private MemoryStream EncryptPackageBinary(byte[] package, ExcelEncryption encryption)
        {
            byte[] encryptionKey;
            //Create the Encryption Info. This also returns the Encryptionkey
            var encryptionInfo = CreateEncryptionInfo(encryption.Password,
                                                      encryption.Algorithm == EncryptionAlgorithm.AES128 ?
                                                      AlgorithmID.AES128 :
                                                      encryption.Algorithm == EncryptionAlgorithm.AES192 ?
                                                      AlgorithmID.AES192 :
                                                      AlgorithmID.AES256, out encryptionKey);

            //ILockBytes lb;
            //var iret = CreateILockBytesOnHGlobal(IntPtr.Zero, true, out lb);

            //IStorage storage = null;
            //MemoryStream ret = null;

            var doc = new CompoundDocument();

            CreateDataSpaces(doc);

            doc.Storage.DataStreams.Add("EncryptionInfo", encryptionInfo.WriteBinary());

            //Encrypt the package
            byte[] encryptedPackage = EncryptData(encryptionKey, package, false);
            using (var ms = RecyclableMemory.GetStream())
            {
                ms.Write(BitConverter.GetBytes((ulong)package.Length), 0, 8);
                ms.Write(encryptedPackage, 0, encryptedPackage.Length);
                doc.Storage.DataStreams.Add("EncryptedPackage", ms.ToArray());
            }

            var ret = RecyclableMemory.GetStream();

            doc.Save(ret);

            return(ret);
        }
Пример #9
0
        public void Save(Stream s)
        {
            BinaryWriter bw = new BinaryWriter(s);

            //Writing Column Naow
            int TotalRows = this.Worksheets[0].Cells.LastRowIndex;

            for (int i = TotalRows + 1; i < TotalRows + 50; i++)
            {
                for (int ColumnCount = 0; ColumnCount < 5; ColumnCount++)
                {
                    this.Worksheets[0].Cells[i, ColumnCount] = new Cell("");
                }
            }

            CompoundDocument doc    = CompoundDocument.Create(s, bw);
            MemoryStream     stream = new MemoryStream();

            WorkbookEncoder.Encode(this, stream);
            doc.WriteStreamData(new string[] { "Workbook" }, stream.ToArray());

            doc.Save();
            doc.Close();
        }
Пример #10
0
        private MemoryStream EncryptPackageAgile(byte[] package, ExcelEncryption encryption)
        {
            var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n";

            xml += "<encryption xmlns=\"http://schemas.microsoft.com/office/2006/encryption\" xmlns:p=\"http://schemas.microsoft.com/office/2006/keyEncryptor/password\" xmlns:c=\"http://schemas.microsoft.com/office/2006/keyEncryptor/certificate\">";
            xml += "<keyData saltSize=\"16\" blockSize=\"16\" keyBits=\"256\" hashSize=\"64\" cipherAlgorithm=\"AES\" cipherChaining=\"ChainingModeCBC\" hashAlgorithm=\"SHA512\" saltValue=\"\"/>";
            xml += "<dataIntegrity encryptedHmacKey=\"\" encryptedHmacValue=\"\"/>";
            xml += "<keyEncryptors>";
            xml += "<keyEncryptor uri=\"http://schemas.microsoft.com/office/2006/keyEncryptor/password\">";
            xml += "<p:encryptedKey spinCount=\"100000\" saltSize=\"16\" blockSize=\"16\" keyBits=\"256\" hashSize=\"64\" cipherAlgorithm=\"AES\" cipherChaining=\"ChainingModeCBC\" hashAlgorithm=\"SHA512\" saltValue=\"\" encryptedVerifierHashInput=\"\" encryptedVerifierHashValue=\"\" encryptedKeyValue=\"\" />";
            xml += "</keyEncryptor></keyEncryptors></encryption>";

            var encryptionInfo = new EncryptionInfoAgile();

            encryptionInfo.ReadFromXml(xml);
            var encr = encryptionInfo.KeyEncryptors[0];
            var rnd  = RandomNumberGenerator.Create();

            var s = new byte[16];

            rnd.GetBytes(s);
            encryptionInfo.KeyData.SaltValue = s;

            rnd.GetBytes(s);
            encr.SaltValue = s;

            encr.KeyValue = new byte[encr.KeyBits / 8];
            rnd.GetBytes(encr.KeyValue);

            //Get the passwork key.
            var hashProvider = GetHashProvider(encryptionInfo.KeyEncryptors[0]);
            var baseHash     = GetPasswordHash(hashProvider, encr.SaltValue, encryption.Password, encr.SpinCount, encr.HashSize);
            var hashFinal    = GetFinalHash(hashProvider, encr, BlockKey_KeyValue, baseHash);

            hashFinal = FixHashSize(hashFinal, encr.KeyBits / 8);

            var encrData = EncryptDataAgile(package, encryptionInfo, hashProvider);

            /**** Data Integrity ****/
            var saltHMAC = new byte[64];

            rnd.GetBytes(saltHMAC);

            SetHMAC(encryptionInfo, hashProvider, saltHMAC, encrData);

            /**** Verifier ****/
            encr.VerifierHashInput = new byte[16];
            rnd.GetBytes(encr.VerifierHashInput);

            encr.VerifierHash = hashProvider.ComputeHash(encr.VerifierHashInput);

            var VerifierInputKey = GetFinalHash(hashProvider, encr, BlockKey_HashInput, baseHash);
            var VerifierHashKey  = GetFinalHash(hashProvider, encr, BlockKey_HashValue, baseHash);
            var KeyValueKey      = GetFinalHash(hashProvider, encr, BlockKey_KeyValue, baseHash);

            var ms = new MemoryStream();

            EncryptAgileFromKey(encr, VerifierInputKey, encr.VerifierHashInput, 0, encr.VerifierHashInput.Length, encr.SaltValue, ms);
            encr.EncryptedVerifierHashInput = ms.ToArray();

            ms = new MemoryStream();
            EncryptAgileFromKey(encr, VerifierHashKey, encr.VerifierHash, 0, encr.VerifierHash.Length, encr.SaltValue, ms);
            encr.EncryptedVerifierHash = ms.ToArray();

            ms = new MemoryStream();
            EncryptAgileFromKey(encr, KeyValueKey, encr.KeyValue, 0, encr.KeyValue.Length, encr.SaltValue, ms);
            encr.EncryptedKeyValue = ms.ToArray();

            xml = encryptionInfo.Xml.OuterXml;

            var byXml = Encoding.UTF8.GetBytes(xml);

            ms = new MemoryStream();
            ms.Write(BitConverter.GetBytes((ushort)4), 0, 2);  //Major Version
            ms.Write(BitConverter.GetBytes((ushort)4), 0, 2);  //Minor Version
            ms.Write(BitConverter.GetBytes((uint)0x40), 0, 4); //Reserved
            ms.Write(byXml, 0, byXml.Length);

            var doc = new CompoundDocument();

            //Add the dataspace streams
            CreateDataSpaces(doc);
            //EncryptionInfo...
            doc.Storage.DataStreams.Add("EncryptionInfo", ms.ToArray());
            //...and the encrypted package
            doc.Storage.DataStreams.Add("EncryptedPackage", encrData);

            ms = new MemoryStream();
            var e = doc.Save();

            ms.Write(e, 0, e.Length);
            return(ms);
        }
Пример #11
0
        internal void Save()
        {
            if (Validate())
            {
                CompoundDocument doc = new CompoundDocument(this._wb._package.tempFolder);
                doc.Storage = new CompoundDocument.StoragePart();
                var store = new CompoundDocument.StoragePart();
                doc.Storage.SubStorage.Add("VBA", store);

                store.DataStreams.Add("_VBA_PROJECT", new MemoryStream(CreateVBAProjectStream()));
                store.DataStreams.Add("dir", new MemoryStream(CreateDirStream()));
                foreach (var module in Modules)
                {
                    store.DataStreams.Add(module.Name, new MemoryStream(VBACompression.CompressPart(Encoding.GetEncoding(CodePage).GetBytes(module.Attributes.GetAttributeText() + module.Code))));
                }

                //Copy streams from the template, if used.
                if (Document != null)
                {
                    foreach (var ss in Document.Storage.SubStorage)
                    {
                        if (ss.Key != "VBA")
                        {
                            doc.Storage.SubStorage.Add(ss.Key, ss.Value);
                        }
                    }
                    foreach (var s in Document.Storage.DataStreams)
                    {
                        if (s.Key != "dir" && s.Key != "PROJECT" && s.Key != "PROJECTwm")
                        {
                            doc.Storage.DataStreams.Add(s.Key, s.Value);
                        }
                    }
                }

                doc.Storage.DataStreams.Add("PROJECT", new MemoryStream(CreateProjectStream()));
                doc.Storage.DataStreams.Add("PROJECTwm", new MemoryStream(CreateProjectwmStream()));

                if (Part == null)
                {
                    Uri  = new Uri(PartUri, UriKind.Relative);
                    Part = _pck.CreatePart(Uri, ExcelPackage.schemaVBA);
                    var rel = _wb.Part.CreateRelationship(Uri, Packaging.TargetMode.Internal, schemaRelVba);
                }
                var st = Part.GetStream(FileMode.Create);
                doc.Save(st);
                st.Flush();

                /*TODO Stream fs = null;
                 * try
                 * {
                 *  fs = new FileStream(_pck.GetTempFile(), FileMode.Create);
                 *  doc.Save(fs);
                 * }
                 * finally
                 * {
                 *  if (fs != null)
                 *      fs.Dispose();
                 * }
                 * Stream st = Part.GetStream(FileMode.Create);
                 * ExcelPackage.CopyStream(fs, st);
                 */
                //Save the digital signture
                Signature.Save(this);
            }
        }
Пример #12
0
        internal void Save()
        {
            if (Validate())
            {
                CompoundDocument doc = new CompoundDocument();
                doc.Storage = new CompoundDocument.StoragePart();
                var store = new CompoundDocument.StoragePart();
                doc.Storage.SubStorage.Add("VBA", store);

                store.DataStreams.Add("_VBA_PROJECT", CreateVBAProjectStream());
                store.DataStreams.Add("dir", CreateDirStream());
                foreach (var module in Modules)
                {
                    store.DataStreams.Add(module.Name, CompoundDocument.CompressPart(Encoding.GetEncoding(CodePage).GetBytes(module.Attributes.GetAttributeText() + module.Code)));
                }

                //Copy streams from the template, if used.
                if (Document != null)
                {
                    foreach (var ss in Document.Storage.SubStorage)
                    {
                        if (ss.Key != "VBA")
                        {
                            doc.Storage.SubStorage.Add(ss.Key, ss.Value);
                        }
                    }
                    foreach (var s in Document.Storage.DataStreams)
                    {
                        if (s.Key != "dir" && s.Key != "PROJECT" && s.Key != "PROJECTwm")
                        {
                            doc.Storage.DataStreams.Add(s.Key, s.Value);
                        }
                    }
                }

                doc.Storage.DataStreams.Add("PROJECT", CreateProjectStream());
                doc.Storage.DataStreams.Add("PROJECTwm", CreateProjectwmStream());

                if (Part == null)
                {
                    Uri = new Uri(PartUri, UriKind.Relative);
                    Part = _pck.CreatePart(Uri, ExcelPackage.schemaVBA);
                    var rel = _wb.Part.CreateRelationship(Uri, TargetMode.Internal, schemaRelVba);
                }
                var vbaBuffer=doc.Save();
                var st = Part.GetStream(FileMode.Create);
                st.Write(vbaBuffer, 0, vbaBuffer.Length);
                st.Flush();
                st.Close();
                //Save the digital signture
                Signature.Save(this);
            }
        }