You need to read up on Fair Use. I am quoting from:
https://en.wikipedia.org/wiki/Fair_use
In determining whether the use made of a work in any particular case is a fair use the factors to be considered shall include:
- The purpose and character of the use, including whether such use is of a commercial nature or is for nonprofit educational purposes;
- the nature of the copyrighted work;
- the amount and substantiality of the portion used in relation to the copyrighted work as a whole; and
- the effect of the use upon the potential market for or value of the copyrighted work.
In determining (1) [...] The Campbell case also addressed the subfactor mentioned in the quotation above, "whether such use is of a commercial nature or is for nonprofit educational purposes." In an earlier case, Sony Corp. of America v. Universal City Studios, Inc., the Supreme Court had stated that "every commercial use of copyrighted material is presumptively ... unfair." In Campbell, the court clarified that this is not a "hard evidentiary presumption" and that even the tendency that commercial purpose will "weigh against a finding of fair use ... will vary with the context." The Campbell court held that hip-hop group 2 Live Crew's parody of the song "Oh, Pretty Woman" was fair use, even though the parody was sold for profit. Thus, having a commercial purpose does not preclude a use from being found fair, even though it makes it less likely.
I would recommend against it. If the work is under copyright and the first or last line is not generic (meaning, it cannot be found in any other commercial work), then you are violating copyright for a patently commercial purpose, and if you are sued you will probably lose.
Make up fake titles and make up fake lines, or if you don't think you can, stick to the public domain works. In fact you can make that part of your story, all the secret message are from famous books a century old and still available. Suggesting somebody wants to make sure their message can be decoded just about anytime, anywhere.
Don't steal. Your creativity must stand entirely on its own; don't ever try to borrow any great lines from film or literature or even songs unless they are in the public domain.