コード例 #1
0
        private static bool TryGetEncryptedPackage(Stream fileStream, CompoundDocument document, string password, out Stream stream)
        {
            var encryptedPackage = document.FindEntry(DirectoryEntryEncryptedPackage);
            var encryptionInfo   = document.FindEntry(DirectoryEntryEncryptionInfo);

            if (encryptedPackage == null || encryptionInfo == null)
            {
                stream = null;
                return(false);
            }

            var infoBytes  = document.ReadStream(fileStream, encryptionInfo.StreamFirstSector, (int)encryptionInfo.StreamSize, encryptionInfo.IsEntryMiniStream);
            var encryption = EncryptionInfo.Create(infoBytes);

            if (encryption.VerifyPassword("VelvetSweatshop"))
            {
                // Magic password used for write-protected workbooks
                password = "******";
            }
            else if (password == null || !encryption.VerifyPassword(password))
            {
                throw new InvalidPasswordException(Errors.ErrorInvalidPassword);
            }

            var secretKey     = encryption.GenerateSecretKey(password);
            var packageStream = document.CreateStream(fileStream, encryptedPackage.StreamFirstSector, (int)encryptedPackage.StreamSize, encryptedPackage.IsEntryMiniStream);

            stream = encryption.CreateEncryptedPackageStream(packageStream, secretKey);
            return(true);
        }
コード例 #2
0
        internal XlsBiffFilePass(byte[] bytes, uint offset, int biffVersion)
            : base(bytes, offset)
        {
            if (biffVersion >= 2 && biffVersion <= 5)
            {
                // Cipher = EncryptionType.XOR;
                var encryptionKey = ReadUInt16(0);
                var hashValue     = ReadUInt16(2);
                EncryptionInfo = EncryptionInfo.Create(encryptionKey, hashValue);
            }
            else
            {
                ushort type = ReadUInt16(0);

                if (type == 0)
                {
                    var encryptionKey = ReadUInt16(2);
                    var hashValue     = ReadUInt16(4);
                    EncryptionInfo = EncryptionInfo.Create(encryptionKey, hashValue);
                }
                else if (type == 1)
                {
                    var encryptionInfo = new byte[bytes.Length - 6]; // 6 = 4 + 2 = biffVersion header + filepass enryptiontype
                    Array.Copy(bytes, 6, encryptionInfo, 0, bytes.Length - 6);
                    EncryptionInfo = EncryptionInfo.Create(encryptionInfo);
                }
                else
                {
                    throw new NotSupportedException("Unknown encryption type: " + type);
                }
            }
        }