Skip to main content
IoT
PSUT Engineering StudioPrincess Sumaya University for Technology
Student Edition
Cyber Defense StudioIoT Security and PrivacyExperimentsWorkspaces
Home / IoT Security and Privacy / Cryptography Engineering
IoTPSUT IoT Engineering StudioPrincess Sumaya University for Technology · Dr. Mohammad Alja’afreh
CRYPTOGRAPHY ENGINEERING · CR07 · MULTIPLE ENCRYPTION
IoT Security and Privacy

DES · double encryption and Triple DES EDE

Double DES uses \(E_{2}\)(\(E_{1}\)(P)); two-key TDEA uses \(E_{1}\)(\(D_{2}\)(\(E_{1}\)(P))) and three-key TDEA uses \(E_{3}\)(\(D_{2}\)(\(E_{1}\)(P))).

CR07 · Laboratory mentorAsk about the current operation and its intermediate values.
CR07 · Academic orientation

Scope and objectives

Scope of the experiment

Double DES applies two encryptions; Triple DES uses encrypt–decrypt–encrypt to permit historical compatibility. Reversing a composition requires reversing both order and each operation.

The focus of this experiment is to trace double encryption and Triple DES EDE order. Students then derive the cancellation case when all keys coincide, and verify inverse DED recovery and explain historical security limitations. The Concepts view explains the operation before the practical work; the Simulation view is used to examine reference and changed inputs, not to substitute for actual measurements.

Learning objectives

  • Trace double encryption and Triple DES EDE order.
  • Derive the cancellation case when all keys coincide.
  • Verify inverse DED recovery and explain historical security limitations.
  • Record the relevant inputs, intermediate states and final verification result before changing the reference configuration.
  • Compare the reference calculation or decision with an independent implementation or authorized observation; state any discrepancy and the applicable model boundary.

Expected learning outcomes

On completion, students can trace double encryption and Triple DES EDE order, derive the cancellation case when all keys coincide, and verify inverse DED recovery and explain historical security limitations.

Required deliverables

Submit the derivation or state trace for the named operation, the exact reference and changed configurations, the associated simulator or practical evidence, and a short validation statement addressing verify inverse DED recovery and explain historical security limitations. Separate predicted values from observations.

Prerequisite knowledge and related learning

Use the curriculum sequence and the Concepts section for prerequisite terminology. After completion, continue to Double encryption · reduced meet-in-the-middle.

Environment and experimental methodology

A standard browser and the experiment-specific Python implementation or Colab notebook. Where the practical uses a protocol model, model results are distinguished from actual local cryptographic operations.

Begin with a manual prediction, record the baseline, change one independent condition at a time, and compare the resulting trace or measurements with the original prediction. Record observations and uncertainty separately from reference values. Follow the experiment’s specific procedure, safety limits, and acceptance checks.

01 · Experiment overview

CR07 · DES · double encryption and Triple DES EDE

Double DES uses \(E_{2}\)(\(E_{1}\)(P)); two-key TDEA uses \(E_{1}\)(\(D_{2}\)(\(E_{1}\)(P))) and three-key TDEA uses \(E_{3}\)(\(D_{2}\)(\(E_{1}\)(P))). Decryption reverses order and operation. Key-length arithmetic is not effective security; TDEA was withdrawn by NIST in 2024.

Before starting

Environment: standard computer, browser and Python/WSL.

Simulation: Editable JavaScript calculation

Technical scope: Classroom computation on a standard computer. Educational cipher code is not a production security implementation.

Complete the six stages in order. Use the same experiment inputs when comparing manual, browser and Python results.

02 · Concepts and mathematical foundations

Double DES uses \(E_{2}\)(\(E_{1}\)(P)); two-key TDEA uses \(E_{1}\)(\(D_{2}\)(\(E_{1}\)(P))) and three-key TDEA uses \(E_{3}\)(\(D_{2}\)(\(E_{1}\)(P))). Decryption reverses order and operation. Key-length arithmetic is not effective security; TDEA was withdrawn by NIST in 2024.

Learning objectives

Calculate the named operation, inspect its intermediate values, reproduce the result in Python, and explain one controlled change. Prerequisites: binary/hexadecimal arithmetic; complete preceding labs in the same topic where applicable.

Before starting

Environment: standard computer, browser and Python/WSL.

Simulation: Editable JavaScript calculation

Technical scope: Classroom computation on a standard computer. Educational cipher code is not a production security implementation.

