예제 #1
0
        /// <summary>
        /// 解密提取package
        /// </summary>
        /// <param name="stream">内存流 </param>
        /// <param name="encryption">加密对象</param>
        /// <returns></returns>
        internal MemoryStream DecryptPackage(MemoryStream stream, ExcelEncryption encryption)
        {
            //创建lockBytes对象。
            ILockBytes lb = GetLockbyte(stream);

            ExceptionHelper.TrueThrow <Exception>(StgIsStorageILockBytes(lb) != 0, "不是加密文件");

            MemoryStream ret = null;

            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);

            Marshal.ReleaseComObject(lb);

            return(ret);
        }
예제 #2
0
        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 (res == 0)
            {
                byte[]         data;
                EncryptionInfo encryptionInfo = null;
                while (res != 1)
                {
                    switch (statstg.pwcsName)
                    {
                    case "EncryptionInfo":
                        data = GetOleStream(storage, statstg);

                        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);
        }
예제 #3
0
        /// <summary>
        /// 解密提取Package
        /// </summary>
        /// <param name="fi">文件</param>
        /// <param name="encryption"></param>
        /// <returns></returns>
        internal MemoryStream DecryptPackage(FileInfo fi, ExcelEncryption encryption)
        {
            ExceptionHelper.TrueThrow(StgIsStorageFile(fi.FullName) != 0, "文件{0}不是一个加密包", fi.FullName);
            MemoryStream ret     = null;
            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);
            }

            return(ret);
        }
		/// <summary>
		/// 解密提取Package
		/// </summary>
		/// <param name="fi">文件</param>
		/// <param name="encryption"></param>
		/// <returns></returns>
		internal MemoryStream DecryptPackage(FileInfo fi, ExcelEncryption encryption)
		{
			ExceptionHelper.TrueThrow(StgIsStorageFile(fi.FullName) != 0, "文件{0}不是一个加密包", fi.FullName);
			MemoryStream ret = null;
			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);
			}

			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 (res == 0)
			{
				byte[] data;
				EncryptionInfo encryptionInfo = null;
				while (res != 1)
				{
					switch (statstg.pwcsName)
					{
						case "EncryptionInfo":
							data = GetOleStream(storage, statstg);

							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>
		/// 加密Package
		/// </summary>
		/// <param name="package">作为一个字节数组封装</param>
		/// <param name="encryption">加密信息</param>
		/// <returns></returns>
		internal MemoryStream EncryptPackage(byte[] package, ExcelEncryption encryption)
		{
			byte[] encryptionKey;
			//创建加密信息。这也返回了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;

			//创建内存中的文件
			if (StgCreateDocfileOnILockBytes(lb,
					STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE | STGM.TRANSACTED,
					0,
					out storage) == 0)
			{
				//创建数据空间存储
				CreateDataSpaces(storage);

				//创建加密信息流
				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;

				//加密包
				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);

				//文件大小
				MemoryStream ms = new MemoryStream();
				BinaryWriter bw = new BinaryWriter(ms);
				bw.Write((ulong)package.LongLength);
				bw.Flush();
				byte[] length = ms.ToArray();
				//先写加密的数据长度
				stream.Write(length, length.Length, IntPtr.Zero);
				//现在加密的数据
				stream.Write(encryptedPackage, encryptedPackage.Length, IntPtr.Zero);
				stream = null;
				storage.Commit(0);
				lb.Flush();

				//复制非托管的流为字节数组
				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>
		/// 解密提取package
		/// </summary>
		/// <param name="stream">内存流 </param>
		/// <param name="encryption">加密对象</param>
		/// <returns></returns>
		internal MemoryStream DecryptPackage(MemoryStream stream, ExcelEncryption encryption)
		{
			//创建lockBytes对象。
			ILockBytes lb = GetLockbyte(stream);

			ExceptionHelper.TrueThrow<Exception>(StgIsStorageILockBytes(lb) != 0, "不是加密文件");

			MemoryStream ret = null;

			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);

			Marshal.ReleaseComObject(lb);

			return ret;
		}
예제 #8
0
        /// <summary>
        /// 加密Package
        /// </summary>
        /// <param name="package">作为一个字节数组封装</param>
        /// <param name="encryption">加密信息</param>
        /// <returns></returns>
        internal MemoryStream EncryptPackage(byte[] package, ExcelEncryption encryption)
        {
            byte[] encryptionKey;
            //创建加密信息。这也返回了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;

            //创建内存中的文件
            if (StgCreateDocfileOnILockBytes(lb,
                                             STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE | STGM.TRANSACTED,
                                             0,
                                             out storage) == 0)
            {
                //创建数据空间存储
                CreateDataSpaces(storage);

                //创建加密信息流
                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;

                //加密包
                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);

                //文件大小
                MemoryStream ms = new MemoryStream();
                BinaryWriter bw = new BinaryWriter(ms);
                bw.Write((ulong)package.LongLength);
                bw.Flush();
                byte[] length = ms.ToArray();
                //先写加密的数据长度
                stream.Write(length, length.Length, IntPtr.Zero);
                //现在加密的数据
                stream.Write(encryptedPackage, encryptedPackage.Length, IntPtr.Zero);
                stream = null;
                storage.Commit(0);
                lb.Flush();

                //复制非托管的流为字节数组
                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);
        }