DE
ctp
cto
ct2
ctts
ct1
jct
scr
ctb
HomepageContributorsLinks / Books
Back to the CrypTool Book

SageMath and Python Code

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

If you have SageMath and Python 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.sage1.13.1 Encryption and decryption with Mini-AES

Chapter 2

chap02_sample010.sage2.9.1 Simple transposition by shifting the letters in the message (key is constructed with “range”)
chap02_sample020.sage2.9.2 Simple transposition by shifting (key and inverse key are constructed)
chap02_sample030.sage2.9.3 Simple column transposition just reverting each plaintext block
chap02_sample040.sage2.9.4 Simple column transposition with randomly generated (permutation) key and showing the size of the key space
chap02_sample050.sage2.9.5 Simple column transposition (demonstrates encoding and ensures correct message length)
chap02_sample060.sage2.9.6 Monoalphabetic substitution with randomly generated key
chap02_sample065.sage2.9.7 Atbash (substitution by reverting the alphabet)
chap02_sample070.sage2.9.8 Caesar (substitution by shifting the alphabet; key explicitly given, step-by-step approach)
chap02_sample080.sage2.9.9 Constructing the Caesar cipher using the built-in shift cipher
chap02_sample090.sage2.9.11 An affine cipher with key (3, 13)
chap02_sample100.sage2.9.12 Constructing the Caesar cipher using the affine cipher
chap02_sample110.sage2.9.13 Vigenère cipher
chap02_sample120.sage2.9.14 Hill cipher with given key matrix
chap02_sample130.sage2.9.15 Monoalphabetic substitution with a hexadecimal alphabet (and decoding in both SageMath and Python)
chap02_sample140.sage2.9.16 Different substitution ciphers on the binary alphabet
chap02_sample150.sage2.9.17 Monoalphabetic substitution (MASC) over own alphabet
chap02_sample160.sage2.9.18 Ciphertext-only attack against shift cipher (Caesar)
chap02_hill_enc_dec_kpa.sage2.9.19 Known-plaintext analysis (KPA) against the Hill cipher

Chapter 4

