Python Encyclopedia for Academics
  • Course Outline
  • Artificial Intelligence
    • Data Science Foundation
      • Python Programming
        • Introduction and Basics
          • Variables
          • Print Function
          • Input From User
          • Data Types
          • Type Conversion
        • Operators
          • Arithmetic Operators
          • Relational Operators
          • Bitwise Operators
          • Logical Operators
          • Assignment Operators
          • Compound Operators
          • Membership Operators
          • Identity Operators
      • Numpy
        • Vectors, Matrix
        • Operations on Matrix
        • Mean, Variance, and Standard Deviation
        • Reshaping Arrays
        • Transpose and Determinant of Matrix
      • Pandas
        • Series and DataFrames
        • Slicing, Rows, and Columns
        • Operations on DataFrames
        • Different wayes to creat DataFrame
        • Read, Write Operations with CSV files
      • Matplotlib
        • Graph Basics
        • Format Strings in Plots
        • Label Parameters, Legend
        • Bar Chart, Pie Chart, Histogram, and Scatter Plot
  • Machine Learning Algorithms
    • Regression Analysis In ML
      • Regression Analysis in Machine Learning
      • Proof of Linear Regression Formulas
      • Simple Linear Regression Implementation
      • Multiple Linear Regression
      • Advertising Dataset Example
      • Bike Sharing Dataset
      • Wine Quality Dataset
      • Auto MPG Dataset
    • Classification Algorithms in ML
      • Proof of Logistic Regression
      • Simplified Mathematical Proof of SVM
      • Iris Dataset
  • Machine Learning Laboratory
    • Lab 1: Titanic Dataset
      • Predicting Survival on the Titanic with Machine Learning
    • Lab 2: Dow Jones Index Dataset
      • Dow Jones Index Predictions Using Machine Learning
    • Lab 3: Diabetes Dataset
      • Numpy
      • Pandas
      • Matplotlib
      • Simple Linear Regression
      • Simple Non-linear Regression
      • Performance Matrix
      • Preprocessing
      • Naive Bayes Classification
      • K-Nearest Neighbors (KNN) Classification
      • Decision Tree & Random Forest
      • SVM Classifier
      • Logistic Regression
      • Artificial Neural Network
      • K means Clustering
    • Lab 4: MAGIC Gamma Telescope Dataset
      • Classification in ML-MAGIC Gamma Telescope Dataset
    • Lab 5: Seoul Bike Sharing Demand Dataset
      • Regression in ML-Seoul Bike Sharing Demand Dataset
    • Lab 6: Medical Cost Personal Datasets
      • Predict Insurance Costs with Linear Regression in Python
    • Lab 6: Predict The S&P 500 Index With Machine Learning And Python
      • Predict The S&P 500 Index With Machine Learning And Python
  • Artificial Neural Networks
    • Biological Inspiration vs. Artificial Neurons
    • Review linear algebra and calculus essentials for ANNs
    • Activation Function
  • Mathematics
    • Pre-Calculus
      • Factorials
      • Roots of Polynomials
      • Complex Numbers
      • Polar Coordinates
      • Graph of a Function
    • Calculus 1
      • Limit of a Function
      • Derivative of Function
      • Critical Points
      • Indefinite Integrals
  • Calculus 2
    • 3D Coordinates and Vectors
    • Vectors and Vector Operations
    • Lines and Planes in Space (3D)
    • Partial Derivatives
    • Optimization Problems (Maxima/Minima) in Multivariable Functions
    • Gradient Vectors
  • Engineering Mathematics
    • Laplace Transform
  • Electrical & electronics Eng
    • Resistor
      • Series Resistors
      • Parallel Resistors
    • Nodal Analysis
      • Example 1
      • Example 2
    • Transient State
      • RC Circuit Equations in the s-Domain
      • RL Circuit Equations in the s-Domain
      • LC Circuit Equations in the s-Domain
      • Series RLC Circuit with DC Source
  • Computer Networking
    • Fundamental
      • IPv4 Addressing
      • Network Diagnostics
  • Cybersecurity
    • Classical Ciphers
      • Caesar Cipher
      • Affine Cipher
      • Atbash Cipher
      • Vigenère Cipher
      • Gronsfeld Cipher
      • Alberti Cipher
      • Hill Cipher