Multiple encryption

1 · Principles and purpose

Double DES applies two encryptions; Triple DES uses encrypt–decrypt–encrypt to permit historical compatibility. Reversing a composition requires reversing both order and each operation.

2 · Mathematical and algorithmic foundations

C=E_K3(D_K2(E_K1(P))); P=D_K1(E_K2(D_K3(C))). If K1=K2=K3, two adjacent inverse operations cancel. Two-key EDE sets K3=K1.

3 · Worked teaching example

The intermediate stage is not a new independent plaintext format. Compare stage outputs using the supplied local cipher and validate the final inverse. DES and TDEA are obsolete for new protection.

Verified default trace · 7 intermediate states

Given parameters (teaching example):

{
  "plaintext": "0123456789ABCDEF",
  "key1": "133457799BBCDFF1",
  "key2": "0E329232EA6D0D73",
  "key3": "AABB09182736CCDD"
}
StageComputed values
1{ "step": "Single E1", "value": "85E813540F0AB405" }
2{ "step": "Double E2", "value": "B4A46FBF0342085D" }
3{ "step": "EDE D2", "value": "371F995CA267FB42" }
4{ "step": "EDE E3", "value": "1ECC68F07E644795" }
5{ "step": "Reverse D3", "value": "371F995CA267FB42" }
6{ "step": "Reverse E2", "value": "85E813540F0AB405" }
7{ "step": "Reverse D1", "value": "0123456789ABCDEF" }

Recorded result

{
  "double": "B4A46FBF0342085D",
  "double_recovered": "0123456789ABCDEF",
  "triple": "1ECC68F07E644795",
  "recovered": "0123456789ABCDEF"
}

Captured earlier local execution. Randomized values and timing can differ; compare the invariant.

4 · Verification and experimental interpretation

Step through the following visualization and compare the named operation with the corresponding computed state. Check the mathematical invariant, inverse operation or protocol acceptance condition. Change one input at a time; distinguish a measured output from a prediction.

Mathematical representation and interpretation

DES substitution and round processing

A six-bit input selects one of four rows using its outer bits and one of sixteen columns using its inner bits. S_j is the selected DES substitution table; v is a four-bit result.

\[r=2b_1+b_6,\qquad c=8b_2+4b_3+2b_4+b_5,\qquad v=S_j[r,c]\]

Input 011011 selects row 1 and column 13; use the selected table to read the output.

Triple DES EDE

E and D are the DES encryption and inverse transformations; the order reverses during decryption. The two-key variant sets K3=K1. These historical constructions are not recommended for new deployments.

\[C=E_{K_3}(D_{K_2}(E_{K_1}(P))),\qquad P=D_{K_1}(E_{K_2}(D_{K_3}(C)))\]

Concept animation · EDE data path

Explore the teaching example independently of the simulator. Each step reveals an operation, data relationship or calculated state. The Simulation view has separate editable inputs.

Encrypt with K1PE(K1)D(K2)E(K3)CLegacy 64-bit block architecture · not a recommended new cipherDecrypt with K2PE(K1)D(K2)E(K3)CLegacy 64-bit block architecture · not a recommended new cipherEncrypt with K3PE(K1)D(K2)E(K3)CLegacy 64-bit block architecture · not a recommended new cipherReverse to decryptCD(K3)E(K2)D(K1)PLegacy 64-bit block architecture · not a recommended new cipher

Step 1 of 4 · Encrypt with K1

Encrypt with K1

E_K1(P) is the first intermediate block.

Teaching example only; inspect live calculations or real practical evidence separately.

Shared research workbenches · network designer & advanced studies
Concurrent-device simulation & Monte Carlo research
Distributed digital twin · topology, protocol & battery research

03 · Interactive simulation

Baseline diagram loaded. Interactive engine initializing…

Verified baseline reference — use the live workstation below to explore steps and compare inputs.
CR07 calculated stateMultiple encryption · independent E/E and E/D/E pathsPlaintext0123456789ABCDEFE₁85E813540F0AB405Double DES: E₂B4A46FBF0342085DTDEA: D₂371F995CA267FB42TDEA: E₃1ECC68F07E644795Selected stageB4A46FBF0342085DDouble encryption and EDE are different constructions

Edit the fields and choose Recompute. Use step controls to inspect real calculated states. Hexadecimal byte fields are written without 0x; numeric fields accept decimal or 0x notation.

Calculated result

