Cipher CTF Challenge Generator (Cardan Grille cipher)Press the blue button to regenerate a Cardan Grille challenge. |
Coding
The Cardan Grille cipher is named after Girolamo Cardano, who used in the 16th Century to write secret messages. It uses a grid to reveal a secret message. In this way, Bob and Alice would share a secret mask that would reveal the secret message. Bob starts with his message and lays down the mask. Next, he will remove the mask, and fill-in the rest of the words with random letters. When Alice receives the cipher message, she will simply apply the secret mask to reveal the message.
For example:
Bob is using a Cardan Grille cipher left a secret message for Alice. He is using a mask of X_X__XX__X_XX_X_X__X_X__X and has a cipher of uhaapzwpyhbtoifrnthudsayi. Can you find the secret message?
We get:
uhaapzwpyhbtoifrnthudsayi X_X__XX__X_XX_X_X__X_X__X h ap py b i r th d ay
The basic coding in C# is:
public static (string, string) getCardan(string s, string mask) { string chars = "abcdefghijklmnopqrstuvwxyz"; var rnd = new System.Random(); mask = mask.ToUpper(); while (countOccurences("_", mask) <= s.Length) { mask = mask + "_"; } s = s.ToLower(); string rtn = ""; int i = 0; int j = 0; foreach (char ch in mask) { if (mask[i] == 'X') rtn = rtn + chars[rnd.Next(26)]; else { if (j >= s.Length) break; rtn = rtn + s[j]; j++; } i++; } return (rtn, mask.Substring(0, i)); }