Example #1
0
        public XlsBiffStream(Stream baseStream, int offset = 0, int explicitVersion = 0, string password = null, byte[] secretKey = null, EncryptionInfo encryption = null)
        {
            BaseStream = baseStream;
            Position   = offset;

            var bof = Read() as XlsBiffBOF;

            if (bof != null)
            {
                BiffVersion = explicitVersion == 0 ? GetBiffVersion(bof) : explicitVersion;
                BiffType    = bof.Type;
            }

            CipherBlock = -1;
            if (secretKey != null)
            {
                SecretKey  = secretKey;
                Encryption = encryption;
                Cipher     = Encryption.CreateCipher();
            }
            else
            {
                var filePass = Read() as XlsBiffFilePass;
                if (filePass == null)
                {
                    filePass = Read() as XlsBiffFilePass;
                }

                if (filePass != null)
                {
                    Encryption = filePass.EncryptionInfo;

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

                    SecretKey = Encryption.GenerateSecretKey(password);
                    Cipher    = Encryption.CreateCipher();
                }
            }

            Position = offset;
        }