{
  "double": "B4A46FBF0342085D",
  "double_recovered": "0123456789ABCDEF",
  "triple": "1ECC68F07E644795",
  "recovered": "0123456789ABCDEF"
}
All intermediate trace rows

Guided simulation activity

Use the baseline inputs shown above. Predict a result, advance one operation at a time, inspect the intermediate state, then change only the parameter specified in the Experiments view. Export the trace and compare it with your manual derivation.

Continue to practical experiments →

04 · Experiments and engineering investigations

Analytical preparation — before Python

Double DES uses \(E_{2}\)(\(E_{1}\)(P)); two-key TDEA uses \(E_{1}\)(\(D_{2}\)(\(E_{1}\)(P))) and three-key TDEA uses \(E_{3}\)(\(D_{2}\)(\(E_{1}\)(P))). Decryption reverses order and operation. Key-length arithmetic is not effective security; TDEA was withdrawn by NIST in 2024.

Predict the baseline result before revealing the calculated state. A controlled comparison changes the named input while keeping the other conditions fixed. Record an observed value separately from an expected value.

Part A · Independent theoretical exercises

Complete each question before running the simulator or Python. Show calculations or protocol traces. The teaching example is a method, not a substitute for the assigned values.

01 · Application and derivation

Application and derivation · Independent work

Draw EDE encryption and the inverse DED sequence using named keys K1, K2, K3. For the baseline block, tabulate the three stage values using the provided DES algorithm; derive the special case K1=K2=K3.

02 · Architecture analysis

Architecture analysis · Independent work

In 3DES EDE let K1=K2 but K3 differ. Reduce the encryption composition algebraically. Does this configuration offer triple-strength security?

Required evidence: state the expected invariant or acceptance condition; record hand-derived results, compare with the simulator or Python trace, and explain the first discrepancy. Do not report a predicted value as a measurement.

Expanded independent theoretical exercise bank

Progress from calculation and interpretation to diagnosis and controlled engineering design. Complete the questions without opening the simulation first; show intermediate steps.

Intermediate · Alternate configuration

Independent theoretical exercise · CR07-01

For the separate teaching-test configuration plaintext: 0123456789ABCDEF; key1: 133457799BBCDFF1; key2: 0E329232EA6D0D73; key3: 133457799BBCDFF1, predict double using the relevant operation or decision rule. Show intermediate work and state whether this is a deterministic result.

Intermediate · Interpret a computation state

Independent theoretical exercise · CR07-02

A teaching trace for DES · double encryption and Triple DES EDE contains step: Double E2; value: B4A46FBF0342085D. Explain the role of value (recorded value B4A46FBF0342085D) and identify the rule or input that determines it. Distinguish an intermediate state from the final result.

Advanced · Identify a terminal invariant

Independent theoretical exercise · CR07-03

The first teaching trace contains step: Single E1; value: 85E813540F0AB405; the last contains step: Reverse D1; value: 0123456789ABCDEF. Identify one distinction between the initial operation and completion. What additional condition must hold to use the result?

Challenging · Controlled algorithm verification

Independent theoretical exercise · CR07-04

Plan a comparison of plaintext: 0123456789ABCDEF; key1: 133457799BBCDFF1; key2: 0E329232EA6D0D73; key3: AABB09182736CCDD with plaintext: 0123456789ABCDEF; key1: 133457799BBCDFF1; key2: 0E329232EA6D0D73; key3: 133457799BBCDFF1. Record the dependent value triple, identify the changed factor(s), and describe the verification and failure criterion without claiming a real security measurement.

Part B · Guided simulation and practical procedure

Before starting — tools and setup

Standard computer; extract the WSL toolkit into a new directory. For the browser activity, use the integrated workstation. Editable browser calculation

Install Python dependencies only if required by the selected toolkit README. Use its requirements file and virtual environment instructions.

Open complete, verified WSL commands in Part C ↓

01

Compare double DES and EDE

  1. Use the exact baseline input below and write a prediction.
  2. Run the three supplied keys and block. Trace E1, E2 and D2, E3; then D3, E2, D1.
  3. Save the trace and compare it with the reference output.

Experimental inputs

Experimental reference · json
{"plaintext":"0123456789ABCDEF","key1":"133457799BBCDFF1","key2":"0E329232EA6D0D73","key3":"AABB09182736CCDD"}
Verified baseline reference (check after predicting)
Experimental reference · json
{"double":"B4A46FBF0342085D","double_recovered":"0123456789ABCDEF","triple":"1ECC68F07E644795","recovered":"0123456789ABCDEF"}

