## SageMath and Python Code

Below you find the SageMath and Python scripts from the CrypTool Book.

If SageMath and Python are installed, you can run the scripts in a terminal via commands like:

`sage chap02_sample020.sage`

`sage chap02_hill_enc_dec_kpa.sage`

`sage chap05_sample_RSA.sage`

`python3 Appendix-B_sample010.py`

## Chapter 1

chap01_sample010.sage | 1.12.1 Encryption and decryption with Mini-AES |

## Chapter 2

chap02_sample010.sage | 2.8.1 Simple transposition by shifting the letters in the message (key is constructed with “range”) |

chap02_sample020.sage | 2.8.2 Simple transposition by shifting (key and inverse key are constructed) |

chap02_sample030.sage | 2.8.3 Simple column transposition just reverting each plaintext block |

chap02_sample040.sage | 2.8.4 Simple column transposition with randomly generated (permutation) key and showing the size of the key space |

chap02_sample050.sage | 2.8.5 Simple column transposition (demonstrates encoding and ensures correct message length) |

chap02_sample060.sage | 2.8.6 Monoalphabetic substitution with randomly generated key |

chap02_sample065.sage | 2.8.7 Atbash (substitution by reverting the alphabet) |

chap02_sample070.sage | 2.8.8 Caesar (substitution by shifting the alphabet; key explicitly given, step-by-step approach) |

chap02_sample080.sage | 2.8.9 Constructing the Caesar cipher using the shift cipher |

chap02_sample090.sage | 2.8.11 An affine cipher with key (3, 13) |

chap02_sample100.sage | 2.8.12 Constructing the Caesar cipher using the affine cipher |

chap02_sample110.sage | 2.8.13 Vigenère cipher |

chap02_sample120.sage | 2.8.14 Hill cipher with given key matrix |

chap02_sample130.sage | 2.8.15 Monoalphabetic substitution with a hexadecimal alphabet (and decoding in both SageMath and Python) |

chap02_sample140.sage | 2.8.16 Different substitution ciphers on the binary alphabet |

chap02_sample150.sage | 2.8.17 Monoalphabetic substitution (MASC) over own alphabet |

chap02_sample160.sage | 2.8.18 Ciphertext-only attack against shift cipher (Caesar) |

chap02_hill_enc_dec_kpa.sage | 2.8.19 Complete program: Known-plaintext analysis (KPA) against the Hill cipher |

## Chapter 4

chap04_sample010.sage | 4.6.2 Double Mersenne primes |

chap04_sample015.sage | 4.6.1 Euclid numbers with -1 |

chap04_sample020.sage | 4.13.1 Special values of the binary and decimal systems |

chap04_sample030.sage | 4.14.1 Generation of the graphs of the three functions x, log(x) and x/log(x) |

chap04_sample040.sage | 4.15.2 Verify the primality of integers generated by a quadratic function |

chap04_sample050.sage | 4.15.3 Primality of results of a quadratic function (variable n runs till 1000) |

