I recently came across a (not very well written) piece of writing in which "cipher" and "cypher" were used interchangeably. Is there any difference between them, or are they two spellings of the exact same word? If so, is it similar to the difference between the words "gray" and "grey", where one is more common in a certain geographic area?
The Oxford Dictionary gives both cipher and cypher in that order.
The preference for cipher over cypher seems to be a little greater in American English than in British English. The Corpus of Contemporary American English has just over three times as many records for cipher than it has for cypher. The British National Corpus has only one and a half times as many. It would not occur to me, as a speaker of British English, to use anything other than cypher.
Short answer: They are two valid spellings of the exact same word, but "cipher" is more common than "cypher".
I just came across this page, which is entitled It’s Cipher, Not Cypher. It says:
Where did the variants [of cipher] come from?
The word we know today as cipher originated in the late 14th century from the Arabic word sifr, meaning “zero.” At this point in the English language (Middle English) the spellings of words were not yet explicitly defined, and writers commonly substituted i‘s for y‘s at will, hence the emergence of cypher as a variant for cipher.
However, after the Great Vowel shift and the standardization of spelling in the 15th and 16th centuries, many of the y’s that denoted “eye” sounds in English were replaced by i’s–hence the change of “wyf” to “wife,” and “cypher” to “cipher.”
Even so, cypher is still considered a valid variant of cipher in many orthographic circles today. Cypher is most popular in England, where it first emerged.
Additionally, this graph from Peter Shor's comment shows how "cypher" was the preferred word until the early 19th century, when "cipher" became more popular. This page shows that almost every country nowadays uses "cipher" over "cypher", with the exceptions being Ghana and Nigeria (and that's probably just from a lack of enough data).
I have never come across cypher and I suspect that spelling is influenced by the y in cyber. Having said that, I do see one of the dictionaries I checked (Collins) giving cypher as an alternative.
In any case, you probably want to stick to cipher.
Even etymologically, the medieval Latin spelling was ciphra (from the Arabic sifr, which ultimately derives from Sanskrit śūnya!). So cypher even if used, is a less logical spelling for this word.
In contrast gray/grey seems to be a more established transatlantic (UK-US) divergence.