Default EDE ciphertext=1ECC68F07E644795. When all keys equal, EDE reduces to single DES 85E813540F0AB405.

Evidence to record

K1–K3, each stage output, final ciphertext and recovery for each case.

Run baseline →
02

Part D · Controlled investigation

  1. Set K3=K1 for two-key EDE; then set K3=K2; finally set K2=K3=K1. Keep plaintext fixed.
  2. Record the conditions held fixed: plaintext and key1,key2
  3. Collect K1–K3, each stage output, final ciphertext and recovery for each case. Compare the first differing intermediate step and explain the relationship.
TrialInput configurationPredictionObservedVerification
1{"plaintext":"0123456789ABCDEF","key1":"133457799BBCDFF1","key2":"0E329232EA6D0D73","key3":"AABB09182736CCDD"}
2{"plaintext":"0123456789ABCDEF","key1":"133457799BBCDFF1","key2":"0E329232EA6D0D73","key3":"133457799BBCDFF1"}

Use the table as a recording template; enter your own measured values in the field below or reproduce it in your report.

Return to simulation →
03

Part E/F · Independent Python verification and deliverables

  1. Download the notebook or toolkit and run the default example.
  2. Reproduce at least one changed input; compare the intermediate values with the browser where supported.
  3. Complete the independent assessment below without copying the worked baseline. Explain discrepancies and distinguish mathematical results from security claims.
Additional investigation instructions

A controlled comparison changes one independent input while holding other inputs fixed, then measures its effect on the dependent output. A baseline is the unmodified run. Evidence-based reasoning means citing the actual observed values, not merely asserting that an algorithm is secure.

  1. Run the supplied default values and record the first and final state.
  2. Predict the effect before changing anything. Change set key3 equal to key1, then distinct. First verified alternate: {"key3": "133457799BBCDFF1"}.
  3. Keep plaintext and key1,key2.
  4. Measure all E/D stage values and recovered plaintext.
  5. Restore the baseline and repeat with another valid input. Locate the first differing calculation.
  6. Submit a comparison table, a trace JSON or screenshots of relevant steps, and a short conclusion with an explicit limitation.
TrialChanged inputFixed conditionsPredictionMeasured valueReason / limitation
BaselineNoneDefaultStudent predictionRecord traceExplain
Trial 1Specified changeUnchanged inputsStudent predictionRecord traceExplain

Part C · Python, Google Colab and WSL

The standalone implementation uses the same synthetic baseline and calculations as the browser. In Colab, download the notebook, open Colab and upload it using File → Upload notebook. Run all cells in order.

WSL / Linux execution workflow

Use Ubuntu or Kali WSL, not PowerShell. Download the toolkit above to your computer. If Python, venv or unzip is missing, install these prerequisites once (requires administrator permission and network access).

Optional prerequisites · bash
sudo apt update
sudo apt install -y python3 python3-venv unzip

In WSL, enter the actual downloaded ZIP path when prompted (for example, a file under /mnt/c/Users/…/Downloads). Extract to a new, dedicated directory. If this directory already exists from a previous extraction, use the existing verified toolkit or choose a clean directory rather than overwriting it.

Step 1 · Extract · bash
read -r -p "Full path to PSUT_V22_9_9_Cryptography_WSL.zip: " ZIP
DEST="$HOME/psut-crypto-v22.9.9"
if [ -f "$ZIP" ] && [ ! -e "$DEST" ]; then
    mkdir "$DEST" && unzip "$ZIP" -d "$DEST"
else
    echo "Check ZIP path and use a fresh destination."
fi

Create the isolated environment in the extracted toolkit root, activate it, then enter this experiment’s actual folder. Install only its declared dependencies. A comment-only requirements file requires no third-party packages.

Step 2 · Activate and install · bash
cd ~/psut-crypto-v22.9.9
python3 -m venv .venv
source .venv/bin/activate
cd CR07
ls
python -m pip install -r requirements.txt

Execute the default run and save the genuine program output. Then run the supplied regression test and the baseline fixture through the explicit input interface.

Step 3 · Run and verify · bash
python lab.py | tee my-results.json
python test_lab.py
python lab.py --params example-input.json
python -m json.tool my-results.json

To change inputs, copy example-input.json, edit the copy in a text editor and run the command below. The original fixture is preserved.

