How a license key survives a typo
A support queue tells you everything about your key format. If it fills with "my key doesn't work," and half of those keys turn out to be one character wrong - a typed O where the key had a 0, a transposed pair, a dropped dash - your format is making customers do a job the computer should do.
The fix is old and well understood, and it costs you about forty lines of code: encode the key so the ambiguous characters never appear, and append a check symbol that catches the typo locally, before anything talks to a server.
The real question
A license key is bytes. A customer is a human with a keyboard. The question is how to carry the first across the second without the two most common human errors - one wrong character, and two adjacent characters swapped - turning into a failed activation and a support ticket.
We want the application to reject a mistyped key immediately, with a clear "you mistyped this," and reserve the real failure ("this key is not valid") for keys that were typed correctly.
The answer has three parts: a human-friendly alphabet, dashed grouping, and a check symbol. None of it is cryptography. It sits around the cryptography.
Crockford Base32: an alphabet without ambiguity
Hexadecimal wastes the letters; raw Base64 has +, /, case sensitivity, and 0 / O / l / 1 collisions that are murder to dictate over the phone. Crockford Base32 was designed for exactly this: 32 symbols, 0-9 and A-Z with the four worst offenders - I, L, O, U - removed.
I and L look like 1; O looks like 0; U is dropped so the encoding never accidentally spells something. Everything is uppercase, and there is no padding.
const string Alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // 32 symbols, no I L O U
const string CheckAlphabet = Alphabet + "*~$=U"; // 37 symbols, for the check only
public static string Encode(byte[] data)
{
var sb = new StringBuilder();
int buffer = 0, bits = 0;
foreach (byte b in data)
{
buffer = (buffer << 8) | b;
bits += 8;
while (bits >= 5)
{
bits -= 5;
sb.Append(Alphabet[(buffer >> bits) & 0x1F]);
}
buffer &= (1 << bits) - 1; // keep only the leftover low bits
}
if (bits > 0)
sb.Append(Alphabet[(buffer << (5 - bits)) & 0x1F]);
return sb.ToString();
}
The decode side does the inverse, five bits at a time - but first it normalizes what the human typed. This is where the removed letters earn their keep: if someone types O, we read it as 0; if they type I or L, we read it as 1. Case is folded. A key dictated over a bad phone line still decodes.
static int SymbolValue(char c)
{
c = char.ToUpperInvariant(c);
return c switch
{
'O' => 0, // operator typed the letter O for zero
'I' or 'L' => 1, // letter I or L for one
_ => Alphabet.IndexOf(c) is var v && v >= 0 ? v : throw new FormatException($"illegal symbol '{c}'")
};
}
public static byte[] Decode(string symbols)
{
var bytes = new List<byte>();
int buffer = 0, bits = 0;
foreach (char c in symbols)
{
buffer = (buffer << 5) | SymbolValue(c);
bits += 5;
if (bits >= 8)
{
bits -= 8;
bytes.Add((byte)((buffer >> bits) & 0xFF));
}
buffer &= (1 << bits) - 1;
}
return bytes.ToArray();
}
Normalization is error correction for a known, fixed class of confusions. The check symbol, next, is error detection for everything else.
The check symbol: one extra character that catches the typo
Append one symbol computed from all the others, and a single wrong character changes it. The arithmetic is the Crockford check: treat the encoded symbols as a base-32 number and take it modulo 37. Thirty-seven is prime and larger than the 32-symbol alphabet, which is precisely what makes the guarantees hold.
The result is 0-36, so the check symbol needs five extra glyphs (*~$=U) for the values a data symbol can't represent - they only ever appear in the check position.
public static char CheckSymbol(string symbols) // the data symbols, de-chunked, no check
{
int acc = 0;
foreach (char c in symbols)
acc = (acc * 32 + SymbolValue(c)) % 37;
return CheckAlphabet[acc];
}
Why does a prime modulus catch the two common errors? A single wrong symbol at position i shifts the running value by delta * 32^i, where delta is the nonzero difference between the right and wrong symbol values. Modulo 37 that product is never zero - 37 is prime, |delta| < 37, and powers of 32 are never 0 mod 37 - so the check symbol is guaranteed to differ.
An adjacent swap shifts the value by (a - b) * 32^i * (1 - 32); since 1 - 32 ≡ 6 (mod 37) is nonzero, that product is zero only when a == b, i.e. when nothing actually moved. Both error classes are caught, every time.
(This is the same reasoning behind ISO 7064 Mod 37,2, which you can substitute if you prefer a standardized scheme.)
The key is a transport envelope. Encoding and the check symbol wrap the signed token on the way out; on the way in, the check rejects a mistyped key locally before the token is ever decoded or verified.
A worked example
Take a 15-byte payload. Base32 gives 24 symbols; the check symbol computed over them is E; grouping into blocks of five and inserting dashes gives the key the customer sees:
9DJQJ-WGHKG-PT1WT-RG0NY-C100E
Now mistype it. Change the G at position 7 to an H - 9DJQJ-WHHKG-… - and recomputing the check over the altered symbols yields X, not E. Rejected. Swap the J and Q in the first block - 9DQJJ-WGHKG-… - and the check becomes R. Rejected.
In both cases the application says "that key is mistyped" without a network call, without touching the signature, without even finishing the decode.
Formatting and parsing tie it together. Note the order in TryParse: the check is verified before the payload is decoded.
public static string Format(byte[] payload)
{
string full = Encode(payload);
full += CheckSymbol(full);
var sb = new StringBuilder();
for (int i = 0; i < full.Length; i++)
{
if (i > 0 && i % 5 == 0)
sb.Append('-');
sb.Append(full[i]);
}
return sb.ToString();
}
public static bool TryParse(string typed, out byte[] payload)
{
payload = Array.Empty<byte>();
string s = new string(typed.Where(c => c != '-' && !char.IsWhiteSpace(c)).ToArray());
if (s.Length < 2) return false;
string data = s[..^1];
char given = char.ToUpperInvariant(s[^1]);
if (CheckSymbol(data) != given) return false; // typo caught here - before any decode
payload = Decode(data);
return true;
}
How this applies in Keyright
The key your customer types is not the license. It is a transport envelope. Inside the decoded bytes is the real artifact: a signed license token - a compact set of claims (product, edition, expiry, entitlements) plus an Ed25519 or ECDSA signature your application verifies offline with an embedded public key.
That signature is what makes the token trustworthy and unforgeable; we covered it in Anatomy of a signed license key.
The layering is clean, and each layer has exactly one job:
- Crockford Base32 + dashes make the token dictatable and pasteable.
- The check symbol catches human error before the token is even decoded.
- The signature inside the token provides integrity and authenticity.
A Keyright integrator issues keys with the service's SDK and verifies them in-process, so the typo check runs on the customer's machine with no round-trip. Only after TryParse succeeds and the signature verifies does Keyright read the claims and apply them - the paid editions, or the capped free Community Edition fallback.
The envelope and the token are independent: you can change the grouping, the block size, even the alphabet, without touching how the token is signed or verified.
When it breaks
Be honest about the limits, because they are the whole point of knowing where the boundary is.
- Detection, not correction. The check symbol tells you that a key is wrong, not which character or what it should be. It flags; the human retypes. Do not try to auto-repair from it.
- One error, or one adjacent swap. The mod-37 guarantee covers exactly those. Two independent wrong characters, a non-adjacent transposition, or several errors at once can - rarely - collide to the same check symbol (roughly a 1-in-37 chance for random multi-error garbage). The check is a cheap first filter, not a proof of correctness.
- It is not integrity. The check symbol is public arithmetic; anyone can recompute it. It stops mistakes, not tampering. An attacker who edits the payload simply recomputes a matching check symbol. Integrity comes only from the signature inside the token - never conflate the two.
- Mind the special glyphs. The check symbol can be one of
*~$=U. Allow for them in the final position of your input field and don't strip them as punctuation, or a legitimate key with a$check will fail to parse.
Takeaway
A typeable license key is an envelope problem, not a crypto problem. Pick an alphabet with no ambiguous characters, normalize the few that remain on input, group with dashes so the eye can track position, and append one mod-37 check symbol so the most common human errors fail fast and locally.
The cryptographic weight stays where it belongs - in the signed token inside. Get the envelope right and your support queue stops filling with keys that were never wrong, only mistyped.
Comments
No comments yet. Start the discussion.