chap04_sample010.sage4.7.2 Double Mersenne primes
chap04_sample015.sage4.7.1 Euclid numbers (with +1 and -1)
chap04_sample020.sage4.14.1 Special values of the binary and decimal systems
chap04_sample030.sage4.15.1 Generation of the graphs of the three functions x, log(x) and x/log(x)
chap04_sample040.sage4.16.2 Verify the primality of integers generated by a quadratic function
chap04_sample050.sage4.16.3 Primality of results of a quadratic function (variable n runs till 1000)
chap04_sample060.sage4.10.1 Calculate the k primorials (k#) for Table 4.9
chap04_sample070.sage4.6.2 Find Carmichael numbers up to n=10000 (plus some more information)
chap04_sample080.sage4.6.3 Find all Carmichael numbers up to n=100000
chap04_sample100.sagePrint Table 4.10: Gaps between the first primes (gap length gi = n)
chap04_sample110.sage4.10.3 List of first maximal prime gaps with merits
chap04_Mills_primes.sage4.10.2 Calculate Mill's primes

Chapter 5

chap05_sample010.sage5.12.1 Comparing the execution time for calculating a gcd and performing a factorization
chap05_sample013.sage5.8.1 Square and multiply done manually in SageMath
chap05_sample017.sage5.2.1 Z, Zn, and Zn∗ in SageMath
chap05_sample020.sage5.13.1 Sample with small numbers: calculating the discrete logs a and b in order to attack DH
chap05_sample025.sage5.17.1 Creating small addition and multiplication tables with SageMath
chap05_sample030.sage5.17.2 Multiplication tables for a·i (mod m) with m = 17, a = 5 and a = 6
chap05_sample040.sage5.17.3 Fast exponentiation of a^e mod m = 103
chap05_sample050.sage5.17.4 Table with all powers a^i (mod m) for m = 11, a,i = 1, ..., 10
chap05_sample060.sage5.17.5 Table with all powers a^i (mod 45) for a,i = 1, ..., 12 plus the order of a
chap05_sample070.sage5.17.6 Table with all powers a^i (mod 46) for a,i = 1, ..., 23 plus the order of a
chap05_sample080.sage5.17.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.sage5.17.8 Calculating one primitive root for a given number
chap05_sample100.sage5.17.9 Function "enum_PrimitiveRoots_of_an_Integer" to calculate all primitive roots for a given number
chap05_sample110.sage5.17.10 Function "count_PrimitiveRoots_of_an_IntegerRange" to calculate the number of all primitive roots for a given range of integers
chap05_sample120.sage5.17.11 Function "count_PrimitiveRoots_of_a_PrimesRange" to calculate the number of primitive roots for a given range of primes
chap05_sample130.sage5.17.12 Code to generate the database with all primitive roots for all primes between 1 and 100000
chap05_sample140.sage5.17.13 Code to generate the database with the smallest primitive root for all primes between 1 and 1000000
chap05_sample150.sage5.17.15 Code to generate the graphics about the primitive roots
chap05_sample160.sage5.17.22 How many private RSA keys d are there if you know a range for the public key n?
chap05_sample170.sage5.17.23 Determine all fixed points for a specific public RSA key
chap05_sample180.sage5.17.16 Code to generate an in Zn invertible element of maximal order
chap05_sample_RSA.sageComplete program: RSA (generate primes, generate keys, encrypt and decrypt integers and ASCII strings)

Chapter 6

chap06_sample010.sage6.5.1 Generation of Table 6.4 with SageMath

Chapter 9

chap09_sample010.sage9.1.1 Solution of a system of linear equations over Q
chap09_sample020.sage9.1.2 Solution of a system of Boolean linear equations
chap09_sample030.sage9.1.3 A Boolean function with truth table and ANF
chap09_sample040.sage9.2.1 Plot of I/O-correlation and potential
chap09_sample050.sage9.2.2 A Boolean map (the S-box S0 of Lucifer)
chap09_sample060.sage9.2.3 An example of Matsui’s test
chap09_sample070.sage9.2.4 Correlation matrix, approximation table, and linear profile of the S-box S0
chap09_sample080.sage9.2.5 Linear profile of the S-box S0 with evaluation
chap09_sample090.sage9.2.6 A Boolean map (S-box S1 of Lucifer)
chap09_sample100.sage9.2.7 Calculations for example B (two-round cipher)
chap09_sample110.sage9.2.8 Dependence of the probability on the key
chap09_sample120.sage9.2.12 Linear cryptanalysis of Mini-Lucifer over 2 rounds
chap09_sample130.sage9.3.1 XOR encryption in Python/SageMath
chap09_sample140.sage9.3.3 A (very poor) pseudorandom sequence in Python/SageMath
chap09_sample150.sage9.3.5 A pseudorandom bit sequence in Python/SageMath
chap09_sample160.sage9.3.6 Determining a coefficient matrix
chap09_sample170.sage9.3.7 The Geffe function
chap09_sample180.sage9.3.8 Calculating a period
chap09_sample190.sage9.3.9 Three LFSRs
chap09_sample200.sage9.3.10 Three LFSR sequences
chap09_sample210.sage9.3.11 The combined sequence
chap09_sample210_without-prints.sagesample210 which is loaded in other sage scripts like chap09_sample230.sage
chap09_sample220.sageLinear profile of the Geffe function
chap09_sample230.sageCoincidences for the Geffe generator
chap09_sample240.sageAnalysis of the Geffe generator – register 1
chap09_sample250.sageAnalysis of the Geffe generator – register 2
chap09_sample260.sage9.3.12 A (much too) simple example for BBS
chap09_sample270.sage9.3.13 Generating a sequence of BBS pseudorandom bits

Appendix Chapter Sage

SageAppendix_sample010.sageSmall samples from different areas in mathematics (1)
SageAppendix_sample020.sageSmall samples from different areas in mathematics (2)
SageJupyter_sample010.sage, sageJNB_sample010.ipynbCreate html code containing Sage and LaTeX commands
SageJupyter_sample020.sage, sageJNB_sample020.ipynbStarting sample using interact in a Jupyter notebook
SageJupyter_sample030.sage, sageJNB_sample030.ipynbGraph with vertical pole and parameters for different curves
SageJupyter_sample040.sage, sageJNB_sample040.ipynbGraph with vertical pole and parameters and generic function (without fig.)
SageJupyter_sample050.sage, sageJNB_sample050.ipynbCode generating the graph for MTW points over time
SageJupyter_sample060.sage, sageJNB_sample060.ipynbCode for graph with widgets from ipywidgets
SageJupyter_sample070.sage, sageJNB_sample070.ipynbCode to generate the LaTeX command of an equation
mwe-arara.texMinimal working example for arara and sagetex

Sage Miscellaneous

bitciphers.sageBit Ciphers

Python

chap03_sample10.py3.5.1 Cost value with LanguageStatisticsLibPy in Python
chap09_sample153.py9.3.1 LFSR with the pylfsr package in Python
Appendix-B_sample010.pyB.1.1 Decryption of the Gold-Bug ciphertext from the novel of E.A. Poe

Other

LFSRseq.pngoutput created by sage script in Chapter 9
primroots.datdat file created by sage script in Chapter 4
primroot-smallest_up-to-one-million.datdat file created by sage script in Chapter 4