Return the letters of s in uppercase. In case more than
one string is passed, each of them will be processed and a list
of upper case strings will be returned.
Produce the Polybius square for the 5 times 5 Bifid cipher.
Examples
>>> from.cryptoimportbifid5_square>>> bifid5_square("gold bug")Matrix([[G, O, L, D, B],[U, A, C, E, F],[H, I, K, M, N],[P, Q, R, S, T],[V, W, X, Y, Z]])
>>> from.cryptoimport(... bifid_square,AZ,padded_key,bifid5)>>> bifid_square(AZ().replace('J',''))Matrix([[A, B, C, D, E],[F, G, H, I, K],[L, M, N, O, P],[Q, R, S, T, U],[V, W, X, Y, Z]])
>>> bifid_square(padded_key(AZ('gold bug!'),bifid5))Matrix([[G, O, L, D, B],[U, A, C, E, F],[H, I, K, M, N],[P, Q, R, S, T],[V, W, X, Y, Z]])
Return the deciphered text that was made from the mapping,
x rightarrow ax+b (mod N), where N is the
number of characters in the alphabet. Deciphering is done by
reciphering with a new key: x rightarrow cx+d (mod N),
where c = a^{-1} (mod N) and d = -a^{-1}b (mod N).
Examples
>>> from.cryptoimportencipher_affine,decipher_affine>>> msg="GO NAVY BEAT ARMY">>> key=(3,1)>>> encipher_affine(msg,key)'TROBMVENBGBALV'>>> decipher_affine(_,key)'GONAVYBEATARMY'
>>> alp=AZ().replace('J','')>>> ct=AZ("meet me on monday!")>>> key=AZ("gold bug")>>> encipher_bifid(ct,key,alp)'IEILHHFSTSFQYE'
When entering the text or ciphertext, spaces are ignored so it
can be formatted as desired. Re-entering the ciphertext from the
preceding, putting 4 characters per line and padding with an extra
J, does not cause problems for the deciphering:
This is the version of the Bifid cipher that uses the 5 times 5
Polybius square; the letter “J” is ignored unless a key of
length 25 is used.
INPUT:
msg: ciphertext string
key: short string for key; duplicated characters are
ignored and if the length is less then 25 characters, it
will be padded with other letters from the alphabet omitting
“J”. Non-alphabetic characters are ignored.
OUTPUT:
plaintext from Bifid5 cipher (all caps, no spaces)
Examples
>>> from.cryptoimportencipher_bifid5,decipher_bifid5>>> key="gold bug">>> encipher_bifid5('meet me on friday',key)'IEILEHFSTSFXEE'>>> encipher_bifid5('meet me on monday',key)'IEILHHFSTSFQYE'>>> decipher_bifid5(_,key)'MEETMEONMONDAY'
Performs the Bifid cipher decryption on ciphertext msg, and
returns the plaintext.
This is the version of the Bifid cipher that uses the 6 times 6
Polybius square.
INPUT:
msg: ciphertext string (digits okay); converted to upper case
key: short string for key (digits okay). If key is
less than 36 characters long, the square will be filled with
letters A through Z and digits 0 through 9. All letters are
converted to uppercase.
OUTPUT:
plaintext from Bifid cipher (all caps, no spaces)
Examples
>>> from.cryptoimportencipher_bifid6,decipher_bifid6>>> key="gold bug">>> encipher_bifid6('meet me on monday at 8am',key)'KFKLJJHF5MMMKTFRGPL'>>> decipher_bifid6(_,key)'MEETMEONMONDAYAT8AM'
>>> key=Matrix([[1,2],[3,5]])>>> encipher_hill("meet me on monday",key)'UEQDUEODOCTCWQ'>>> decipher_hill(_,key)'MEETMEONMONDAY'
When the length of the plaintext (stripped of invalid characters)
is not a multiple of the key dimension, extra characters will
appear at the end of the enciphered and deciphered text. In order to
decipher the text, those characters must be included in the text to
be deciphered. In the following, the key has a dimension of 4 but
the text is 2 short of being a multiple of 4 so two characters will
be added.
Performs the affine cipher encryption on plaintext msg, and
returns the ciphertext.
Encryption is based on the map x rightarrow ax+b (mod N)
where N is the number of characters in the alphabet.
Decryption is based on the map x rightarrow cx+d (mod N),
where c = a^{-1} (mod N) and d = -a^{-1}b (mod N).
In particular, for the map to be invertible, we need
mathrm{gcd}(a, N) = 1 and an error will be raised if this is
not true.
Notes
This is a straightforward generalization of the shift cipher with
the added complexity of requiring 2 characters to be deciphered in
order to recover the key.
ALGORITHM:
INPUT:
msg: string of characters that appear in symbols
a,b: a pair integers, with gcd(a,N)=1
(the secret key)
symbols: string of characters (default = uppercase
letters). When no symbols are given, msg is converted
to upper case letters and all other charactes are ignored.
OUTPUT:
ct: string of characters (the ciphertext message)
STEPS:
Number the letters of the alphabet from 0, …, N
Compute from the string msg a list L1 of
corresponding integers.
Compute from the list L1 a new list L2, given by
replacing x by a*x+b(modN), for each element
x in L1.
Compute from the list L2 a string ct of
corresponding letters.
Performs the Bifid cipher encryption on plaintext msg, and
returns the ciphertext.
This is the version of the Bifid cipher that uses the 5 times 5
Polybius square. The letter “J” is ignored so it must be replaced
with something else (traditionally an “I”) before encryption.
Notes
The Bifid cipher was invented around 1901 by Felix Delastelle.
It is a fractional substitution cipher, where letters are
replaced by pairs of symbols from a smaller alphabet. The
cipher uses a 5 times 5 square filled with some ordering of the
alphabet, except that “J” is replaced with “I” (this is a so-called
Polybius square; there is a 6 times 6 analog if you add back in
“J” and also append onto the usual 26 letter alphabet, the digits
0, 1, …, 9).
According to Helen Gaines’ book Cryptanalysis, this type of cipher
was used in the field by the German Army during World War I.
ALGORITHM: (5x5 case)
INPUT:
msg: plaintext string; converted to upper case and
filtered of anything but all letters except J.
key: short string for key; non-alphabetic letters, J
and duplicated characters are ignored and then, if the
length is less than 25 characters, it is padded with other
letters of the alphabet (in alphabetical order).
OUTPUT:
ciphertext (all caps, no spaces)
STEPS:
Create the 5 times 5 Polybius square S associated
to key as follows:
moving from left-to-right, top-to-bottom,
place the letters of the key into a 5 times 5
matrix,
if the key has less than 25 letters, add the
letters of the alphabet not in the key until the
5 times 5 square is filled.
Create a list P of pairs of numbers which are the
coordinates in the Polybius square of the letters in
msg.
Let L1 be the list of all first coordinates of P
(length of L1=n), let L2 be the list of all
second coordinates of P (so the length of L2
is also n).
Let L be the concatenation of L1 and L2
(length L=2*n), except that consecutive numbers
are paired (L[2*i],L[2*i+1]). You can regard
L as a list of pairs of length n.
Let C be the list of all letters which are of the
form S[i,j], for all (i,j) in L. As a
string, this is the ciphertext of msg.
Performs the Bifid cipher encryption on plaintext msg, and
returns the ciphertext.
This is the version of the Bifid cipher that uses the 6 times 6
Polybius square.
INPUT:
msg: plaintext string (digits okay)
key: short string for key (digits okay). If key is
less than 36 characters long, the square will be filled with
letters A through Z and digits 0 through 9.
OUTPUT:
ciphertext from Bifid cipher (all caps, no spaces)
i is a plaintext message expressed as an integer.
key is public key (p, r, e). In order to encrypt
a message, a random number a in range(2,p)
is generated and the encryped message is returned as
c_{1} and c_{2} where:
The Hill cipher [1]_, invented by Lester S. Hill in the 1920’s [2]_,
was the first polygraphic cipher in which it was practical
(though barely) to operate on more than three symbols at once.
The following discussion assumes an elementary knowledge of
matrices.
First, each letter is first encoded as a number starting with 0.
Suppose your message msg consists of n capital letters, with no
spaces. This may be regarded an n-tuple M of elements of
Z_{26} (if the letters are those of the English alphabet). A key
in the Hill cipher is a k x k matrix K, all of whose entries
are in Z_{26}, such that the matrix K is invertible (i.e., the
linear transformation K: Z_{N}^k rightarrow Z_{N}^k
is one-to-one).
ALGORITHM:
INPUT:
msg: plaintext message of n upper-case letters
key: a k x k invertible matrix K, all of whose
entries are in Z_{26} (or whatever number of symbols
are being used).
pad: character (default “Q”) to use to make length
of text be a multiple of k
OUTPUT:
ct: ciphertext of upper-case letters
STEPS:
Number the letters of the alphabet from 0, …, N
Compute from the string msg a list L of
corresponding integers. Let n=len(L).
Break the list L up into t=ceiling(n/k)
sublists L_1, …, L_t of size k (with
the last list “padded” to ensure its size is
k).
Compute new list C_1, …, C_t given by
C[i]=K*L_i (arithmetic is done mod N), for each
i.
Concatenate these into a list C=C_1+...+C_t.
Compute from C a string ct of corresponding
letters. This has length k*t.
Performs shift cipher encryption on plaintext msg, and returns the
ciphertext.
Notes
The shift cipher is also called the Caesar cipher, after
Julius Caesar, who, according to Suetonius, used it with a
shift of three to protect messages of military significance.
Caesar’s nephew Augustus reportedly used a similar cipher, but
with a right shift of 1.
ALGORITHM:
INPUT:
key: an integer (the secret key)
msg: plaintext of upper-case letters
OUTPUT:
ct: ciphertext of upper-case letters
STEPS:
Number the letters of the alphabet from 0, …, N
Compute from the string msg a list L1 of
corresponding integers.
Compute from the list L1 a new list L2, given by
adding (kmod26) to each element in L1.
Compute from the list L2 a string ct of
corresponding letters.
Returns the ciphertext obtained by replacing each character that
appears in old with the corresponding character in new.
If old is a mapping, then new is ignored and the replacements
defined by old are used.
Notes
This is a more general than the affine cipher in that the key can
only be recovered by determining the mapping for each symbol.
Though in practice, once a few symbols are recognized the mappings
for other characters can be quickly guessed.
The substitution cipher, in general, is a method
whereby “units” (not necessarily single characters) of plaintext
are replaced with ciphertext according to a regular system.
Performs the Vigenère cipher encryption on plaintext msg, and
returns the ciphertext.
Examples
>>> from.cryptoimportencipher_vigenere,AZ>>> key="encrypt">>> msg="meet me on monday">>> encipher_vigenere(msg,key)'QRGKKTHRZQEBPR'
Section 1 of the Kryptos sculpture at the CIA headquarters
uses this cipher and also changes the order of the the
alphabet [2]_. Here is the first line of that section of
the sculpture:
The Vigenère cipher is named after Blaise de Vigenère, a sixteenth
century diplomat and cryptographer, by a historical accident.
Vigenère actually invented a different and more complicated cipher.
The so-called Vigenère cipher was actually invented
by Giovan Batista Belaso in 1553.
This cipher was used in the 1800’s, for example, during the American
Civil War. The Confederacy used a brass cipher disk to implement the
Vigenère cipher (now on display in the NSA Museum in Fort
Meade) [1]_.
The Vigenère cipher is a generalization of the shift cipher.
Whereas the shift cipher shifts each letter by the same amount
(that amount being the key of the shift cipher) the Vigenère
cipher shifts a letter by an amount determined by the key (which is
a word or phrase known only to the sender and receiver).
For example, if the key was a single letter, such as “C”, then the
so-called Vigenere cipher is actually a shift cipher with a
shift of 2 (since “C” is the 2nd letter of the alphabet, if
you start counting at 0). If the key was a word with two
letters, such as “CA”, then the so-called Vigenère cipher will
shift letters in even positions by 2 and letters in odd positions
are left alone (shifted by 0, since “A” is the 0th letter, if
you start counting at 0).
ALGORITHM:
INPUT:
msg: string of characters that appear in symbols
(the plaintext)
key: a string of characters that appear in symbols
(the secret key)
symbols: a string of letters defining the alphabet
OUTPUT:
ct: string of characters (the ciphertext message)
STEPS:
Number the letters of the alphabet from 0, …, N
Compute from the string key a list L1 of
corresponding integers. Let n1=len(L1).
Compute from the string msg a list L2 of
corresponding integers. Let n2=len(L2).
Break L2 up sequencially into sublists of size
n1; the last sublist may be smaller than n1
For each of these sublists L of L2, compute a
new list C given by C[i]=L[i]+L1[i](modN)
to the i-th element in the sublist, for each i.
Assemble these lists C by concatenation into a new
list of length n2.
Compute from the new list a string ct of
corresponding letters.
Once it is known that the key is, say, n characters long,
frequency analysis can be applied to every n-th letter of
the ciphertext to determine the plaintext. This method is
called Kasiski examination (although it was first discovered
by Babbage). If they key is as long as the message and is
comprised of randomly selected characters – a one-time pad – the
message is theoretically unbreakable.
The cipher Vigenère actually discovered is an “auto-key” cipher
described as follows.
ALGORITHM:
INPUT:
key: a string of letters (the secret key)
msg: string of letters (the plaintext message)
OUTPUT:
ct: string of upper-case letters (the ciphertext message)
STEPS:
Number the letters of the alphabet from 0, …, N
Compute from the string msg a list L2 of
corresponding integers. Let n2=len(L2).
Let n1 be the length of the key. Append to the
string key the first n2-n1 characters of
the plaintext message. Compute from this string (also of
length n2) a list L1 of integers corresponding
to the letter numbers in the first step.
Compute a new list C given by
C[i]=L1[i]+L2[i](modN).
Compute from the new list a string ct of letters
corresponding to the new integers.
To decipher the auto-key ciphertext, the key is used to decipher
the first n1 characters and then those characters become the
key to decipher the next n1 characters, etc…:
The lfsr sequence defined by
x_{n+1} = c_k x_n + ldots + c_0 x_{n-k}, for
n leq k.
Notes
S. Golomb [G] gives a list of three statistical properties a
sequence of numbers a = {a_n}_{n=1}^infty,
a_n in {0,1}, should display to be considered
“random”. Define the autocorrelation of a to be
\[\begin{split}C(k) = \left\{ \begin{array}{cc} 1,& k = 0,\\ \epsilon, & k \ne 0. \end{array} \right.\end{split}\]
(For sequences satisfying these first two properties, it is known
that epsilon = -1/P must hold.)
proportional runs property: In each period, half the runs have
length 1, one-fourth have length 2, etc.
Moreover, there are as many runs of 1’s as there are of
0’s.
Solomon Golomb, Shift register sequences, Aegean Park Press,
Laguna Hills, Ca, 1967
Examples
>>> from.cryptoimportlfsr_sequence>>> from..polys.domainsimportFF>>> F=FF(2)>>> fill=[F(1),F(1),F(0),F(1)]>>> key=[F(1),F(0),F(0),F(1)]>>> lfsr_sequence(key,fill,10)[1 mod 2, 1 mod 2, 0 mod 2, 1 mod 2, 0 mod 2,1 mod 2, 1 mod 2, 0 mod 2, 0 mod 2, 1 mod 2]
Return a string of the distinct characters of symbols with
those of key appearing first, omitting characters in key
that are not in symbols. A ValueError is raised if a) there are
duplicate characters in symbols or b) there are characters
in key that are not in symbols.
Examples
>>> from.cryptoimportpadded_key>>> padded_key('PUPPY','OPQRSTUVWXY')'PUYOQRSTVWX'>>> padded_key('RSA','ARTIST')Traceback (most recent call last):...ValueError: duplicate characters in symbols: T
modelparameters.sympy.crypto.crypto.rsa_private_key(p, q, e)[source]¶
Return the RSA private key, (n,d), where n
is a product of two primes and d is the inverse of
e (mod phi(n)). False is returned if any assumption
is violated.
modelparameters.sympy.crypto.crypto.rsa_public_key(p, q, e)[source]¶
Return the RSA public key pair, (n, e), where n
is a product of two primes and e is relatively
prime (coprime) to the Euler totient phi(n). False
is returned if any assumption is violated.