chap04_sample060.sage | 4.9.1 Calculate the k primorials (k#) for Table 4.8 |

chap04_sample070.sage | 4.5.2 Find Carmichael numbers up to n=10000 (plus some more information) |

chap04_sample080.sage | 4.5.3 Find all Carmichael numbers up to n=100000 |

chap04_sample100.sage | - Print Table 4.9 (Gaps between the first primes with gap length gi = n) |

chap04_sample110.sage | 4.9.2 List of first maximal prime gaps with merits |

## Chapter 5

chap05_sample010.sage | 5.12.1 Comparing the execution time for calculating a gcd and performing a factorization |

chap05_sample013.sage | 5.8.1 Square and multiply done manually in SageMath |

chap05_sample017.sage | 5.2.1 Z, Zn, and Zn∗ in SageMath |

chap05_sample020.sage | 5.13.1 Sample with small numbers: calculating the discrete logs a and b in order to attack DH |

chap05_sample025.sage | 5.20.1 Creating small addition and multiplication tables with SageMath |

chap05_sample030.sage | 5.20.2 Multiplication tables for a·i (mod m) with m = 17, a = 5 and a = 6 |

chap05_sample040.sage | 5.20.3 Fast exponentiation of a^e mod m = 103 |

chap05_sample050.sage | 5.20.4 Table with all powers a^i (mod m) for m = 11, a,i = 1, ..., 10 |

chap05_sample060.sage | 5.20.5 Table with all powers a^i (mod 45) for a,i = 1, ..., 12 plus the order of a |

chap05_sample070.sage | 5.20.6 Table with all powers a^i (mod 46) for a,i = 1, ..., 23 plus the order of a |

chap05_sample080.sage | 5.20.7 Code for tables with all powers a^i (mod m) for variables a and i plus order of a and Eulerphi of m |

chap05_sample090.sage | 5.20.8 Calculating one primitive root for a given number |

chap05_sample100.sage | 5.20.9 Function "enum_PrimitiveRoots_of_an_Integer" to calculate all primitive roots for a given number |

chap05_sample110.sage | 5.20.10 Function "count_PrimitiveRoots_of_an_IntegerRange" to calculate the number of all primitive roots for a given range of integers |

chap05_sample120.sage | 5.20.11 Function "count_PrimitiveRoots_of_a_PrimesRange" to calculate the number of primitive roots for a given range of primes |

chap05_sample130.sage | 5.20.12 Code to generate the database with all primitive roots for all primes between 1 and 100000 |

chap05_sample140.sage | 5.20.13 Code to generate the database with the smallest primitive root for all primes between 1 and 1000000 |

chap05_sample150.sage | 5.20.15 Code to generate the graphics about the primitive roots |

chap05_sample160.sage | 5.20.22 How many private RSA keys d are there if you know a range for the public key n? |

chap05_sample170.sage | 5.20.23 Determine all fixed points for a specific public RSA key |

chap05_sample180.sage | 5.17.16 Code to generate an in Zn invertible element of maximal order |

chap05_sample_RSA.sage | Complete program: RSA (generate primes, generate keys, encrypt and decrypt integers and ASCII strings) |

## Chapter 6

chap06_sample010.sage | 6.5.1 Generation of Table 6.4 with SageMath |

## Chapter 9

chap09_sample010.sage | 9.1.1 Solution of a system of linear equations over Q |

chap09_sample020.sage | 9.1.2 Solution of a system of Boolean linear equations |

chap09_sample030.sage | 9.1.3 A Boolean function with truth table and ANF |

chap09_sample040.sage | 9.2.1 Plot of I/O-correlation and potential |

chap09_sample050.sage | 9.2.2 A Boolean map (the S-box S0 of Lucifer) |

chap09_sample060.sage | 9.2.3 An example of Matsui’s test |

chap09_sample070.sage | 9.2.4 Correlation matrix, approximation table, and linear profile of the S-box S0 |

chap09_sample080.sage | 9.2.5 Linear profile of the S-box S0 with evaluation |

chap09_sample090.sage | 9.2.6 A Boolean map (S-box S1 of Lucifer) |

chap09_sample100.sage | 9.2.7 Calculations for example B (two-round cipher) |

chap09_sample110.sage | 9.2.8 Dependence of the probability on the key |

chap09_sample120.sage | 9.2.12 Linear cryptanalysis of Mini-Lucifer over 2 rounds |

chap09_sample130.sage | 9.3.1 XOR encryption in Python/SageMath |

chap09_sample140.sage | 9.3.3 A (very poor) pseudorandom sequence in Python/SageMath |

chap09_sample150.sage | 9.3.5 A pseudorandom bit sequence in Python/SageMath |

chap09_sample160.sage | 9.3.6 Determining a coefficient matrix |

chap09_sample170.sage | 9.3.7 The Geffe function |

chap09_sample180.sage | 9.3.8 Calculating a period |

chap09_sample190.sage | 9.3.9 Three LFSRs |

chap09_sample200.sage | 9.3.10 Three LFSR sequences |

chap09_sample210.sage | 9.3.11 The combined sequence |

chap09_sample210_without-prints.sage | sample210 which is loaded in other sage scripts like chap09_sample230.sage |

chap09_sample220.sage | 9.3.12 Linear profile of the Geffe function |

chap09_sample230.sage | 9.3.13 Coincidences for the Geffe generator |

chap09_sample240.sage | 9.3.14 Analysis of the Geffe generator – register 1 |

chap09_sample250.sage | 9.3.15 Analysis of the Geffe generator – register 2 |

chap09_sample260.sage | 9.3.16 A (much too) simple example for BBS |

chap09_sample270.sage | 9.3.17 Generating a sequence of BBS pseudorandom bits |

## Appendix Chapter Sage

SageAppendix_sample010.sage | A.6.1 Small samples from different areas in mathematics (1) |

SageAppendix_sample020.sage | A.6.2 Small samples from different areas in mathematics (2) |

SageJupyter_sample010.sage, sageJNB_sample010.ipynb | A.6.11 Create html code containing Sage expressions and LaTeX code |

SageJupyter_sample020.sage, sageJNB_sample020.ipynb | A.6.14 Starting sample using interact in a Jupyter notebook |

SageJupyter_sample030.sage, sageJNB_sample030.ipynb | A.6.15 Graph with vertical pole and parameters for different curves |

SageJupyter_sample040.sage, sageJNB_sample040.ipynb | A.6.16 Graph with vertical pole and parameters and generic function |

SageJupyter_sample050.sage, sageJNB_sample050.ipynb | A.6.18 Code generating the graph for MTC3 points over time |

SageJupyter_sample060.sage, sageJNB_sample060.ipynb | A.6.17 Code for graph with widgets from ipywidgets (compare Fig. A.6.15) |

SageJupyter_sample070.sage, sageJNB_sample070.ipynb | A.6.12 Code to generate the LaTeX code of an equation |

mwe-arara.tex | A.6.13 Minimal working example for arara and sagetex |

## Sage Miscellaneous

bitciphers.sage | Bit Ciphers |

## Python

Appendix-B_sample010.py | B.1.1 Decryption of the Gold-Bug ciphertext from the novel of E.A. Poe (with Python) |

chap09_sample153.py | 9.3.1 LFSR with the pylfsr package in Python |

## Other

LFSRseq.png | output created by sage script in Chapter 9 |

primroots.dat | dat file created by sage script in Chapter 4 |

primroot-smallest_up-to-one-million.dat | dat file created by sage script in Chapter 4 |