Cryptography

Classic Cryptosystems

Classical Encryption and Decryption Algorithms

  • ShiftCryptosystem

  • AffineCryptosystem

  • PermutationCryptosystem

  • SubstitutionCryptosystem

  • VernamCryptosystem

  • VigenereCryptosystem

  • HillCryptosystem

class simula.crypto.classic.AffineCryptosystem(alphabet=('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'), block_length=1)

Affine Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = (5, 11)
simula : C = AffineCryptosystem.encipher(M, k); C
'CDIXFVSFC'
simula : AffineCryptosystem.decipher(C, k)
'TOPSECRET'
classmethod decipher(cipher, key)

Decryption algorithm for Affine Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = (5, 11)
simula : C = AffineCryptosystem.encipher(M, k); C
'CDIXFVSFC'
simula : AffineCryptosystem.decipher(C, k)
'TOPSECRET'
Parameters
  • cipher (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[str, int]) –

Return type

str

classmethod encipher(message, key)

Encryption algorithm for Affine Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = (5, 11)
simula : C = AffineCryptosystem.encipher(M, k); C
'CDIXFVSFC'
Parameters
  • message (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[str, int]) –

Return type

str

class simula.crypto.classic.HillCryptosystem(alphabet=('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'), block_length=1)

Hill Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = matrix([[11, 25, 25], [12, 16, 3], [11, 16, 14]])
simula : C = HillCryptosystem.encipher(M, k); C
'WDZIAWCNB'
simula : HillCryptosystem.decipher(C, k)
'TOPSECRET'
classmethod decipher(cipher, key)

Decryption algorithm or Hill Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = matrix([[11, 25, 25], [12, 16, 3], [11, 16, 14]])
simula : C = HillCryptosystem.encipher(M, k); C
'WDZIAWCNB'
simula : HillCryptosystem.decipher(C, k)
'TOPSECRET'
Parameters
  • cipher (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[str, int]) –

Return type

str

classmethod encipher(message, key)

Encryption algorithm or Hill Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = matrix([[11, 25, 25], [12, 16, 3], [11, 16, 14]])
simula : C = HillCryptosystem.encipher(M, k); C
'WDZIAWCNB'
Parameters
  • message (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[str, int]) –

Return type

str

class simula.crypto.classic.PermutationCryptosystem(alphabet=('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'), block_length=1)

Permutation Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = {0: 2, 1: 0, 2: 1}
simula : C = PermutationCryptosystem.encipher(M, k); C
'PTOCSETRE'
simula : PermutationCryptosystem.decipher(C, k)
'TOPSECRET'
simula : k2 = {0: 2, 1: 5, 2: 4, 3: 1, 4: 6, 5: 3, 6: 8, 7: 0, 8: 7}
simula : C2 = PermutationCryptosystem.encipher(M, k2); C2
'PCEORSTTE'
classmethod decipher(cipher, key)

