File size: 38,681 Bytes
b4f6a98 0ff7556 d473696 0ff7556 3a3ce41 346ef1a d473696 0ff7556 3a3ce41 346ef1a 0ff7556 b4f6a98 855192d 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 d473696 3a3ce41 b4f6a98 0ff7556 b4f6a98 0ff7556 d473696 346ef1a 3a3ce41 0ff7556 346ef1a 0ff7556 346ef1a b4f6a98 346ef1a 3a3ce41 b4f6a98 346ef1a 0ff7556 346ef1a b4f6a98 0ff7556 d473696 b4f6a98 0ff7556 346ef1a 0ff7556 b4f6a98 346ef1a 0ff7556 b4f6a98 346ef1a 0ff7556 b4f6a98 0ff7556 346ef1a 0ff7556 b4f6a98 346ef1a 0ff7556 346ef1a 0ff7556 b4f6a98 346ef1a 0ff7556 3a3ce41 0ff7556 3a3ce41 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 d473696 0ff7556 b4f6a98 0ff7556 b4f6a98 0ff7556 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 | from __future__ import annotations
import argparse
import json
import random
import re
from pathlib import Path
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
AFFINE_A_VALUES = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
# ---------------------------------------------------------------------------
# Attack-method and educational-note registries (extended for all new labels)
# ---------------------------------------------------------------------------
ATTACKS: dict[str, list[str]] = {
"plaintext": [],
"caesar_rot": ["brute_force_26", "frequency_analysis", "chi_squared_english"],
"caesar": ["brute_force_26", "frequency_analysis", "chi_squared_english"],
"rot13": ["brute_force_26", "self_inverse_check"],
"atbash": ["self_inverse_check", "frequency_analysis"],
"affine": ["brute_force_312", "frequency_analysis"],
"monoalphabetic": ["frequency_analysis", "pattern_words", "hill_climbing_lm"],
"substitution": ["frequency_analysis", "pattern_words", "hill_climbing_lm"],
"vigenere": ["kasiski_examination", "friedman_test", "index_of_coincidence"],
"beaufort": ["kasiski_examination", "friedman_test", "index_of_coincidence"],
"gronsfeld": ["kasiski_examination", "digit_key_brute_force"],
"autokey": ["running_key_analysis", "index_of_coincidence"],
"trithemius": ["progressive_shift_detection", "frequency_analysis"],
"porta": ["kasiski_examination", "index_of_coincidence"],
"rail_fence": ["rail_count_search", "anagram_recovery"],
"columnar": ["key_length_search", "anagram_recovery"],
"columnar_transposition":["key_length_search", "anagram_recovery"],
"scytale": ["column_count_search", "anagram_recovery"],
"double_transposition": ["key_length_search", "anagram_recovery"],
"stager_route": ["column_count_search", "route_pattern_search"],
# new sparse-label entries
"aeneas_tacticus": ["sequential_number_decode", "frequency_analysis"],
"arnold_andre": ["book_cipher_attack", "triple_index_search"],
"babington": ["nomenclator_frequency", "symbol_mapping"],
"bacon_cipher": ["five_bit_decode", "ab_frequency_analysis"],
"book_cipher": ["book_cipher_attack", "triple_index_search"],
"commercial_code": ["codebook_frequency", "five_letter_group_analysis"],
"culper_ring": ["codebook_frequency", "number_range_analysis"],
"homophonic": ["frequency_analysis", "multiple_ciphertext_attack"],
"morse_code": ["symbol_decode", "frequency_analysis"],
"navajo_code": ["codebook_frequency", "word_list_attack"],
"null_cipher": ["acrostic_detection", "first_letter_extraction"],
"one_time_pad": ["information_theoretic_proof", "key_reuse_attack"],
"pigpen": ["symbol_frequency", "grid_position_decode"],
"polybius": ["pair_decode", "frequency_analysis"],
"running_key": ["index_of_coincidence", "probable_plaintext"],
"tap_code": ["dot_count_decode", "grid_position_search"],
"vernam": ["key_reuse_attack", "information_theoretic_proof"],
"voynich_render": ["statistical_analysis", "unsolved"],
"wallis_cipher": ["number_substitution_decode", "frequency_analysis"],
"zimmermann": ["codebook_frequency", "number_group_analysis"],
}
EDU_NOTES: dict[str, str] = {
"plaintext": "No cipher applied. Useful as a negative-class baseline.",
"caesar_rot": "Caesar / ROT-N: single-shift monoalphabetic with only 26 keys.",
"caesar": "Caesar / ROT-N: single-shift monoalphabetic with only 26 keys.",
"rot13": "ROT-13 is Caesar with shift 13 — it is its own inverse.",
"atbash": "Atbash maps A↔Z, B↔Y, … and is its own inverse.",
"affine": "Affine: E(x)=(a·x+b) mod 26 — only 312 valid keys.",
"monoalphabetic": "Monoalphabetic substitution with an arbitrary 26-letter permutation.",
"substitution": "Monoalphabetic substitution with an arbitrary 26-letter permutation.",
"vigenere": "Vigenère: repeating-key Caesar; vulnerable to Kasiski and Friedman analysis.",
"beaufort": "Beaufort: E(x)=k-x mod 26 — reciprocal cipher (decryption identical to encryption).",
"gronsfeld": "Gronsfeld: Vigenère variant with a digit-only key (0–9 per position).",
"autokey": "Autokey: Vigenère where the plaintext extends the key — harder to Kasiski-attack.",
"trithemius": "Trithemius: progressive Caesar (shift = letter position); no key needed.",
"porta": "Porta: 13-row table polyalphabetic cipher; reciprocal (encrypt = decrypt).",
"rail_fence": "Rail-fence: zig-zag transposition — rearranges letters, keeps frequencies.",
"columnar": "Columnar transposition: writes rows, reads columns in key order.",
"columnar_transposition":"Columnar transposition: writes rows, reads columns in key order.",
"scytale": "Scytale: Spartan cylinder cipher — wrap text around a rod of fixed diameter.",
"double_transposition": "Double columnar transposition: apply columnar twice for stronger security.",
"stager_route": "Route (Stager) cipher: write into rectangle, read via alternating column route.",
# new sparse-label entries
"aeneas_tacticus": "Aeneas Tacticus: Greek sequential-number cipher; A=1…Z=26.",
"arnold_andre": "Arnold-André book cipher: coordinates (page.section.word) referencing a shared text.",
"babington": "Babington nomenclator: symbol tokens ⟨wNN⟩/⟨aNN⟩ used in the 1586 Babington Plot.",
"bacon_cipher": "Bacon's biliteral cipher: each letter encoded as 5-bit A/B string.",
"book_cipher": "Book cipher: coordinates (page.line.word) referencing a shared document.",
"commercial_code": "Commercial code: five-letter groups from a codebook, often ending in X or Z.",
"culper_ring": "Culper Ring code: Washington's three-digit codebook numbers (800–999 range).",
"homophonic": "Homophonic substitution: each letter maps to multiple possible numbers.",
"morse_code": "Morse code: dots and dashes; word-separated by / symbol.",
"navajo_code": "Navajo Code Talker cipher: Navajo words represent English letters.",
"null_cipher": "Null/steganographic cipher: hidden message formed by first letters of words.",
"one_time_pad": "One-time pad: theoretically unbreakable if key is truly random and never reused.",
"pigpen": "Pigpen cipher: letters encoded as positions in tic-tac-toe and X grids.",
"polybius": "Polybius square: 5×5 grid maps letters to two-digit row-column pairs.",
"running_key": "Running-key cipher: Vigenère with a very long key (e.g. a book passage).",
"tap_code": "Tap code: letters encoded as dot-groups representing row and column in 5×5 grid.",
"vernam": "Vernam cipher: XOR-based stream cipher; with a random key equivalent to OTP.",
"voynich_render": "Voynich manuscript: undeciphered 15th-century script; pseudo-syllabic output.",
"wallis_cipher": "Wallis cipher: each letter mapped to a unique two-digit number from a fixed table.",
"zimmermann": "Zimmermann Telegram code: WWI German diplomatic numeric codebook cipher.",
}
# ---------------------------------------------------------------------------
# Diverse plaintext corpus (mix of historical, technical, and educational text)
# ---------------------------------------------------------------------------
BASE_SENTENCES = [
"THE LIBRARY PRESERVES KNOWLEDGE FOR THE COMMUNITY",
"CRYPTOGRAPHY REWARDS PATIENCE AND CAREFUL THINKING",
"EVERY SYSTEM NEEDS AN HONEST THREAT MODEL",
"CLASSICAL CIPHERS TEACH WHY MODERN SECURITY MATTERS",
"THE DETECTIVE STUDIES PATTERNS BEFORE MAKING CLAIMS",
"FAIL CLOSED IS A POSTURE NOT A SLOGAN",
"FREQUENCY ANALYSIS CAN REVEAL WEAK CIPHERS",
"THE MESSAGE WAS HIDDEN BUT NOT SECURE",
"GOOD EDUCATIONAL TOOLS EXPLAIN THEIR LIMITS",
"PUBLIC INFRASTRUCTURE DESERVES CAREFUL DESIGN",
"A STRONG CLAIM NEEDS STRONG EVIDENCE",
"THE INDEX OF COINCIDENCE IS A USEFUL CLUE",
"HISTORICAL CIPHERS ARE TEACHERS NOT PROTECTION",
"A TRANSPARENT TOOL SHOULD SHOW ITS REASONING",
"SECURITY CLAIMS SHOULD BE TESTED UNDER SCRUTINY",
"A MODEL CAN CLASSIFY PATTERNS BUT IT CAN ALSO BE WRONG",
"MODERN ENCRYPTION IS NOT BROKEN BY LETTER FREQUENCY",
"THE BEST LESSONS SHOW WHERE THE METHOD FAILS",
"OBSERVABLE EVIDENCE COMES BEFORE CONFIDENCE",
"THE ARCHITECTURE SHOULD MAKE MISUSE HARDER",
"TRANSPOSITION CIPHERS REARRANGE LETTERS WITHOUT SUBSTITUTION",
"THE KASISKI EXAMINATION FINDS REPEATED TRIGRAMS IN CIPHERTEXT",
"POLYALPHABETIC CIPHERS USE MULTIPLE ALPHABET SHIFTS",
"SUBSTITUTION CIPHERS MAP EACH LETTER TO A DIFFERENT LETTER",
"THE RAIL FENCE CIPHER WRITES TEXT IN A ZIGZAG PATTERN",
"COLUMNAR TRANSPOSITION READS COLUMNS IN KEY ORDER",
"AN AFFINE CIPHER APPLIES A LINEAR FUNCTION TO EACH LETTER",
"THE VIGENERE CIPHER USES A REPEATING KEYWORD",
"CAESAR SHIFTED THE ALPHABET BY THREE POSITIONS",
"ATBASH ENCODES BY REVERSING THE ALPHABET",
"INDEX OF COINCIDENCE MEASURES LETTER FREQUENCY DISTRIBUTION",
"FRIEDMAN ESTIMATED KEY LENGTH FROM STATISTICS",
"BRUTE FORCE WORKS ONLY WHEN THE KEY SPACE IS SMALL",
"PATTERN WORDS HELP BREAK MONOALPHABETIC SUBSTITUTION",
"ENTROPY MEASURES UNCERTAINTY IN A DISTRIBUTION",
"DIGRAPH AND TRIGRAPH FREQUENCIES EXPOSE SIMPLE CIPHERS",
"HUMAN READABLE OUTPUT SHOULD SHOW ALL REASONING STEPS",
"DO NOT CONFUSE OBFUSCATION WITH ENCRYPTION",
"PEER REVIEW AND REPRODUCIBILITY MATTER IN RESEARCH",
"OPEN SOURCE TOOLS ALLOW INDEPENDENT VERIFICATION",
"THE ENIGMA MACHINE USED ROTORS TO CREATE A POLYALPHABETIC CIPHER",
"ALAN TURING BROKE ENIGMA WITH CRIBS AND THE BOMBE MACHINE",
"THE PLAYFAIR CIPHER ENCRYPTS PAIRS OF LETTERS SIMULTANEOUSLY",
"BEAUFORT IS THE RECIPROCAL OF VIGENERE AND DECRYPTS ITSELF",
"THE AUTOKEY CIPHER EXTENDS THE KEY WITH THE PLAINTEXT ITSELF",
"GRONSFELD USES A DIGIT SEQUENCE AS THE REPEATING KEY",
"THE TRITHEMIUS CIPHER ADVANCES THE ALPHABET BY ONE POSITION EACH LETTER",
"A SCYTALE IS A ROD USED BY SPARTANS TO ENCRYPT MESSAGES",
"DOUBLE TRANSPOSITION APPLIES COLUMNAR ENCRYPTION TWICE",
"ROUTE CIPHERS READ A GRID IN A PRESCRIBED GEOMETRIC PATTERN",
"POLYBIUS ENCODED LETTERS AS PAIRS OF NUMBERS FROM ONE TO FIVE",
"THE BIFID CIPHER COMBINES A POLYBIUS SQUARE WITH TRANSPOSITION",
"HILL CIPHER USES LINEAR ALGEBRA TO ENCRYPT BLOCKS OF LETTERS",
"THE FOUR SQUARE CIPHER USES TWO KEYWORD POLYBIUS SQUARES",
"TWO SQUARE CIPHER ENCRYPTS DIGRAPHS WITH TWO POLYBIUS ALPHABETS",
"ERROR CORRECTION AND DETECTION ARE FUNDAMENTAL TO SECURE SYSTEMS",
"THE FRIEDMAN TEST USES THE INDEX OF COINCIDENCE TO ESTIMATE KEY LENGTH",
"KERCKHOFFS PRINCIPLE SAYS THE SYSTEM MUST BE SECURE EVEN IF ENEMY KNOWS ALGORITHM",
"THE ONE TIME PAD IS THEORETICALLY UNBREAKABLE IF USED CORRECTLY",
"DIFFIE AND HELLMAN INTRODUCED PUBLIC KEY CRYPTOGRAPHY IN NINETEEN SEVENTY SIX",
]
# ---------------------------------------------------------------------------
# Cipher implementations
# ---------------------------------------------------------------------------
def clean(s: str) -> str:
return re.sub(r"[^A-Z]", "", s.upper())
def with_spacing(s: str) -> str:
if random.random() < 0.45:
group = random.choice([4, 5])
c = clean(s)
return " ".join(c[i:i + group] for i in range(0, len(c), group))
return s
def caesar(s: str, shift: int) -> str:
out = []
for ch in s.upper():
if ch in ALPHABET:
out.append(ALPHABET[(ALPHABET.index(ch) + shift) % 26])
else:
out.append(ch)
return "".join(out)
def atbash(s: str) -> str:
return s.upper().translate(str.maketrans(ALPHABET, ALPHABET[::-1]))
def vigenere(s: str, key: str) -> str:
key = clean(key)
out, j = [], 0
for ch in s.upper():
if ch in ALPHABET:
k = ALPHABET.index(key[j % len(key)])
out.append(ALPHABET[(ALPHABET.index(ch) + k) % 26])
j += 1
else:
out.append(ch)
return "".join(out)
def beaufort(s: str, key: str) -> str:
"""Beaufort cipher: E(x) = (k - x) mod 26 — reciprocal."""
key = clean(key)
out, j = [], 0
for ch in s.upper():
if ch in ALPHABET:
k = ALPHABET.index(key[j % len(key)])
x = ALPHABET.index(ch)
out.append(ALPHABET[(k - x) % 26])
j += 1
else:
out.append(ch)
return "".join(out)
def gronsfeld(s: str, digit_key: str) -> str:
"""Gronsfeld cipher: Vigenère with digits (0–9) as key characters."""
key = [int(d) for d in digit_key if d.isdigit()]
if not key:
key = [3, 1, 4, 1, 5]
out, j = [], 0
for ch in s.upper():
if ch in ALPHABET:
out.append(ALPHABET[(ALPHABET.index(ch) + key[j % len(key)]) % 26])
j += 1
else:
out.append(ch)
return "".join(out)
def autokey(s: str, primer: str) -> str:
"""Autokey Vigenère: key starts with primer, then continues with plaintext."""
chars = clean(s)
primer = clean(primer)
keystream = primer + chars
out = []
for i, ch in enumerate(chars):
k = ALPHABET.index(keystream[i])
out.append(ALPHABET[(ALPHABET.index(ch) + k) % 26])
return "".join(out)
def trithemius(s: str) -> str:
"""Trithemius: progressive Caesar — shift increases by 1 per letter."""
out, i = [], 0
for ch in s.upper():
if ch in ALPHABET:
out.append(ALPHABET[(ALPHABET.index(ch) + i) % 26])
i += 1
else:
out.append(ch)
return "".join(out)
def porta(s: str, key: str) -> str:
"""Porta cipher (simplified): 13-alphabet polyalphabetic table."""
# Each row shifts both halves; key letter selects which row (A/B→0, C/D→1, …)
# Standard Porta mixes both halves of the alphabet in each row.
PORTA_ROWS = [
"NOPQRSTUVWXYZABCDEFGHIJKLM",
"ONPQRSTUVWXYZABCDEFGHIJKLM",
"OPNQRSTUVWXYZABCDEFGHIJKLM", # simplified approximation
"OPQNRSTUVWXYZABCDEFGHIJKLM",
"OPQRNSTUVWXYZABCDEFGHIJKLM",
"OPQRSNUVWXYZABCDEFGHIJKLMT",
"OPQRSTUNVWXYZABCDEFGHIJKLM",
"OPQRSTUVNWXYZABCDEFGHIJKLM",
"OPQRSTUVWNXYZABCDEFGHIJKLM",
"OPQRSTUVWXNYZABCDEFGHIJKLM",
"OPQRSTUVWXYNZABCDEFGHIJKLM",
"OPQRSTUVWXYZNABCDEFGHIJKLM",
"OPQRSTUVWXYZANABCDEFGHIJKL",
]
key = clean(key)
out, j = [], 0
for ch in s.upper():
if ch in ALPHABET:
ki = (ALPHABET.index(key[j % len(key)]) // 2) % 13
row = PORTA_ROWS[ki]
x = ALPHABET.index(ch)
out.append(row[x])
j += 1
else:
out.append(ch)
return "".join(out)
def rail_fence(text: str, rails: int) -> str:
chars = clean(text)
fence = ["" for _ in range(rails)]
rail, direction = 0, 1
for ch in chars:
fence[rail] += ch
if rail == 0:
direction = 1
elif rail == rails - 1:
direction = -1
rail += direction
return "".join(fence)
def columnar(text: str, key: str) -> str:
chars = clean(text)
key = clean(key)
cols = len(key)
chars += "X" * ((-len(chars)) % cols)
rows = [chars[i:i + cols] for i in range(0, len(chars), cols)]
order = sorted(range(cols), key=lambda i: (key[i], i))
return "".join("".join(row[i] for row in rows) for i in order)
def scytale(text: str, cols: int) -> str:
"""Scytale: wrap text around a rod of `cols` diameter and read off columns."""
chars = clean(text)
padding = (-len(chars)) % cols
chars += "X" * padding
n_rows = len(chars) // cols
result = []
for c in range(cols):
for r in range(n_rows):
result.append(chars[r * cols + c])
return "".join(result)
def double_transposition(text: str, key1: str, key2: str) -> str:
"""Double columnar transposition: apply columnar encryption twice."""
return columnar(columnar(text, key1), key2)
def stager_route(text: str, cols: int) -> str:
"""Route (Stager) cipher: fill rectangle row-by-row, read columns alternating direction."""
chars = clean(text)
padding = (-len(chars)) % cols
chars += "X" * padding
n_rows = len(chars) // cols
grid = [chars[r * cols:(r + 1) * cols] for r in range(n_rows)]
result = []
for c in range(cols):
if c % 2 == 0:
for r in range(n_rows):
result.append(grid[r][c])
else:
for r in range(n_rows - 1, -1, -1):
result.append(grid[r][c])
return "".join(result)
def affine(text: str, a: int, b: int) -> str:
out = []
for ch in text.upper():
if ch in ALPHABET:
out.append(ALPHABET[(a * ALPHABET.index(ch) + b) % 26])
else:
out.append(ch)
return "".join(out)
def substitution(text: str, alphabet: str) -> str:
trans = str.maketrans(ALPHABET, alphabet)
return text.upper().translate(trans)
# ---------------------------------------------------------------------------
# Sparse-label cipher encoders (museum-only classes → synthetic generation)
# ---------------------------------------------------------------------------
# Bacon biliteral cipher: each letter → 5-bit A/B string
_BACON = {
'A': 'AAAAA', 'B': 'AAAAB', 'C': 'AAABA', 'D': 'AAABB', 'E': 'AABAA',
'F': 'AABAB', 'G': 'AABBA', 'H': 'AABBB', 'I': 'ABAAA', 'J': 'ABAAA',
'K': 'ABAAB', 'L': 'ABABA', 'M': 'ABABB', 'N': 'ABBAA', 'O': 'ABBAB',
'P': 'ABBBA', 'Q': 'ABBBB', 'R': 'BAAAA', 'S': 'BAAAB', 'T': 'BAABA',
'U': 'BAABB', 'V': 'BAABB', 'W': 'BABAA', 'X': 'BABAB', 'Y': 'BABBA',
'Z': 'BABBB',
}
def bacon_cipher_encode(text: str) -> str:
return " ".join(_BACON.get(c, 'AAAAA') for c in clean(text))
# Polybius square: 5×5 grid (I=J), letters → row-col digit pair
_POLYBIUS_GRID: dict[str, str] = {}
_p_idx = 0
for _ch in "ABCDEFGHIKLMNOPQRSTUVWXYZ": # no J
_POLYBIUS_GRID[_ch] = f"{_p_idx // 5 + 1}{_p_idx % 5 + 1}"
_p_idx += 1
_POLYBIUS_GRID['J'] = _POLYBIUS_GRID['I']
def polybius_encode(text: str) -> str:
return " ".join(_POLYBIUS_GRID.get(c, '11') for c in clean(text))
# Morse code: dots and dashes, word-separated by " / "
_MORSE: dict[str, str] = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
'Z': '--..',
}
def morse_encode(text: str) -> str:
words = [re.sub(r'[^A-Z]', '', w) for w in text.upper().split()]
return " / ".join(
" ".join(_MORSE[c] for c in w if c in _MORSE)
for w in words if w
)
# Tap code: letters as row.col dot-groups separated by double-space
# 5×5 grid, K → C (no K row)
_TAP: dict[str, tuple] = {}
_t_idx = 0
for _ch in "ABCDEFGHIJLMNOPQRSTUVWXYZ": # no K
_TAP[_ch] = (_t_idx // 5 + 1, _t_idx % 5 + 1)
_t_idx += 1
_TAP['K'] = _TAP['C']
def tap_code_encode(text: str) -> str:
parts = []
for c in clean(text):
row, col = _TAP.get(c, (1, 1))
parts.append('.' * row + ' ' + '.' * col)
return ' '.join(parts)
# Aeneas Tacticus: A=1 … Z=26 (space-separated integers)
def aeneas_tacticus_encode(text: str) -> str:
return " ".join(str(ALPHABET.index(c) + 1) for c in clean(text))
# Wallis cipher: fixed shuffled 2-digit mapping (seeded for reproducibility)
_wallis_nums = list(range(10, 100))
random.Random(42).shuffle(_wallis_nums)
_WALLIS_MAP: dict[str, str] = {c: str(_wallis_nums[i]) for i, c in enumerate(ALPHABET)}
def wallis_cipher_encode(text: str) -> str:
return " ".join(_WALLIS_MAP[c] for c in clean(text))
# Homophonic substitution: common letters have multiple numeric homophones
_HOMO_TABLE: dict[str, list[int]] = {}
_homo_pool = list(range(1, 100))
random.Random(7).shuffle(_homo_pool)
_homo_idx_ = 0
for _rank, _c in enumerate("ETAOINSHRDLCUMWFGYPBVKJXQZ"):
_n = max(1, 4 - _rank // 6) # E→4 homophones, Z→1
_HOMO_TABLE[_c] = _homo_pool[_homo_idx_: _homo_idx_ + _n]
_homo_idx_ = min(_homo_idx_ + _n, 98)
def homophonic_encode(text: str) -> str:
return " ".join(str(random.choice(_HOMO_TABLE.get(c, [1]))) for c in clean(text))
# Culper Ring: ~3-digit numbers (800–998), 999 = word separator
def culper_ring_encode(text: str) -> str:
groups: list[str] = []
for word in text.upper().split():
letters = re.sub(r'[^A-Z]', '', word)
if not letters:
continue
for c in letters:
groups.append(str(800 + ALPHABET.index(c) * 3 + random.randint(0, 2)))
groups.append('999')
return " ".join(groups)
# Zimmermann Telegram style: 4–5 digit numeric groups
def zimmermann_encode(text: str) -> str:
codes: list[str] = []
for c in clean(text):
base = (ALPHABET.index(c) + 1) * 314 + random.randint(0, 99)
if random.random() < 0.5:
codes.append(f"{base % 10000:04d}")
else:
codes.append(f"{base % 100000:05d}")
return " ".join(codes)
# Book cipher: page.line.word triples
def book_cipher_encode(text: str) -> str:
parts = []
for _c in clean(text):
parts.append(f"{random.randint(1, 20)}.{random.randint(1, 50)}.{random.randint(1, 15)}")
return " ".join(parts)
# Arnold-André book cipher: page.section.word triples (tighter ranges)
def arnold_andre_encode(text: str) -> str:
parts = []
for _c in clean(text):
parts.append(f"{random.randint(1, 20)}.{random.randint(1, 6)}.{random.randint(1, 10)}")
return " ".join(parts)
# Babington nomenclator: ⟨wNN⟩ / ⟨aNN⟩ tokens
_BABINGTON_MAP: dict[str, str] = {
c: f"⟨{'w' if i % 2 == 0 else 'a'}{(i * 3 + 7) % 40 + 1:02d}⟩"
for i, c in enumerate(ALPHABET)
}
def babington_encode(text: str) -> str:
return " ".join(_BABINGTON_MAP.get(c, '⟨w00⟩') for c in clean(text))
# Navajo Code Talker
_NAVAJO: dict[str, str] = {
'A': 'WOL-LA-CHEE', 'B': 'SHUSH', 'C': 'MOASI', 'D': 'BE',
'E': 'DZEH', 'F': 'MA-E', 'G': 'AH-JAH', 'H': 'LIN',
'I': 'TKIN', 'J': 'TKELE-CHO-G', 'K': 'KLIZZIE', 'L': 'DIBEH-YAZZIE',
'M': 'NA-AS-TSO-SI','N': 'TSAH', 'O': 'A-KHA', 'P': 'CLA-GI-AIH',
'Q': 'CA-YEILTH', 'R': 'GAH', 'S': 'DIBEH', 'T': 'THAN-ZIE',
'U': 'NO-DA-IH', 'V': 'A-KEH-DI-GLINI', 'W': 'GLOE-IH', 'X': 'AL-AN-AS-DZOH',
'Y': 'TSAH-AS-ZIH', 'Z': 'BESH-DO-TLIZ',
}
def navajo_encode(text: str) -> str:
chars = clean(text)[:40] # keep length sane
return " / ".join(_NAVAJO.get(c, 'WOL-LA-CHEE') for c in chars)
# Null / acrostic cipher: first letter of each word encodes message
_ACROSTIC: dict[str, list[str]] = {
'A': ['again','along','after','above','alert','among','array'],
'B': ['below','brief','board','broad','basis','batch'],
'C': ['could','clear','cover','cause','cross','carry','claim'],
'D': ['depot','delay','draft','drive','daily','depth'],
'E': ['enemy','early','eight','extra','exact','enter'],
'F': ['force','front','first','field','flank','final'],
'G': ['guard','group','guide','given','going','gains'],
'H': ['heavy','holds','heads','hence','hours','house'],
'I': ['intel','inner','issue','input','incur'],
'J': ['joint','joins','judge','jetty'],
'K': ['keeps','knife','known','kings'],
'L': ['light','large','lines','lower','leads','local'],
'M': ['major','march','motor','mixed','miles','moved'],
'N': ['north','night','naval','notes','needs'],
'O': ['order','outer','often','opens','other'],
'P': ['point','posts','place','parts','phase'],
'Q': ['quick','quiet','quota'],
'R': ['range','route','radio','raids','right','roads'],
'S': ['south','swift','scout','stage','shore','seeks'],
'T': ['troop','takes','three','track','turns'],
'U': ['under','unite','until','upper','units'],
'V': ['vital','vague','value','verse'],
'W': ['where','while','width','wings','watch'],
'X': ['xerox','extra'],
'Y': ['yield','years','young'],
'Z': ['zones','zonal','zeal'],
}
def null_cipher_encode(text: str) -> str:
chars = clean(text)[:25]
return " ".join(random.choice(_ACROSTIC.get(c, ['extra'])) for c in chars)
# Pigpen cipher: Unicode symbol representations (matching real dataset format)
_PIGPEN_SYMS = [
'⌐', '¬', 'r', 'Γ', 'L', '⌐·', '¬·', 'r·', 'Γ·',
'┘', '┐', '└', '┌', '─', '┘·', '┐·', '└·', '┌·',
'X', 'K', 'X·', 'K·',
'⌒', '⌣', '⌒·', '⌣·',
]
_PIGPEN_TABLE: dict[str, str] = {ALPHABET[i]: _PIGPEN_SYMS[i % len(_PIGPEN_SYMS)] for i in range(26)}
def pigpen_encode(text: str) -> str:
return " ".join(_PIGPEN_TABLE[c] for c in clean(text))
# Commercial code: 5-letter groups; many ending in Z
def commercial_code_encode(text: str) -> str:
chars = clean(text)
if not chars:
chars = "HELLO"
groups: list[str] = []
for i in range(0, len(chars), 5):
chunk = list(chars[i:i + 5])
while len(chunk) < 5:
chunk.append(random.choice('XZABCDE'))
if random.random() < 0.4:
chunk[-1] = random.choice('XZ')
groups.append("".join(chunk))
return " ".join(groups)
# Voynich manuscript render: pseudo-syllabic words
_VOYNICH_WORDS = [
'oror', 'oxed', 'otol', 'qokeedy', 'chedy', 'shedy', 'dainy', 'otaiin',
'cheol', 'ykcheol', 'qokal', 'raiin', 'aiiin', 'chol', 'chor',
'daral', 'shal', 'sharal', 'okal', 'daiin', 'chedal', 'sheedy',
'otedy', 'qotedy', 'ctaiin', 'ytedy', 'rcheol', 'okeedy', 'lkaiin',
'okshy', 'okedy', 'otain', 'qokedy', 'okain', 'choldaiin', 'sheol',
'ypchol', 'dal', 'shor', 'kaiiin', 'qokeedy', 'ytaiin', 'shaiin',
]
def voynich_render_encode(text: str) -> str:
n_words = max(10, len(clean(text)) // 3)
return " ".join(random.choice(_VOYNICH_WORDS) for _ in range(n_words))
# Running key: Vigenère with a full sentence as the key
def running_key_encode(text: str) -> str:
plain = clean(text)
key = clean(random.choice(BASE_SENTENCES))
while len(key) < len(plain):
key += key
out = [ALPHABET[(ALPHABET.index(p) + ALPHABET.index(key[i])) % 26]
for i, p in enumerate(plain)]
return with_spacing("".join(out))
# One-time pad: truly random letter sequence
def one_time_pad_encode(text: str) -> str:
return with_spacing("".join(random.choice(ALPHABET) for _ in clean(text)))
# Vernam: same structure as OTP for text representation
def vernam_encode(text: str) -> str:
return with_spacing("".join(random.choice(ALPHABET) for _ in clean(text)))
def make_plain() -> str:
k = random.randint(1, 4)
parts = random.sample(BASE_SENTENCES, k=k)
plain = " ".join(parts)
if random.random() < 0.12:
plain += " " + random.choice([
"TODAY", "TOMORROW", "CAREFULLY", "HONESTLY", "SLOWLY",
"ALWAYS", "NEVER", "CERTAINLY", "PERHAPS",
])
return plain
# ---------------------------------------------------------------------------
# Record builder
# ---------------------------------------------------------------------------
def record(
label: str, plaintext: str, ciphertext: str, meta: dict[str, object], rid: int
) -> dict[str, object]:
text_clean = clean(ciphertext)
# Symbolic/numeric ciphers (tap code, morse, polybius, book ciphers, etc.) lose their
# distinctive format when passed through with_spacing()'s clean() branch.
# Preserve the raw ciphertext when it is not primarily alphabetic.
cipher_nonspace = ciphertext.replace(" ", "")
alpha_ratio = len(text_clean) / max(1, len(cipher_nonspace))
if alpha_ratio >= 0.5:
display_text = with_spacing(ciphertext)
else:
display_text = ciphertext # keep dots / numbers / symbols intact
# text_length uses the plaintext letter count for non-alphabetic ciphers
# so difficulty/length are still meaningful even for numeric outputs.
n = len(text_clean) if text_clean else len(clean(plaintext))
if n < 30:
difficulty = "easy"
elif n < 80:
difficulty = "medium"
else:
difficulty = "hard"
return {
"id": f"cda-{rid:07d}",
"text": display_text,
"ciphertext": display_text,
"plaintext": plaintext,
"label": label,
"cipher": label,
"key": meta,
"difficulty": difficulty,
"language": "en",
"text_length": n,
"length": n,
"attack_methods": ATTACKS.get(label, []),
"educational_note": EDU_NOTES.get(label, ""),
"source": "synthetic_educational",
}
# ---------------------------------------------------------------------------
# Per-label generator
# ---------------------------------------------------------------------------
_VIGENERE_KEYS = [
"KEY", "LIBRARY", "CIPHER", "MUSEUM", "PRAYER", "SECURE", "PATTERN",
"HONEST", "PUBLIC", "MODEL", "SIGNAL", "ENIGMA", "LIBERTY", "FREEDOM",
"JUSTICE", "WARRIOR", "DRAGON", "THUNDER", "PALACE", "BATTLE",
]
_DOUBLE_KEYS_A = ["CIPHER", "MUSEUM", "SECURE", "LIBERTY", "JUSTICE"]
_DOUBLE_KEYS_B = ["PATTERN", "SIGNAL", "FREEDOM", "DRAGON", "PALACE"]
_GRONSFELD_KEYS = ["314159", "271828", "141421", "31416", "27183", "11235", "99999"]
_PORTA_KEYS = ["KEY", "LIBRARY", "CIPHER", "SECURE", "PATTERN", "JUSTICE"]
def build_row(label: str, rid: int) -> dict[str, object]: # noqa: C901
plain = make_plain()
if label in ("plaintext",):
return record(label, plain, plain, {}, rid)
if label in ("caesar_rot", "caesar"):
shift = random.randint(1, 25)
return record(label, plain, caesar(plain, shift), {"shift": shift}, rid)
if label == "rot13":
return record(label, plain, caesar(plain, 13), {"shift": 13}, rid)
if label == "atbash":
return record(label, plain, atbash(plain), {}, rid)
if label == "vigenere":
key = random.choice(_VIGENERE_KEYS)
return record(label, plain, vigenere(plain, key), {"key": key}, rid)
if label == "beaufort":
key = random.choice(_VIGENERE_KEYS)
return record(label, plain, beaufort(plain, key), {"key": key}, rid)
if label == "gronsfeld":
key = random.choice(_GRONSFELD_KEYS)
return record(label, plain, gronsfeld(plain, key), {"key": key}, rid)
if label == "autokey":
primer = random.choice(_VIGENERE_KEYS[:8])
return record(label, plain, autokey(plain, primer), {"primer": primer}, rid)
if label == "trithemius":
return record(label, plain, trithemius(plain), {}, rid)
if label == "porta":
key = random.choice(_PORTA_KEYS)
return record(label, plain, porta(plain, key), {"key": key}, rid)
if label == "rail_fence":
rails = random.choice([2, 3, 4, 5])
return record(label, plain, rail_fence(plain, rails), {"rails": rails}, rid)
if label in ("columnar", "columnar_transposition"):
key = random.choice(_VIGENERE_KEYS)
return record(label, plain, columnar(plain, key), {"key": key}, rid)
if label == "scytale":
cols = random.randint(4, 12)
return record(label, plain, scytale(plain, cols), {"cols": cols}, rid)
if label == "double_transposition":
key1 = random.choice(_DOUBLE_KEYS_A)
key2 = random.choice(_DOUBLE_KEYS_B)
return record(label, plain, double_transposition(plain, key1, key2),
{"key1": key1, "key2": key2}, rid)
if label == "stager_route":
cols = random.randint(5, 12)
return record(label, plain, stager_route(plain, cols), {"cols": cols}, rid)
if label == "affine":
a, b = random.choice(AFFINE_A_VALUES), random.randint(0, 25)
return record(label, plain, affine(plain, a, b), {"a": a, "b": b}, rid)
if label in ("substitution", "monoalphabetic"):
alph = list(ALPHABET)
random.shuffle(alph)
alph = "".join(alph)
return record(label, plain, substitution(plain, alph), {"alphabet": alph}, rid)
# --- Sparse museum-label generators ---
if label == "bacon_cipher":
return record(label, plain, bacon_cipher_encode(plain), {}, rid)
if label == "polybius":
return record(label, plain, polybius_encode(plain), {}, rid)
if label == "morse_code":
return record(label, plain, morse_encode(plain), {}, rid)
if label == "tap_code":
return record(label, plain, tap_code_encode(plain), {}, rid)
if label == "aeneas_tacticus":
return record(label, plain, aeneas_tacticus_encode(plain), {}, rid)
if label == "wallis_cipher":
return record(label, plain, wallis_cipher_encode(plain), {}, rid)
if label == "homophonic":
return record(label, plain, homophonic_encode(plain), {}, rid)
if label == "culper_ring":
return record(label, plain, culper_ring_encode(plain), {}, rid)
if label == "zimmermann":
return record(label, plain, zimmermann_encode(plain), {}, rid)
if label == "book_cipher":
return record(label, plain, book_cipher_encode(plain), {}, rid)
if label == "arnold_andre":
return record(label, plain, arnold_andre_encode(plain), {}, rid)
if label == "babington":
return record(label, plain, babington_encode(plain), {}, rid)
if label == "navajo_code":
return record(label, plain, navajo_encode(plain), {}, rid)
if label == "null_cipher":
return record(label, plain, null_cipher_encode(plain), {}, rid)
if label == "pigpen":
return record(label, plain, pigpen_encode(plain), {}, rid)
if label == "commercial_code":
return record(label, plain, commercial_code_encode(plain), {}, rid)
if label == "voynich_render":
return record(label, plain, voynich_render_encode(plain), {}, rid)
if label == "running_key":
return record(label, plain, running_key_encode(plain), {}, rid)
if label == "one_time_pad":
return record(label, plain, one_time_pad_encode(plain), {}, rid)
if label == "vernam":
return record(label, plain, vernam_encode(plain), {}, rid)
raise ValueError(f"Unknown label: {label!r}")
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
# Labels the synthetic generator can produce. Keep in sync with build_row().
_SYNTH_LABELS = [
"plaintext",
"caesar_rot", "caesar", "rot13", "atbash", "affine",
"substitution", "monoalphabetic",
"vigenere", "beaufort", "gronsfeld", "autokey", "trithemius", "porta",
"rail_fence", "columnar", "columnar_transposition",
"scytale", "double_transposition", "stager_route",
# sparse museum-only labels now with synthetic generators
"aeneas_tacticus", "arnold_andre", "babington", "bacon_cipher",
"book_cipher", "commercial_code", "culper_ring", "homophonic",
"morse_code", "navajo_code", "null_cipher", "one_time_pad",
"pigpen", "polybius", "running_key", "tap_code",
"vernam", "voynich_render", "wallis_cipher", "zimmermann",
]
def main() -> None:
ap = argparse.ArgumentParser(
description="Generate or extend the synthetic cipher-examples dataset."
)
ap.add_argument("--out", default="data/cipher_examples.jsonl")
ap.add_argument("--n", type=int, default=10000,
help="Number of synthetic examples to generate.")
ap.add_argument("--seed", type=int, default=42)
ap.add_argument(
"--append", action="store_true",
help="Append to existing file instead of overwriting. "
"Uses max existing id to avoid collisions.",
)
ap.add_argument(
"--labels", nargs="*", default=None,
help="Restrict generation to these labels (default: all synthetic labels).",
)
ap.add_argument(
"--per-label", type=int, default=None,
help="Generate exactly this many examples per label (overrides --n).",
)
args = ap.parse_args()
random.seed(args.seed)
labels = args.labels or _SYNTH_LABELS
unknown = [lbl for lbl in labels if lbl not in _SYNTH_LABELS]
if unknown:
ap.error(f"Unknown labels (not implemented in synthetic generator): {unknown}")
out = Path(args.out)
out.parent.mkdir(parents=True, exist_ok=True)
# Determine starting row ID to avoid collisions when appending.
start_id = 0
if args.append and out.exists():
for line in out.read_text(encoding="utf-8").splitlines():
if line.strip():
try:
rid = int(json.loads(line).get("id", "cda-0").split("-")[-1])
start_id = max(start_id, rid + 1)
except (ValueError, KeyError):
pass
if args.per_label is not None:
total = args.per_label * len(labels)
plan = [lbl for lbl in labels for _ in range(args.per_label)]
random.shuffle(plan)
else:
total = args.n
plan = [labels[i % len(labels)] for i in range(total)]
random.shuffle(plan)
mode = "a" if args.append else "w"
written = 0
with out.open(mode, encoding="utf-8") as f:
for i, label in enumerate(plan):
row = build_row(label, start_id + i)
f.write(json.dumps(row, ensure_ascii=False) + "\n")
written += 1
print(f"Wrote {written:,} examples ({'appended' if args.append else 'created'}) → {out}")
if __name__ == "__main__":
main()
|