Mathematics made simple.

Followers

Making an Encryption Application in Python Using the RSA Algorithm


 The RSA Cryptosystem

RSA algorithm is an asymmetric cryptography algorithm. Asymmetric actually means that it works on two different keys i.e. Public Key and Private Key. As the name describes that the public key is given to everyone and the private key is kept private.
The Idea: The idea of the RSA algorithm is based on the fact that it is difficult to factorize a large integer. The public key consists of two numbers where one number is the product of two large prime numbers. And private key is also derived from the same two prime numbers. So if somebody can factorize the large number, the private key is compromised. Therefore encryption strength totally lies on the key size and if we double or triple the key size, the strength of encryption increases exponentially. RSA keys can be typically 1024 or 2048 bits long, but experts believe that 1024 bit keys could be broken in the near future. But till now it seems to be an infeasible task.

The mechanism behind the RSA algorithm




Now we have our public (n = 3127, e = 3) and private (d = 2011) keys. 

Below is a Python implementation of the RSA algorithm:

Here a python package called tkinter is used to make the graphical user interface (GUI) for the application we are developing. 

Program

import tkinter as tk
from tkinter import ttk
letter = [ ”a” , ”b” , ”c” , ”d” , ”e” , ”f” , ”g” , ”h” , ”i” , ”j” , ”k” , ”l” , ”m” , ”n” , ”o” , ”p” , ”q” , ”r” , ”s” , ”t” , ”u” , ”v” , ”w” , ”x” , ”y” , ”z” , ” ” , ”A” , ”B ” , ”C” , ”D” , ”E” , ”F” , ”G” , ”H” , ”I” , ”J” , ”K” , ”L” , ”M” , ”N” , ”O” , ”P” , ”Q” , ”R” , ”S” , ”T” , ”U” , ”V” , ”W” , ”X” , ”Y” , ”Z” ] 
n = 53*59
phi = 52*58
e = 3
d = 2011
a = []
b = []
c = []
f = []
def encryption():
    msg = entry1.get()
    for i in msg:
        pos = letters.index(i)
        a.append(pos)
    for j in a:
        b.append((j**e)%n)
    label2.configure(text = ”The Encrypted text”)
    label3.configure(text = b)
def decryption():
    msg = entry1.get()
    g = " "
    l = list(map(int,msg.split(" ")))
    for k in l:
        c.append((k**d)%n)
    for r in c:
        f.append(letters[r])
    for j in f:
        g += j
    label2.configure(text = "The Decrypted text")
    label3.configure(text = g)
root = tk.Tk()
root.title("RSA Algorithm")
root.geometry("500x300")
label1 = ttk.Label(text = "Enter the Message to be Encrypted or Decrypted")
label1.place(x =130, y = 50)
label2 = ttk.Label(text = " ")
label2.place(x = 100,y = 180)
label3 = ttk.Label(text = " ")
label3.place(x = 100, y =200)
entry1 = ttk.Entry()
entry1.place(x=175,y=90)
button1 = ttk.Button(text = "Encrypt",cursor="hand2",command=encryption)
button1.place(x=100,y=140)
button2 = ttk.Button(text = "Decrypt" , cursor = "hand3" , command = lambda:entry1.delete(0,tk.END))
button3.place(x=200,y=140)
root.mainloop()

Output

Encryption

Decryption

0 comments:

Post a Comment

Ultimate Theorem. Powered by Blogger.

About Me

My photo
Mathematics has always fascinated me. I love the subject since childhood. This love towards the field helped me in completing a Masters Degree in Mathematics. As much as I love the subject, I love teaching it too.

Local Linear Approximation

You might have seen the famous approximation $\sin(x) \approx x$ for $x$ near 0. We can use derivatives to approximate non-linear functions ...

Search This Blog

ultimate theorem