Decryption algorithm for Permutation Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = {0: 2, 1: 0, 2: 1}
simula : C = PermutationCryptosystem.encipher(M, k); C
'PTOCSETRE'
simula : PermutationCryptosystem.decipher(C, k)
'TOPSECRET'
Parameters
  • cipher (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (dict) –

Return type

str

classmethod encipher(message, key)

Encryption algorithm for Permutation Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = {0: 2, 1: 0, 2: 1}
simula : C = PermutationCryptosystem.encipher(M, k); C
'PTOCSETRE'
Parameters
  • message (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (dict) –

Return type

str

class simula.crypto.classic.ShiftCryptosystem(alphabet=('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'), block_length=1)

Shift Cryptosystem.

EXAMPLES:

simula : M, k = "TOPSECRET", 7
simula : C = ShiftCryptosystem.encipher(M, k); C
'AVWZLJYLA'
simula : ShiftCryptosystem.decipher(C, k)
'TOPSECRET'
classmethod decipher(cipher, key)

Decryption algorithm for Shift Cryptosystem.

EXAMPLES:

simula : M, k = "TOPSECRET", 7
simula : C = ShiftCryptosystem.encipher(M, k); C
'AVWZLJYLA'
simula : ShiftCryptosystem.decipher(C, k)
'TOPSECRET'
Parameters
  • cipher (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[str, int]) –

Return type

str

classmethod encipher(message, key)

Encryption algorithm for Shift Cryptosystem.

EXAMPLES:

simula : M, k = "TOPSECRET", 7
simula : C = ShiftCryptosystem.encipher(M, k); C
'AVWZLJYLA'
Parameters
  • message (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[str, int]) –

Return type

str

classmethod keygen()

Returns randomly a key for the Shift Cryptosystem.

class simula.crypto.classic.SubstitutionCryptosystem(alphabet=('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'), block_length=1)

Substitution Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = {0: 20, 1: 24, 2: 12, 3: 7, 4: 22, 5: 0, 6: 1, 7: 16, 8: 6, 9: 9, 10: 17, 11: 15,
 12: 4, 13: 18, 14: 23, 15: 8, 16: 19, 17: 13, 18: 2, 19: 3, 20: 21, 21: 11, 22: 5, 23: 10, 24: 25, 25: 14}
simula : C = SubstitutionCryptosystem.encipher(M, k); C
'DXICWMNWD'
simula : SubstitutionCryptosystem.decipher(C, k)
'TOPSECRET'
classmethod decipher(cipher, key)

Decryption algorithm for Substitution Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = {0: 20, 1: 24, 2: 12, 3: 7, 4: 22, 5: 0, 6: 1, 7: 16, 8: 6, 9: 9, 10: 17, 11: 15,
12: 4, 13: 18, 14: 23, 15: 8, 16: 19, 17: 13, 18: 2, 19: 3, 20: 21, 21: 11, 22: 5, 23: 10, 24: 25, 25: 14}
simula : C = SubstitutionCryptosystem.encipher(M, k); C
'DXICWMNWD'
simula : SubstitutionCryptosystem.decipher(C, k)
'TOPSECRET'
Parameters
  • cipher (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (dict) –

Return type

str

classmethod encipher(message, key)

Encryption algorithm for Substitution Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = {0: 20, 1: 24, 2: 12, 3: 7, 4: 22, 5: 0, 6: 1, 7: 16, 8: 6, 9: 9, 10: 17, 11: 15,
12: 4, 13: 18, 14: 23, 15: 8, 16: 19, 17: 13, 18: 2, 19: 3, 20: 21, 21: 11, 22: 5, 23: 10, 24: 25, 25: 14}
simula : C = SubstitutionCryptosystem.encipher(M, k); C
'DXICWMNWD'
Parameters
  • message (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (dict) –

Return type

str

class simula.crypto.classic.VernamCryptosystem(alphabet=('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'), block_length=1)

Vernam Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = (23, 13, 25, 22, 2, 16, 9, 11, 7)
simula : C = VernamCryptosystem.encipher(M, k); C
'QBOOGSAPA'
simula : VernamCryptosystem.decipher(C, k)
'TOPSECRET'
classmethod decipher(cipher, key)

Decryption algorithm for Vernam Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = (23, 13, 25, 22, 2, 16, 9, 11, 7)
simula : C = VernamCryptosystem.encipher(M, k); C
'QBOOGSAPA'
simula : VernamCryptosystem.decipher(C, k)
'TOPSECRET'
Parameters
  • cipher (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[Tuple[str], Tuple[int]]) –

Return type

str

classmethod encipher(message, key)

Encryption algorithm for Vernam Cryptosystem.

EXAMPLES:

simula : M = "TOPSECRET"
simula : k = (23, 13, 25, 22, 2, 16, 9, 11, 7)
simula : C = VernamCryptosystem.encipher(M, k); C
'QBOOGSAPA'
Parameters
  • message (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[Tuple[str], Tuple[int]]) –

Return type

str

class simula.crypto.classic.VigenereCryptosystem(alphabet=('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'), block_length=1)

Vigenere Cryptosystem.

EXAMPLES:

simula : M, k = "TOPSECRET", (12, 16, 16)
simula : C = VigenereCryptosystem.encipher(M, k); C
''FEFEUSDUJ'
simula : VigenereCryptosystem.decipher(C, k)
'TOPSECRET'
classmethod decipher(cipher, key)

Decryption algorithm for Vigenere Cryptosystem.

EXAMPLES:

simula : M, k = "TOPSECRET", (12, 16, 16)
simula : C = VigenereCryptosystem.encipher(M, k); C
'FEFEUSDUJ'
simula : VigenereCryptosystem.decipher(C, k)
'TOPSECRET'
Parameters
  • cipher (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[Tuple[str], Tuple[int]]) –

Return type

str

classmethod encipher(message, key)

Encryption algorithm for Vigenere Cryptosystem.

EXAMPLES:

simula : M, k = "TOPSECRET", (12, 16, 16)
simula : C = VigenereCryptosystem.encipher(M, k); C
'FEFEUSDUJ'
Parameters
  • message (Union[str, List[int], List[str], Tuple[int], Tuple[str]]) –

  • key (Union[Tuple[str], Tuple[int]]) –

Return type

str

simula.crypto.classic.ascii_letters()

Returns the ASCII letters.

simula.crypto.classic.clean_text(message, alphabet=None)

Transforms or deletes all non-ascii letters.

Parameters

message (str) –

Return type

str

Asymmetric Schemes

Public key encryption and Signature schemes.

  • RSA (encryption and signature)

  • ElGamal (encryption and signature)

  • DSA (Digital Signature Algorithm)

class simula.crypto.asymmetric.DSA

Digital Signature Algorithm (DSA)

See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf

classmethod keygen(N=None, L=None)

Returns a keypair sk = (p, q, g, x)` and ``pk = (p, q, g, y) when p and q have size respectively L and N.

Parameters
  • N (Optional[int]) –

  • L (Optional[int]) –

classmethod signing(message, sk, hash_func=None)

DSA signing algorithm.

Parameters
  • message (int) – an integer

  • sk (Tuple[int, int, int, int]) – the private key sk = (p, q, g, x)

  • hash_func – (optional) message digest

Return type

Tuple[int, int]

classmethod verifier(sign, pk, message, hash_func=None)

DSA Verification algorithm.

Parameters
  • sign (Tuple[int, int]) – an integer

  • pk (Tuple[int, int, int, int]) – the public key pk = (p, q, g, y)

  • message (int) – an integer

  • hash_func – (optional) message digest

Return type

bool

class simula.crypto.asymmetric.ECDSA

Elliptic Curve Digital Signature Algorithm (ECDSA)

classmethod keygen(*, size=None, p=None, ec_coeffs=None)

Returns a keypair sk = (q, E, G, x)` and ``pk = (q, E, G, Y) when p has size size.

Parameters
  • size (Optional[int]) –

  • p (Optional[int]) –

classmethod signing(message, sk, hash_func=None)

ECDSA signing algorithm.

Parameters
  • message (int) – an integer

  • sk (Tuple[int, simula.hecc.weirstrass.EllipticCurve, Union[simula.hecc.curve.EllipticCurvePoint, Tuple], int]) – the private key sk = (q, E, G, x)

  • hash_func – (optional) message digest

Return type

Tuple[int, int]

classmethod verifier(sign, pk, message, hash_func=None)

ECDSA Verification algorithm.

Parameters
Return type

bool

class simula.crypto.asymmetric.ElGamal

El Gamal scheme : Encryption and Signature.

EXAMPLES:

simula : scheme = ElGamal()
simula : p, a = 11, 7
simula : p, g, ga = 11, 2, 7
simula : sk, pk = (p, a), (p, g, ga)
simula : c = scheme.encipher(30, pk); c
(7, 3)
simula : scheme.decipher(c, sk)
3
simula : ElGamal.decipher(ElGamal.encipher(5, pk), sk) == 5
True
classmethod decipher(cipher, sk)

El Gamal decipher algorithm.

Parameters
  • cipher (int) – an integer

  • sk (int) – the private key sk = (p, a)

Return type

int

classmethod encipher(message, pk)

El Gamal encipher algorithm.

Parameters
  • message (int) – an integer

  • pk (int) – the public key pk = (p, g, ga)`

Return type

Tuple[int, int]

classmethod keygen(size=None, sign=False)

Returns a keypair sk = (p, a) or (p, g, a) and pk = (p, g, ga) when p has size size.

Parameters

size (Optional[int]) –

classmethod signing(message, sk, hash_func=None)

El Gamal signing algorithm.

Parameters
  • message (int) – an integer

  • sk (Tuple[int, int, int]) – the private key sk = (p, g, a)

  • hash_func – (optional) message digest

Return type

Tuple[int, int]

classmethod verifier(sign, pk, message, hash_func=None)

El Gamal Verification algorithm.

Parameters
  • sign (Tuple[int, int]) – an integer

  • pk (Tuple[int, int, int]) – the public key pk = (p, g, ga)

  • message (int) – an integer

  • hash_func – (optional) message digest

Return type

bool

class simula.crypto.asymmetric.RSA

RSA scheme : Encryption and Signature.

EXAMPLES:

simula : scheme = RSA()
simula : p, q, d = 11, 13, 107
simula : N, e = 143, 83
simula : sk, pk = (p, q, d), (N, e)
simula : c = scheme.encipher(30, pk); c
127
simula : scheme.decipher(c, sk)
30
simula : RSA.decipher(RSA.encipher(58, pk), sk) == 58
True
classmethod decipher(cipher, sk)

RSA decipher algorithm.

Parameters
  • cipher (int) – an integer

  • sk (Union[Tuple[int, int], Tuple[int, int, int]]) – the private key sk = (p, q, d)

Return type

int

classmethod encipher(message, pk)

RSA encipher algorithm.

Parameters
  • message (int) – an integer

  • pk (Tuple[int, int]) – the public key pk = (N, e)`

Return type

int

classmethod keygen(size=None)

Returns a keypair sk = (p, q, d) and pk = (N, e) when p and q have size size.

Parameters

size (Optional[int]) –

classmethod signing(message, sk, hash_func=None)

RSA signing algorithm.

Parameters
  • message (int) – an integer

  • sk (Union[Tuple[int, int], Tuple[int, int, int]]) – the private key sk = (p, q, d)

  • hash_func – (optional) message digest

Return type

int

classmethod verifier(sign, pk, message, hash_func=None)

RSA Verification algorithm.

Parameters
  • sign (int) – an integer

  • pk (Tuple[int, int]) – the public key pk = (N, e)

  • message (int) – an integer

  • hash_func – (optional) message digest

Return type

int

Schemes based on Elliptic Curves

class simula.crypto.ecc.ECDSA

Elliptic Curve Digital Signature Algorithm (ECDSA)

classmethod keygen(*, size=None, p=None, ec_coeffs=None)

Returns a keypair sk = (q, E, G, x)` and ``pk = (q, E, G, Y) when p has size size.

Parameters
  • size (Optional[int]) –

  • p (Optional[int]) –

classmethod signing(message, sk, hash_func=None)

ECDSA signing algorithm.

Parameters
  • message (int) – an integer

  • sk (Tuple[int, simula.hecc.weirstrass.EllipticCurve, Union[simula.hecc.curve.EllipticCurvePoint, Tuple], int]) – the private key sk = (q, E, G, x)

  • hash_func – (optional) message digest

Return type

Tuple[int, int]

classmethod verifier(sign, pk, message, hash_func=None)

ECDSA Verification algorithm.

Parameters
Return type

bool