Negotiating with an editor after a failure to review a paper honestly Suppose the first paragraph of a paper submitted to a journal says "We prove that the Riemann hypothesis follows from Catholic doctrines concerning sacraments [etc.]" and the referee's report accompanying the rejection says "The author's purpose is to prove the P =/= NP conjecture&nbsp;.&nbsp;.&nbsp;.&nbsp;" etc. and explains that that fails.

How reasonable would it be to argue that the journal should reconsider on the grounds that the paper was not honestly reviewed? And in so doing, how would one avoid offending the editor who probably holds the referee in high esteem and has never heard of the author?
How reasonable would it be to argue that the journal should reconsider on the grounds that the paper was not honestly reviewed? 


If I were you I would really, really, really avoid bringing anything to do with "honesty" into the discussion. Seriously, it's unprofessional and off-putting to question the integrity of people who may have a much higher reputation and level of credibility than you (and you don't even know the identity of the person you're accusing... not a good position to be in). However, I think it would be extremely reasonable to politely point out the mistake and make the case that the reviewer's misconception of what the paper does is severe enough so as to invalidate the conclusion of the review and justify giving the paper a fresh look. 

Now, whether it is also reasonable to expect your argument to sway the editor's decision is a separate question. Personally I wouldn't be too optimistic, but it's worth a try, and if you phrase your rebuttal politely and professionally then I don't think you are risking anything other than a bit of time and a minor disappointment.

I also agree with a point made in the comments that it would be worth taking a fresh look at your paper to see to what extent a lack of clarity in your writing may have contributed to the reviewer's mistake. If you find any way to improve the presentation in a way that minimizes the chances for this sort of misunderstanding, do so, and include a revised version of your manuscript when you submit the rebuttal, and explain the changes you've made to eliminate the chances for a misunderstanding.


  And in so doing, how would one avoid offending the editor who probably holds the referee in high esteem and has never heard of the author?


I don't see why the editor should be offended, and also think it should be possible to not offend even the reviewer if your email is polite and sticks to facts. Mostly, just don't say anything offensive (like questioning people's honesty!) and you should be fine. Here's one way you might do it:


  Dear Editor,
  
  Thank you for sending me the referee report for my paper and the decision regarding my submission. While I appreciate the consideration given to my paper by yourself and the anonymous referee, I wanted to point out what appears to be a misguided interpretation of my paper's results by the referee. They claim that I try and fail to prove the conjecture that P=/=NP, but in fact my claim is that I have proved the Riemann Hypothesis, which is a very different conjecture. The difference between those two claims is that [insert very brief explanation here if necessary] and is well-known to experts in the area. I believe that my reasoning is solid and have checked the paper to the best of my ability, but of course a serious examination of my paper would be necessary in order for a fair and correct decision to be made regarding whether the paper should be published by your journal. The referee's mischaracterization of my claims obviously calls the conclusion of the review into question. 
  
  I therefore kindly request that you send the paper to be refereed again, either by the same referee with a request that he or she rethink their opinion in view of their earlier misunderstanding, or to a different referee who is qualified to provide a serious opinion on the paper.
  
  I also wanted to mention that I have made a few small changes to paragraphs X, Y, Z in Section A to try to make the statement of my results clearer and minimize the chances that other referees or readers of my paper will misunderstand my claims. The revised manuscript is attached.
  
  [Yours etc.]