Skip to main content

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

Comments

Popular posts from this blog

Visualization of Collatz Conjecture using Python

The Collatz Conjecture The Problem The Collatz conjecture (a.k.a the hailstone problem or the $3n + 1$ problem) was proposed by Lother Collatz  in 1937. Although the problem on which the conjecture is based is really simple that even a fourth-grader can easily understand it, the behaviour of the conjecture makes it exceedingly difficult to prove(or disprove).  First of all, lets define the Collatz map . Let $T : \mathbb{Z}^+ \rightarrow \mathbb{Z}^+$ be defined by The conjecture is that for every positive integer $n$, there exists a $k$ such that  $T^k(n) = 1$. Let us consider an example. As mentioned above, the conjecture states that this is true for any positive integer n. But what makes this problem interesting is that even after 60  years since its proposal nobody has been able to prove (or disprove) it. Mathematicians say the "Mathematics is yet not ready to tackle such problems." So far the conjecture has been checked for all starting values up to $87 \times...

Generating Sexy Prime Pairs using Python

Sexy Primes, What are they? They are prime numbers that differ by 6. For example, 5 & 11,  7 & 13. The name comes from the Latin word for six; sex. What we are going to do is, write a program to generate all the sexy prime pairs within a given interval of natural numbers.  Sexy Prime Pairs This is a program to generate all the sexy prime pairs below 10000. Program def check_prime(n):     for i in range(2,n//2+1):         if n%i == 0:     return False     return True def primes_list(a,b):     primes_list = []     for i in range(a,b):         if check_prime(i):     primes_list.append(i)     return (primes_list) def sexy_list(primes_list):     sexy_list = []     for i in primes_list:         for j in primes_list:     if j-i == 6:         sexy_list.append((i,j))     return(sexy_...

Monte Carlo Integration using Python

 Monte Carlo Integration Monte Carlo integration is a  powerful method of computing the value of complex integrals using probabilistic techniques. This technique uses random numbers to compute the definite integral of a function. Here we are going to use the python programs written in the  previous post  to generate pseudorandom numbers and approximate the value of the definite integral of a function. Consider a function to be integrated, as shown below: That is we need to evaluate the definite integral $\int_{a}^{b} f(x)\mathrm{d}x$. The integral is just the area under the curve. The width of the interval $(b- a)$ times the average value of the function is also the value of the integral, that is, $\int_{a}^{b}f(x)\mathrm{d}x = (b - a)f_{average} = (b-a)\langle f \rangle$ So if we had some independent way of calculating the average value of the integrand, then we could evaluate the integral. This is where random numbers come in. Imagine that we had a list of random n...