/// <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. ILockBytes lb = GetLockbyte(stream); MemoryStream ret = null; if (StgIsStorageILockBytes(lb) == 0) { IStorage storage = null; if (StgOpenStorageOnILockBytes( lb, null, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out storage) == 0) { ret = GetStreamFromPackage(storage, encryption); } Marshal.ReleaseComObject(storage); } else { throw (new InvalidDataException("The stream is not an encrypted package")); } Marshal.ReleaseComObject(lb); return(ret); }
/// <summary> /// Read the package from the OLE document and decrypt it using the supplied password /// </summary> /// <param name="fi">The file</param> /// <param name="encryption"></param> /// <returns></returns> internal MemoryStream DecryptPackage(FileInfo fi, ExcelEncryption encryption) { MemoryStream ret = null; if (StgIsStorageFile(fi.FullName) == 0) { IStorage storage = null; if (StgOpenStorage( fi.FullName, null, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out storage) == 0) { ret = GetStreamFromPackage(storage, encryption); Marshal.ReleaseComObject(storage); } } else { throw(new InvalidDataException(string.Format("File {0} is not an encrypted package", fi.FullName))); } return(ret); }
private MemoryStream GetStreamFromPackage(IStorage storage, ExcelEncryption encryption) { MemoryStream ret = null; comTypes.STATSTG statstg; storage.Stat(out statstg, (uint)STATFLAG.STATFLAG_DEFAULT); IEnumSTATSTG pIEnumStatStg = null; storage.EnumElements(0, IntPtr.Zero, 0, out pIEnumStatStg); comTypes.STATSTG[] regelt = { statstg }; uint fetched = 0; uint res = pIEnumStatStg.Next(1, regelt, out fetched); //if (regelt[0].pwcsName == "DataSpaces") //{ // PrintStorage(storage, regelt[0],""); //} if (res == 0) { byte[] data; EncryptionInfo encryptionInfo = null; while (res != 1) { switch (statstg.pwcsName) { case "EncryptionInfo": data = GetOleStream(storage, statstg); //File.WriteAllBytes(@"c:\temp\EncInfo1.bin", data); encryptionInfo = new EncryptionInfo(); encryptionInfo.ReadBinary(data); encryption.Algorithm = encryptionInfo.Header.AlgID == AlgorithmID.AES128 ? EncryptionAlgorithm.AES128 : encryptionInfo.Header.AlgID == AlgorithmID.AES192 ? EncryptionAlgorithm.AES192 : EncryptionAlgorithm.AES256; break; case "EncryptedPackage": data = GetOleStream(storage, statstg); ret = DecryptDocument(data, encryptionInfo, encryption.Password); break; } if ((res = pIEnumStatStg.Next(1, regelt, out fetched)) != 1) { statstg = regelt[0]; } } } Marshal.ReleaseComObject(pIEnumStatStg); return(ret); }
private MemoryStream GetStreamFromPackage(IStorage storage, ExcelEncryption encryption) { MemoryStream ret=null; comTypes.STATSTG statstg; storage.Stat(out statstg, (uint)STATFLAG.STATFLAG_DEFAULT); IEnumSTATSTG pIEnumStatStg = null; storage.EnumElements(0, IntPtr.Zero, 0, out pIEnumStatStg); comTypes.STATSTG[] regelt = { statstg }; uint fetched = 0; uint res = pIEnumStatStg.Next(1, regelt, out fetched); //if (regelt[0].pwcsName == "DataSpaces") //{ // PrintStorage(storage, regelt[0],""); //} if (res == 0) { byte[] data; EncryptionInfo encryptionInfo = null; while (res != 1) { switch (statstg.pwcsName) { case "EncryptionInfo": data = GetOleStream(storage, statstg); //File.WriteAllBytes(@"c:\temp\EncInfo1.bin", data); encryptionInfo = new EncryptionInfo(); encryptionInfo.ReadBinary(data); encryption.Algorithm = encryptionInfo.Header.AlgID == AlgorithmID.AES128 ? EncryptionAlgorithm.AES128 : encryptionInfo.Header.AlgID == AlgorithmID.AES192 ? EncryptionAlgorithm.AES192 : EncryptionAlgorithm.AES256; break; case "EncryptedPackage": data = GetOleStream(storage, statstg); ret = DecryptDocument(data, encryptionInfo, encryption.Password); break; } if ((res = pIEnumStatStg.Next(1, regelt, out fetched)) != 1) { statstg = regelt[0]; } } } Marshal.ReleaseComObject(pIEnumStatStg); return ret; }
/// <summary> /// Encrypts a package /// </summary> /// <param name="package">The package as a byte array</param> /// <param name="encryption">The encryption info from the workbook</param> /// <returns></returns> internal MemoryStream EncryptPackage(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; //Create the document in-memory if (StgCreateDocfileOnILockBytes(lb, STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE | STGM.TRANSACTED, 0, out storage)==0) { //First create the dataspace storage CreateDataSpaces(storage); //Create the Encryption info Stream comTypes.IStream stream; storage.CreateStream("EncryptionInfo", (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), (uint)0, (uint)0, out stream); byte[] ei=encryptionInfo.WriteBinary(); stream.Write(ei, ei.Length, IntPtr.Zero); stream = null; //Encrypt the package byte[] encryptedPackage=EncryptData(encryptionKey, package, false); storage.CreateStream("EncryptedPackage", (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), (uint)0, (uint)0, out stream); //Write Size here MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); bw.Write((ulong)package.LongLength); bw.Flush(); byte[] length = ms.ToArray(); //Write Encrypted data length first as an unsigned long stream.Write(length, length.Length, IntPtr.Zero); //And now the Encrypted data stream.Write(encryptedPackage, encryptedPackage.Length, IntPtr.Zero); stream = null; storage.Commit(0); lb.Flush(); //Now copy the unmanaged stream to a byte array --> memory stream var statstg = new comTypes.STATSTG(); lb.Stat(out statstg, 0); int size = (int)statstg.cbSize; IntPtr buffer = Marshal.AllocHGlobal(size); UIntPtr readSize; byte[] pack=new byte[size]; lb.ReadAt(0, buffer, size, out readSize); Marshal.Copy(buffer, pack, 0, size); Marshal.FreeHGlobal(buffer); ret = new MemoryStream(); ret.Write(pack, 0, size); } Marshal.ReleaseComObject(storage); Marshal.ReleaseComObject(lb); return ret; }
/// <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. ILockBytes lb = GetLockbyte(stream); MemoryStream ret = null; if (StgIsStorageILockBytes(lb) == 0) { IStorage storage = null; if (StgOpenStorageOnILockBytes( lb, null, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out storage) == 0) { ret = GetStreamFromPackage(storage, encryption); } Marshal.ReleaseComObject(storage); } else { throw (new Exception("The stream is not an encrypted package")); } Marshal.ReleaseComObject(lb); return ret; }
/// <summary> /// Read the package from the OLE document and decrypt it using the supplied password /// </summary> /// <param name="fi">The file</param> /// <param name="encryption"></param> /// <returns></returns> internal MemoryStream DecryptPackage(FileInfo fi, ExcelEncryption encryption) { MemoryStream ret = null; if (StgIsStorageFile(fi.FullName) == 0) { IStorage storage = null; if (StgOpenStorage( fi.FullName, null, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out storage) == 0) { ret = GetStreamFromPackage(storage, encryption); Marshal.ReleaseComObject(storage); } } else { throw(new Exception(string.Format("File {0} is not an encrypted package",fi.FullName))); } return ret; }
/// <summary> /// Encrypts a package /// </summary> /// <param name="package">The package as a byte array</param> /// <param name="encryption">The encryption info from the workbook</param> /// <returns></returns> internal MemoryStream EncryptPackage(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; //Create the document in-memory if (StgCreateDocfileOnILockBytes(lb, STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE | STGM.TRANSACTED, 0, out storage) == 0) { //First create the dataspace storage CreateDataSpaces(storage); //Create the Encryption info Stream comTypes.IStream stream; storage.CreateStream("EncryptionInfo", (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), (uint)0, (uint)0, out stream); byte[] ei = encryptionInfo.WriteBinary(); stream.Write(ei, ei.Length, IntPtr.Zero); stream = null; //Encrypt the package byte[] encryptedPackage = EncryptData(encryptionKey, package, false); storage.CreateStream("EncryptedPackage", (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), (uint)0, (uint)0, out stream); //Write Size here MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); bw.Write((ulong)package.LongLength); bw.Flush(); byte[] length = ms.ToArray(); //Write Encrypted data length first as an unsigned long stream.Write(length, length.Length, IntPtr.Zero); //And now the Encrypted data stream.Write(encryptedPackage, encryptedPackage.Length, IntPtr.Zero); stream = null; storage.Commit(0); lb.Flush(); //Now copy the unmanaged stream to a byte array --> memory stream var statstg = new comTypes.STATSTG(); lb.Stat(out statstg, 0); int size = (int)statstg.cbSize; IntPtr buffer = Marshal.AllocHGlobal(size); UIntPtr readSize; byte[] pack = new byte[size]; lb.ReadAt(0, buffer, size, out readSize); Marshal.Copy(buffer, pack, 0, size); Marshal.FreeHGlobal(buffer); ret = new MemoryStream(); ret.Write(pack, 0, size); } Marshal.ReleaseComObject(storage); Marshal.ReleaseComObject(lb); return(ret); }