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.

On 11/20/2026 at 5:17 PM, Sensei said:

RSA algorithm

Note that the way encode() is implemented the result is a substitution cipher. As far as I can tell each byte always maps to the same ciphertext integer, and distinct bytes map to distinct integers. The cipher text integers play the role of symbols in the substitution alphabet. (I'm sure Sensei is aware of the above, but @Trurl might not; and be tempted to misinterpret the posted code)

4 hours ago, Ghideon said:

Note that the way encode() is implemented the result is a substitution cipher. As far as I can tell each byte always maps to the same ciphertext integer, and distinct bytes map to distinct integers. The cipher text integers play the role of symbols in the substitution alphabet. (I'm sure Sensei is aware of the above, but @Trurl might not; and be tempted to misinterpret the posted code)

Yeah. The input data should not consist of a single character (8 bits, 1 byte), but rather a block with length in bits bit_length_of(n). For a typical RSA-2048, it is 2048/8=256 bytes. And primes in range 2^128.

This was merely an example algorithm, which is easy to analyze line by line. So primes are in range 100 ... 1000, so they fit in 16 bits, and block fits in 32 bits (4 bytes).

I brought this to ChatGPT attention after I posted it here, but you know how weak all these LLMs are in programming..

Since you’ve noted that encoding algorithm could be replaced by output = database[ index ];, the decoding process could also be implemented in the same way. The only catch is that with RSA-2048, we would end up with 2^2048 elements in the array... ;) There wouldn't be enough atoms in the entire observable universe to encompass this.

To bypass this weakness one can use RSA-OAEP. https://en.wikipedia.org/wiki/Optimal_asymmetric_encryption_padding

Edited by Sensei

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.