Skip to content

Techriot RSA Cryptography API

  • Labs

RSA (Rivest-Shamir-Adleman) is a widely used encryption algorithm in cybersecurity and cryptography. It’s named after its creators: Ronald Rivest, Adi Shamir, and Leonard Adleman. RSA is a form of asymmetric encryption. Asymmetric means it uses two distinct keys: a public key and a private key. The public key is used for encryption, while the private key is used for decryption. This duality allows secure communication and data protection. When someone wants to send you an encrypted message, they use your public key to encrypt the data.

Download or see the code I wrote, on Github.

Only you, with the corresponding private key, can decrypt and access the original message. This ensures confidentiality and security in digital communication. The strength of RSA lies in the difficulty of factoring the product of two large prime numbers. The security of the algorithm relies on the fact that factoring large numbers into primes is a computationally intensive process, making it infeasible for attackers to decipher the encrypted information without the private key.

See and try the application on the same domain and port 4500 or click this link Techriot RSA.

Techriot RSA

Let’s proceed to our Python application. To create Python RSA application we use module actually just called “RSA” and base64 module we use to encode text into human readable form. Script contains four functions “keygen” for generating RSA key pair, “encrypt” encrypts plaintext and produces cipher text, “decrypt” produces plaintext as name of the functions state. This code you can see below and on the github and you can build your own GUI because this one script works and own module.

import rsa
from base64 import b64encode, b64decode

def keygen(keysize): # key size = 512, 1024, 2048, 4096
    # Generate the key pair
    pubkey, privkey = rsa.newkeys(keysize)

    # Save the private key to a file
    with open('keypair/private.pem', mode='wb') as prvKeyFile:

    # Save the public key to a file
    with open('keypair/public.pem', mode='wb') as pubKeyFile:

def encrypt(pubkey, plaintext):
        pubkey = rsa.PublicKey.load_pkcs1(pubkey)
        scrambled = rsa.encrypt(plaintext.encode('ascii'), pubkey)
        return b64encode(scrambled).decode("ascii")
        return "ERROR: Invalid key!"

def decrypt(prvkey, b64text):
        prvkey = rsa.PrivateKey.load_pkcs1(prvkey)
        plaintext = rsa.decrypt(b64decode(b64text.encode('ascii')), prvkey)
        return plaintext.decode()
        return "ERROR: Invalid private key or ciphertext!"

Flask API

This is the API GUI i created using Flask library. You can use mine of create own and then just import the techriotrsa module into another script. First we import Flask class and functions we need to run the API and our self-created module. Each of those functions under decorator represents one web page except “download_fiile” which activates only after keys are generated and user clicks download button.

from flask import Flask, request, render_template, session, send_file, url_for, redirect
import techriotrsa # self created module 

app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.secret_key = "place_secret_key_here"

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == "POST":
            keysize = int(request.form['keysize'])
            session["keys_generated"] = True
            with open('keypair/private.pem', "r") as file:
                prv_key =
            with open('keypair/public.pem', "r") as file:
                pub_key =
            return render_template("index.html", private_key="private.pem", prv_key=prv_key, pub_key=pub_key)
            return render_template("index.html", error="ERROR: No key size selected!")
    return render_template("index.html")

@app.route('/encrypt', methods=['GET', 'POST'])
def encrypt():
    if request.method == "POST":
        plaintext = request.form.get('plaintext')
        pubkey = request.form.get('pubkey')
        scrambled = techriotrsa.encrypt(pubkey, plaintext)
        return render_template("encrypt.html", scrambled=scrambled)
    return render_template("encrypt.html")

@app.route('/decrypt', methods=['GET', 'POST'])
def decrypt():
    if request.method == "POST":
        ciphertext = request.form.get('scrambledtext')
        prvkey = request.form.get('prvkey')
        plaintext = techriotrsa.decrypt(prvkey, ciphertext)
        return render_template("decrypt.html", plaintext=plaintext)
    return render_template("decrypt.html")

def download_file(filename):
    if not session.get("keys_generated"):
        return redirect(url_for("/"))
    return send_file(path_or_file="keypair/"+filename, as_attachment=True)

if __name__ == "__main__":