Step 4 · Change an input · bash
cp example-input.json my-input.json
# Edit my-input.json with your preferred editor.
python lab.py --params my-input.json

These scripts terminate normally. Use Ctrl+C to stop a long-running calculation if needed. When finished, leave the isolated virtual environment; no global service cleanup is required for these local examples.

Step 5 · Finish · bash
deactivate

Declared dependencies: # Standard library only for core experiment

Expected terminal output — captured local execution

This excerpt contains the actual first trace step and final result from python lab.py, followed by the actual regression test output. Full stdout is in CR07/verified-output.txt inside the toolkit. Timing, random signatures, keys, certificate fields and implementation-dependent sizes can vary; compare the invariant and record your own observations.

Verified execution excerpt · text
{
  "first_step": {
    "step": "Single E1",
    "value": "85E813540F0AB405"
  },
  "result": {
    "double": "B4A46FBF0342085D",
    "double_recovered": "0123456789ABCDEF",
    "triple": "1ECC68F07E644795",
    "recovered": "0123456789ABCDEF"
  }
}

Test script output:
PASS

Check the output JSON and the test exit status. An assertion error means the expected invariant was not met. If Python reports a missing module, activate the environment and install the experiment’s requirements file; if a file is missing, check pwd and ls.

Inspect the documented Python implementation
Documented Python implementation · python
# ============================================================
# CR07 | Educational implementation and trace functions
# Copyright © 2026/2027 Dr. Mohammad Alja’afreh
# PSUT IoT Engineering Studio | Educational, synthetic inputs
# ============================================================
def hx(n,w=2): return f'{n:0{w}X}'

# Wrap shifted bits around the fixed word width instead of discarding them.
def rot(x,k,w): return ((x<<k)|(x>>(w-k)))&((1<<w)-1)

def row(label,**values): return {'step':label,**values}

# Standard DES lookup table: IP
IP=[58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7]

# Standard DES lookup table: E
E=[32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1]

# Standard DES lookup table: P
P=[16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25]

# Standard DES lookup table: PC1
PC1=[57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4]

# Standard DES lookup table: PC2
PC2=[14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32]