Powered by GitBook
On this page
  • Steps to Encrypt and Decrypt Using the Gronsfeld Cipher
  • Practical Example: Encryption
  • Practical Example: Decryption
  • Python Code
  • Output
  1. Cybersecurity
  2. Classical Ciphers

Gronsfeld Cipher

Nerd Cafe

The Gronsfeld Cipher is a variant of the Vigenère cipher but uses only numeric keys, making it simpler. Each digit in the key determines how many positions to shift the corresponding letter in the plaintext.

Steps to Encrypt and Decrypt Using the Gronsfeld Cipher

  1. Choose a numeric key: The key is a sequence of digits (e.g., 31415).

  2. Prepare the plaintext: Write down the message you want to encrypt, e.g., "HELLO".

  3. Repeat the key: If the key is shorter than the plaintext, repeat it to match the length of the plaintext.

  4. Encrypt the plaintext: For each letter in the plaintext:

  • Convert the letter to its position in the alphabet (A=0, B=1, ..., Z=25).

  • Add the corresponding digit from the key.

  • Wrap around using modulo 26 if the result exceeds 25.

  • Convert the result back to a letter.

  1. Decrypt the ciphertext: Reverse the process by subtracting the key digits instead of adding.

Practical Example: Encryption

Inputs:

  • Plaintext: HELLO

  • Key: 31415

Encryption Process:

Convert the letters of the plaintext to their numeric positions:

H = 7, E = 4, L = 11, L = 11, O = 14

Repeat the key to match the length of the plaintext:

Key:  31415

Add each key digit to the corresponding plaintext letter (modulo 26):

7 + 3 = 10  → K
4 + 1 = 5   → F
11 + 4 = 15 → P
11 + 1 = 12 → M
14 + 5 = 19 → T

Resulting ciphertext:

Ciphertext: KFPMT

Practical Example: Decryption

Inputs:

  • Ciphertext: KFPMT

  • Key: 31415

Decryption Process:

Convert the letters of the ciphertext to their numeric positions:

K = 10, F = 5, P = 15, M = 12, T = 19

Subtract each key digit from the corresponding ciphertext letter (modulo 26):

10 - 3 = 7   → H
5 - 1 = 4    → E
15 - 4 = 11  → L
12 - 1 = 11  → L
19 - 5 = 14  → O

Resulting plaintext:

Plaintext: HELLO

Python Code

The Python code for the Gronsfeld Cipher has been implemented. It includes both encryption and decryption functions, along with an example usage.

def gronsfeld_encrypt(plaintext, key):
    # Convert key to a list of integers
    key_digits = [int(d) for d in str(key)]
    key_length = len(key_digits)

    ciphertext = ""

    for i, char in enumerate(plaintext):
        if char.isalpha():
            shift = key_digits[i % key_length]
            base = ord('A') if char.isupper() else ord('a')
            encrypted_char = chr((ord(char) - base + shift) % 26 + base)
            ciphertext += encrypted_char
        else:
            ciphertext += char  # Preserve non-alphabet characters

    return ciphertext

def gronsfeld_decrypt(ciphertext, key):
    # Convert key to a list of integers
    key_digits = [int(d) for d in str(key)]
    key_length = len(key_digits)

    plaintext = ""

    for i, char in enumerate(ciphertext):
        if char.isalpha():
            shift = key_digits[i % key_length]
            base = ord('A') if char.isupper() else ord('a')
            decrypted_char = chr((ord(char) - base - shift) % 26 + base)
            plaintext += decrypted_char
        else:
            plaintext += char  # Preserve non-alphabet characters

    return plaintext

# Example Usage
plaintext = "HELLO"
key = 31415

ciphertext = gronsfeld_encrypt(plaintext, key)
decrypted_text = gronsfeld_decrypt(ciphertext, key)

print("Plaintext:", plaintext)
print("Key:", key)
print("Ciphertext:", ciphertext)
print("Decrypted Text:", decrypted_text)

Output

Plaintext: HELLO
Key: 31415
Ciphertext: KFPMT
Decrypted Text: HELLO
PreviousVigenère CipherNextAlberti Cipher

Last updated 1 month ago