The cryptorama module¶
-
class
cryptorama.alphabet.
Alphabet
(letters)[source]¶ A bijection from Z/nZ to a set of letters. The letters are strings of length 1. An alphabet is initialized with a string of length n. If this initialization string has repeated letters a ValueError is raised.
Use [] to map from indexes to letters. Use () to map from letters to indexes.
-
class
cryptorama.alphabet.
Substitution
(domain, range, function)[source]¶ A bijection from one alphabet to another, defined by a python function that is assumed to give a bijection between the index rings of the two alphabets. On letters not in the domain alphabet, a Substitution acts as the identity.
For alphabets A and B and a function f, the mapping is A[m] -> B[f(m)].
-
class
cryptorama.message.
Message
(message, alphabet=abcdefghijklmnopqrstuvwxyz)[source]¶ A Message is a Python string with an additional attribute which is an Alphabet. The default alphabet is small letters.
It is not required that all letters in the message be in the alphabet, but those which are not in the alphabet are preserved when encrypting the message. To avoid having them appear in the ciphertext, encrypt M.clean() rather than M.
A Message supports all string methods, but they return Messages with the same alphabet, rather than plain strings.
-
clean
()[source]¶ Return a new message obtained by deleting all letters not in the alphabet.
>>> m = Message('HELLO, WORLD', big) >>> m *HELLO, WORLD* >>> m.clean() *HELLOWORLD*
-
cycle_substitute
(subs_list)[source]¶ Given a list of k Substitutions apply the nth Substitution (mod k) to the nth letter.
>>> m = Message('hello, world', small) >>> s1 = Substitution(small, big, lambda n : n + 2) >>> m.substitute(s1) *JGNNQ, YQTNF* >>> s2 = Substitution(small, big, lambda n : n + 5) >>> m.substitute(s2) *MJQQT, BTWQI* >>> m.cycle_substitute([s1, s2]) *JJNQQ, BQWNI*
-
frequencies
(sort=True)[source]¶ Return a list of pairs (l, f) where l is a letter of the alphabet and f is the frequency at which l appears in this message. The letters appear in order.
-
-
class
cryptorama.cipher.
AffineCipher
(m=1, b=0, plain=abcdefghijklmnopqrstuvwxyz, cipher=ABCDEFGHIJKLMNOPQRSTUVWXYZ)[source]¶ A affine cipher, using the function x -> m*x + b (mod N) where N is the alphabet size.
Instantiate with a plaintext alphabet, a ciphertext alphabet and integers m and b. By default, small letters are used for plaintext and big letters for ciphertext.
>>> m = Message('hello, world') >>> C = AffineCipher(m=5, b=3) >>> m *hello, world* >>> M = C.encrypt(m) >>> M *MXGGV, JVKGS* >>> C.decrypt(M) *hello, world*
-
class
cryptorama.cipher.
CaesarCipher
(key=0, plain=abcdefghijklmnopqrstuvwxyz, cipher=ABCDEFGHIJKLMNOPQRSTUVWXYZ)[source]¶ A Ceasar cipher is an affine cipher with m = 1. The key, b, can be provided as a letter in the plaintext alphabet or an integer.
>>> m = Message('hello, world') >>> C = CaesarCipher(key='m') >>> M = C.encrypt(m) >>> C = CaesarCipher(key='m') >>> m = Message('hello, world') >>> m *hello, world* >>> M = C.encrypt(m) >>> M *TQXXA, IADXP* >>> C.decrypt(M) *hello, world*
-
classmethod
crack
(ciphertext, plain=abcdefghijklmnopqrstuvwxyz)[source]¶ Brute force. Returns a dictionary mapping each key to the message obtained by decrypting with that key.
>>> m = Message('hello, world') >>> m *hello, world* >>> C = CaesarCipher(key='f') >>> M = C.encrypt(m) >>> M *MJQQT, BTWQI* >>> tries = CaesarCipher.crack(M) >>> tries['b'] *lipps, asvph* >>> tries['f'] *hello, world*
-
classmethod
-
class
cryptorama.cipher.
Cipher
[source]¶ Base class for ciphers.
-
class
cryptorama.cipher.
MultiplicativeCipher
(key=1, plain=abcdefghijklmnopqrstuvwxyz, cipher=ABCDEFGHIJKLMNOPQRSTUVWXYZ)[source]¶ A MultiplicativeCipher is an affine cipher with b=0.
>>> m = Message('hello, world') >>> m *hello, world* >>> C = MultiplicativeCipher(key=5) >>> M = C.encrypt(m) >>> M *JUDDS, GSHDP* >>> C.decrypt(M) *hello, world*
-
class
cryptorama.cipher.
SubstitutionCipher
(plain, cipher)[source]¶ Provide two alphabets of the same size. The cipher maps letters of the plaintext alphabet to letters of the ciphertext alphabet in order. In other words it is an affine cipher with m=1 and b=0.
>>> m = Message('hello, world') >>> m *hello, world* >>> qwerty = Alphabet('QWERTYUIOPASDFGHJKLZXCVBNM') >>> C = SubstitutionCipher(small, qwerty) >>> M = C.encrypt(m) >>> M *ITSSG, VGKSR* >>> C.decrypt(M) *hello, world*
-
class
cryptorama.cipher.
VigenereCipher
(keyword=”, plain=abcdefghijklmnopqrstuvwxyz, cipher=ABCDEFGHIJKLMNOPQRSTUVWXYZ)[source]¶ The Vigenere cipher uses a cycle substitution where the nth substitution in the cycle is a shift by the nth letter of the key word.
>>> m = Message('hello, world') >>> m *hello, world* >>> V = VigenereCipher(keyword='bye') >>> M = V.encrypt(m) >>> M *ICPMM, APPPE* >>> V.decrypt(M) *hello, world*
-
classmethod
crack
(ciphertext, plain=abcdefghijklmnopqrstuvwxyz)[source]¶ Crack a Vigenere cipher by using Bonnie’s algorithm.
>>> M = Message('YBRWY JFM N QCGYFR GIL SUZJX WJMFJ. NUJ VVL VBDM VS NUJ HRNAUGIEMIBI WBSMGFHGQS GJUFJX UNG. FTGRYCZJM GMYL TZSJLRI BVR U PMIVHY OJNJJYA F HVHERQ UAI U QNGR. OYFXY NQQNDM GTIX YBR SCPPYY-FZGJL NQF, VY QNX VVLARW. NUJ VVL VBDM YFOTMYQ FHQ QUHLBRI. IAJ XND USYYE OYFXY TWUOGYQ YBR SCPPYY, MCF KUGMYE YIBP BVR UFNXR FHQ XUVI, "DRXMR, YBBXY OTSF FLR RUXNHT KOA TZ LTO. GMYL YBVSE LTO QTHG PHBB NUJ XVRY VX QBWNU RIEJ NUFH GMY ANWXJF." WJMFJ AENHAJX NSX FFCQ, "IIAY QBWLL IUQ. N EATQ JMCPM CF BIEYB ZTLR. GOG NZ V YIBP NUJ XVRY, GMYL BIHQX FYIC IIVSA VY. MB KUE NPR HIYQYPYYQ $10 IIYQUEX."', big) >>> VigenereCipher.crack(M) ('fun', *there was a little boy named jesse. the big boys in the neighborhood constantly teased him. sometimes they offered him a choice between a nickel and a dime. jesse always took the nickel-after all, it was bigger. the big boys laughed and laughed. one day after jesse grabbed the nickel, his father took him aside and said, "jesse, those boys are making fun of you. they think you dont know the dime is worth more than the nickel." jesse grinned and said, "dont worry dad. i know which is worth more. but if i took the dime, they would stop doing it. so far ive collected $10 dollars."*)
-
classmethod