Skip to content

CamiloAT/block-cipher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Custom Block Cipher for Logistics

Graphical Interface (GUI) encryption system that implements a custom block cipher algorithm based on a Substitution-Permutation Network (SPN). It is specifically designed to secure package tracking codes in legacy logistics systems, using purely string manipulations and bitwise operations without external lists or dynamic arrays.


1. System Description

The block_cipher.py program allows encrypting tracking codes to ensure their integrity. It operates on a restricted alphanumeric alphabet (A-Z, 0-9) and enforces strict 20-character blocks padding or truncating as necessary. The algorithm relies heavily on mathematical foundations, enforcing Shannon's confusion and diffusion principles through custom chaining mechanisms.


2. Algorithm Architecture

The general architecture of the cipher is defined as: $M_0 \to [S_{chained} \to \pi] \times 32 \to C$

2.1 Block Preparation ($M_0$)

  • The input block evaluates characters in an N=36 alphabet (A-Z, 0-9).
  • It is strictly adapted to guarantee a maximum size of 20 characters.
  • Rules: Rejects accents, spaces, and special characters (like 'ñ').
  • Padding/Truncation: If the message is less than 20 characters, smart padding with the letter X is applied. If it exceeds the limit, the string is truncated.

2.2 Key Generation ($K_0$ and $K_r$)

  • Master Key ($K_0$): Generated dynamically and deterministically from the input block's characters using purely logical bitwise operations (<< and XOR).
  • Round Subkeys ($K_r$): Derived from the original $K_0$ to feed each of the 32 rounds. It uses 16-bit rotation techniques mapped against the Golden Constant (0x9E37) to avoid repetition cycles.

2.3 Chained Substitution ($S$)

Implements Shannon's Confusion principle.

  • Suppresses linear relationships by altering each character in GF(2) field arithmetic (Modulo N).
  • Highlights a custom carry chain mechanism: the transformation generated at position i acts as an additive residue that strongly affects the subsequent position j > i in that same round.

2.4 Transposition ($\pi$)

Implements Shannon's Diffusion principle.

  • Applies a fixed global permutation: $\pi(i) = (7i + 3) \mod 20$.
  • The strength of this transposition is mathematically verified by proving it is a perfect bijection ($\gcd(7, 20) = 1$).

3. Cryptographic Analysis and Mathematical Foundation

According to the mathematical review, the following considerations validate the security and experimental robustness of the cipher:

3.1 Confusion and Diffusion (Shannon, 1949)

  • Confusion: Actively achieved in the substitute() function. It uses non-linear modular addition statically coupled with XOR, interacting with 6-bit extracts of the variable subkey.
  • Diffusion: Achieved by aggressively combining global transposition (transpose()) with the carry chain mechanism. After completing 32 iterations, modifying a single bit in the input randomly perturbs all characters in the ciphertext.

3.2 Avalanche Effect and Number of Rounds (R)

The application can experimentally demonstrate the effectiveness of scaling to $R=32$ rounds via the "Round Analysis" simulation:

  • R = 1: Low/poor diffusion. Bit tracking only reaches later positions without global interconnection.
  • R = 4 to 8: The Permutation+Carry combination achieves ~50% to 80% coverage. The avalanche effect is rapidly compounding.
  • R = 16: The exact moment where diffusion reaches complete asymptotic stabilization.
  • R = 32 (Operation Default): Employs a conservative 2x multiplier over the base saturation index to rigorously compensate for any weaknesses escaping the defined S-Box symmetry.

3.3 Resistance to Differential and Linear Cryptanalysis

  • Differential (Biham & Shamir, 1990): Propagated differences in differential blocks ($\Delta P$) disperse chaotically due to non-linear modular pseudo-random additions. After 32 rounds, carrying a residual bias of $1/36$, the probability of tracing a coherent path drops mathematically to $p \approx 10^{-50}$.
  • Linear (Matsui, 1993): Linear approximations lack structural traction thanks to Matsui's Piling-Up Lemma. Concatenated logical dependencies expose biases so small ($10^{-40}$) that they are effectively nullified by the dynamic subkey's rotational shift.

4. Requirements

  • Python 3.6 or higher
  • tkinter (Native Python library for the GUI)
  • No external dependencies — standard library only (pip installs not required)
  • Multi-platform (Windows, macOS, Linux)

5. Execution Instructions

To launch the graphical interface application, open your terminal (PowerShell, CMD, or Bash) in the project directory and run:

python block_cipher.py

6. GUI Operation Guide

A minimalist and intuitive interface deploying the exact controls required for research and defense testing:

Action / Button Description
Message Field Text input for the logistics tracking code to be encrypted (e.g. PKG2024ABCDEF1234).
Cifrar y Reporte Compiles the iterative process, printing intermediate byte block states at rounds 1, 8, and 32(FINAL). For each step, it records the exact round input, chained substitution result, and diffuse transposition output.
Reporte Completo (32 Rondas) Generates an exhaustive trace, revealing the internal block transformations across every single one of the 32 implemented rounds, fully mapping the input vs. output lifecycle.
Analizar Difusión Runs a strict avalanche test by intentionally altering a single bit in the input message to verify cipher resilience using its original $K_0$ key.
Análisis por Rondas Progressively evaluates the probabilistic behavior of diffusions across different iteration counts, proving the percentage evolution at R=(1, 4, 8, 16, 32).
Limpiar Consola Clears previous logs and static tabulations from the output window environment.

About

Custom block cipher (SPN network) in Python designed to secure logistics tracking codes. Includes a GUI and built-in cryptographic analysis tools.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages