omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / bacon-cipher / oracle.py
| 1 | # Independent reference for "Bacon cipher" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | secret = "rosetta code" |
| 4 | cover = "bacon's cipher is a method of steganography devised by francis bacon. a message is concealed in the presentation of text rather than its content." |
| 5 | bits = "".join(format(26 if c == " " else ord(c) - 97, "05b") for c in secret) |
| 6 | out, b = [], 0 |
| 7 | for c in cover: |
| 8 | if "a" <= c <= "z" and b < len(bits): |
| 9 | if bits[b] == "1": |
| 10 | c = c.upper() |
| 11 | b += 1 |
| 12 | out.append(c) |
| 13 | enc = "".join(out) |
| 14 | print(enc) |
| 15 | got = "".join("1" if c.isupper() else "0" for c in enc if c.isalpha())[:len(bits)] |
| 16 | print("".join(" " if int(got[i:i + 5], 2) == 26 else chr(97 + int(got[i:i + 5], 2)) for i in range(0, len(got), 5))) |