# Standard DES lookup table: S
S=[[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13], [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9], [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12], [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14], [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3], [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13], [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12], [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]

# Select bits by the standard one-based DES position table.
def perm(x,width,table):
    v=0
    for i in table: v=(v<<1)|((x>>(width-i))&1)
    return v

# Use outer input bits for row and middle four bits for column.
def dsbox(n,b):
    r=((b>>5)<<1)|(b&1); c=(b>>1)&15
    return r,c,S[n][16*r+c]

# Discard parity with PC-1, rotate C/D, select 48-bit subkeys with PC-2.
def deskeys(key):
    cd=perm(key,64,PC1); c,d=cd>>28,cd&0xfffffff; out=[]
    for i,n in enumerate([1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1]):
        c,d=rot(c,n,28),rot(d,n,28); k=perm((c<<28)|d,56,PC2)
        out.append((c,d,k,n))
    return out

# Apply IP, the 16 Feistel rounds and inverse IP; reverse subkeys for decryption.
def des(p,key,dec=False):
    v=perm(p,64,IP); l,r=v>>32,v&0xffffffff; trace=[row('IP',state=hx(v,16))]; keys=deskeys(key)[::(-1 if dec else 1)]
    for i,(c,d,k,n) in enumerate(keys,1):
        e=perm(r,32,E); x=e^k; sub=0
        for j in range(8): sub=(sub<<4)|dsbox(j,(x>>(42-6*j))&63)[2]
        f=perm(sub,32,P); l,r=r,l^f
        trace.append(row(f'Round {i}',K=hx(k,12),expanded=hx(e,12),mixed=hx(x,12),sbox=hx(sub,8),P=hx(f,8),state=hx((l<<32)|r,16)))
    result=perm((r<<32)|l,64,[IP.index(i)+1 for i in range(1,65)])
    return result,trace

def n(s): return int(s,0)

def b(s): return bytes.fromhex(s)

def exact(s,size):
    v=b(s)
    if len(v)!=size: raise ValueError(f'Expected {size} bytes')
    return v

def pack(result,trace): return {'result':result,'trace':trace}

EXTRA = {}

def run(p):
    v=int.from_bytes(exact(p['plaintext'],8),'big');ks=[int.from_bytes(exact(p['key'+str(i)],8),'big') for i in (1,2,3)]; a=des(v,ks[0])[0]; double=des(a,ks[1])[0]; b0=des(a,ks[1],True)[0]; c=des(b0,ks[2])[0]; d=des(c,ks[2],True)[0]; e=des(d,ks[1])[0]; f=des(e,ks[0],True)[0]; dd=des(des(double,ks[1],True)[0],ks[0],True)[0]
    return pack({'double':hx(double,16),'double_recovered':hx(dd,16),'triple':hx(c,16),'recovered':hx(f,16)},[row(name,value=hx(x,16)) for name,x in [('Single E1',a),('Double E2',double),('EDE D2',b0),('EDE E3',c),('Reverse D3',d),('Reverse E2',e),('Reverse D1',f)]])

PARAMS = {'plaintext': '0123456789ABCDEF', 'key1': '133457799BBCDFF1', 'key2': '0E329232EA6D0D73', 'key3': 'AABB09182736CCDD'}
EXPECTED = {'double': 'B4A46FBF0342085D', 'double_recovered': '0123456789ABCDEF', 'triple': '1ECC68F07E644795', 'recovered': '0123456789ABCDEF'}
ALT = {'plaintext': '0123456789ABCDEF', 'key1': '133457799BBCDFF1', 'key2': '0E329232EA6D0D73', 'key3': '133457799BBCDFF1'}
ALT_EXPECTED = {'double': 'B4A46FBF0342085D', 'double_recovered': '0123456789ABCDEF', 'triple': '055152350CD7A4BB', 'recovered': '0123456789ABCDEF'}

if __name__ == "__main__":
    import argparse, json
    parser=argparse.ArgumentParser(description="CR07 synthetic cryptography exercise")
    parser.add_argument("--params", help="JSON parameter override file")
    parser.add_argument("--test", action="store_true")
    args=parser.parse_args()
    if args.test:
        assert run(PARAMS)["result"] == EXPECTED
        assert run(ALT)["result"] == ALT_EXPECTED
        print("CR07 default and changed input passed")
    else:
        parameters=PARAMS if not args.params else {**PARAMS,**json.load(open(args.params))}
        print(json.dumps(run(parameters),indent=2))
Verified default result
Reference result · json
{
  "double": "B4A46FBF0342085D",
  "double_recovered": "0123456789ABCDEF",
  "triple": "1ECC68F07E644795",
  "recovered": "0123456789ABCDEF"
}

05 · Interactive flashcards

Recall before revealing. Review and mastery ratings remain on this device.

Browse all explanations
What happens if all three keys are equal?

E-D cancels and the result is single DES.

What is the dependent variable in this lab?

all E/D stage values and recovered plaintext

What must remain controlled?

plaintext and key1,key2

What input settings define the baseline for this experiment?

plaintext = 0123456789ABCDEF; key1 = 133457799BBCDFF1; key2 = 0E329232EA6D0D73; key3 = AABB09182736CCDD

What is the first intermediate state in the baseline?

step: Single E1; value: 85E813540F0AB405

What check must you reproduce before varying the inputs?

Default EDE ciphertext=1ECC68F07E644795. When all keys equal, EDE reduces to single DES 85E813540F0AB405.

Which observations belong in your experimental record?

K1–K3, each stage output, final ciphertext and recovery for each case.

06 · Knowledge assessment

Formative check

Which experimental design best isolates the effect of the specified input?

Responses are for instructor review; no grading key is provided here.

Independent assessment

Run EDE with K3=K1, then K3=K2, then all three distinct. Record ciphertext and each intermediate state; determine when a stage cancels and justify algebraically. Use an additional valid trial, a calculation or a counterexample to support your reasoning. Submit evidence from both runtimes and explain what the experiment does not establish.

Part F · Engineering evidence record

Record the baseline, prediction, changed input, observed result, discrepancy and limitation. Save locally or export with the displayed result. This is not an official submission.

All Cryptography Engineering labsNext laboratory · CR08 →

How to investigate · CR07

Independent variable: set key3 equal to key1, then distinct.

Controlled variables: plaintext and key1,key2.

Dependent evidence: all E/D stage values and recovered plaintext.

Weak conclusion: “The output changed.” Stronger reasoning: cite both measured values, name the transformation responsible, and state why the observation does or does not generalize.

Dr. Mohammad · CR07

Offline, deterministic, current-step tutoring. No external AI service or real hardware access.