Skip to content

Can you use traditional cipher be used in public key cryptography?

Featured Replies

@Trurl

I asked ChatGPT v6 to generate an RSA algorithm for you. Here is the code:

#!/bin/env python

import random
import math

# -----------------------------
# MATHEMATICAL FUNCTIONS
# -----------------------------

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def is_prime(n):
    if n < 2:
        return False

    if n == 2:
        return True

    if n % 2 == 0:
        return False

    i = 3
    while i * i <= n:
        if n % i == 0:
            return False
        i += 2

    return True


def generate_prime(min_value=100, max_value=1000):
    while True:
        number = random.randint(min_value, max_value)

        if is_prime(number):
            return number


def mod_inverse(e, phi):
    # Extended Euclidean Algorithm
    old_r, r = e, phi
    old_s, s = 1, 0

    while r != 0:
        quotient = old_r // r

        old_r, r = r, old_r - quotient * r
        old_s, s = s, old_s - quotient * s

    if old_r != 1:
        raise ValueError("Inverse does not exist")

    return old_s % phi


# -----------------------------
# RSA KEY GENERATION
# -----------------------------

def generate():
    p = generate_prime()
    q = generate_prime()

    while p == q:
        q = generate_prime()

    n = p * q
    phi = (p - 1) * (q - 1)

    e = 65537

    if e >= phi or gcd(e, phi) != 1:
        e = 3
        while gcd(e, phi) != 1:
            e += 2

    d = mod_inverse(e, phi)

    public_key = (e, n)
    private_key = (d, n)

    return q, p, n, phi, e, d, public_key, private_key


# -----------------------------
# ENCRYPTION
# -----------------------------

def encode(text, public_key):
    e, n = public_key

    # Convert text into bytes
    data = text.encode("utf-8")

    # Each byte is encrypted separately
    encrypted = []

    for byte in data:
        encrypted.append(pow(byte, e, n))

    return encrypted


# -----------------------------
# DECRYPTION
# -----------------------------

def decode(encrypted, private_key):
    d, n = private_key

    decrypted = []

    for value in encrypted:
        decrypted.append(pow(value, d, n))

    # Convert bytes back into text
    return bytes(decrypted).decode("utf-8")


# -----------------------------
# MAIN
# -----------------------------

if __name__ == "__main__":

    # Generate keys
    q, p, n, phi, e, d, public_key, private_key = generate()

    print("========== RSA KEYS ==========")
    print("q:", q)
    print("p:", p)
    print("n:", n)
    print("phi:", phi)
    print("e:", e)
    print("d:", d)

    print("\nPublic key:", public_key)
    print("Private key:", private_key)

    # Sample text
    text_in = "Hello World! RSA in Python."

    print("\n========== ENCRYPTION ==========")
    print("TEXT IN:", text_in)

    encrypted = encode(text_in, public_key)

    print("ENCODED:", encrypted)

    print("\n========== DECRYPTION ==========")

    text_out = decode(encrypted, private_key)

    print("DECODED:", text_out)

    print("\n========== RESULT ==========")
    print("Original == Decoded:", text_in == text_out)

The program generates private and public keys, encrypts a sample text (which you can modify or provide as a command-line argument), and then decrypts it to verify that the result matches the original.

We don't want to wait too long, so the LLM selected ranges of prime numbers between 100 and 1000. Feel free to modify it for larger ranges if you want.

I specifically forbade him from using libraries, so that he would have to write all the mathematical code by hand.

The generate() function could be simpler – currently, we are returning significantly more data, which serves only as information for the user.

Create an account or sign in to comment

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.