├── README.md ├── RSA Python.sln ├── RSA Python.pyproj └── RSA_Python.py /README.md: -------------------------------------------------------------------------------- 1 | # RSA-Python 2 | The RSA algorithm coded in Python 3 | 4 | Created in collaboration with [Unnikrishnan Menon](https://github.com/7enTropy7) 5 | 6 | **RSA Algorithm** 7 | * Pick two large primes $p,q$. 8 | * Compute $n=pq$ and $\varphi(n)=\mathrm{lcm}(p-1,q-1)$ 9 | * Choose a public key $e$ such that $1< e< \varphi(n)$ and $\gcd(e,\varphi(n))=1$ 10 | * Calculate $d$ such that $de\equiv 1 \pmod\varphi(n)$ 11 | * Let the message **key** be $m$ 12 | * **Encrypt: ** $c\equiv m^e\pmod n$ 13 | * **Decrypt: ** $m\equiv c^d\pmod n$ 14 | 15 | ![image](https://user-images.githubusercontent.com/7680591/59566357-3a722d00-902d-11e9-991c-b67e5f369722.png) 16 | 17 | And this is what a code says…. 18 | ![rsa](https://user-images.githubusercontent.com/7680591/59566011-0e07e200-9028-11e9-9f54-449d0ff23a28.jpg) 19 | 20 | Note that you may have to setup an external dependency environment in some cases, it would create a big chunk of files that are not here due to exceeding the maximum number of files to upload. 21 | -------------------------------------------------------------------------------- /RSA Python.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29009.5 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "RSA Python", "RSA Python.pyproj", "{24393137-B8A3-41AD-B263-39AAB3586B47}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {24393137-B8A3-41AD-B263-39AAB3586B47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {24393137-B8A3-41AD-B263-39AAB3586B47}.Release|Any CPU.ActiveCfg = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | GlobalSection(ExtensibilityGlobals) = postSolution 21 | SolutionGuid = {80DC23D8-68E3-4C6C-9A9B-629315FE5E4F} 22 | EndGlobalSection 23 | EndGlobal 24 | -------------------------------------------------------------------------------- /RSA Python.pyproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | Debug 4 | 2.0 5 | 24393137-b8a3-41ad-b263-39aab3586b47 6 | . 7 | RSA_Python.py 8 | 9 | 10 | . 11 | . 12 | RSA Python 13 | RSA Python 14 | MSBuild|env|$(MSBuildProjectFullPath) 15 | 16 | 17 | true 18 | false 19 | 20 | 21 | true 22 | false 23 | 24 | 25 | 26 | 27 | 28 | 29 | env 30 | 3.7 31 | env (Python 3.7 (32-bit)) 32 | Scripts\python.exe 33 | Scripts\pythonw.exe 34 | PYTHONPATH 35 | X86 36 | 37 | 38 | 39 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /RSA_Python.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | #required for the sqrt() function, if you want to avoid doing **0.5 3 | import random 4 | #required for randrange 5 | from random import randint as rand 6 | 7 | #just to use the well known keyword rand() from C++ 8 | 9 | 10 | def gcd(a, b): 11 | if b == 0: 12 | return a 13 | else: 14 | return gcd(b, a % b) 15 | 16 | 17 | def mod_inverse(a, m): 18 | for x in range(1, m): 19 | if (a * x) % m == 1: 20 | return x 21 | return -1 22 | 23 | 24 | def isprime(n): 25 | if n < 2: 26 | return False 27 | elif n == 2: 28 | return True 29 | else: 30 | for i in range(2, int(sqrt(n)) + 1, 2): 31 | if n % i == 0: 32 | return False 33 | return True 34 | 35 | 36 | #initial two random numbers p,q 37 | p = rand(1, 1000) 38 | q = rand(1, 1000) 39 | 40 | 41 | def generate_keypair(p, q, keysize): 42 | # keysize is the bit length of n so it must be in range(nMin,nMax+1). 43 | # << is bitwise operator 44 | # x << y is same as multiplying x by 2**y 45 | # i am doing this so that p and q values have similar bit-length. 46 | # this will generate an n value that's hard to factorize into p and q. 47 | 48 | nMin = 1 << (keysize - 1) 49 | nMax = (1 << keysize) - 1 50 | primes = [2] 51 | # we choose two prime numbers in range(start, stop) so that the difference of bit lengths is at most 2. 52 | start = 1 << (keysize // 2 - 1) 53 | stop = 1 << (keysize // 2 + 1) 54 | 55 | if start >= stop: 56 | return [] 57 | 58 | for i in range(3, stop + 1, 2): 59 | for p in primes: 60 | if i % p == 0: 61 | break 62 | else: 63 | primes.append(i) 64 | 65 | while (primes and primes[0] < start): 66 | del primes[0] 67 | 68 | #choosing p and q from the generated prime numbers. 69 | while primes: 70 | p = random.choice(primes) 71 | primes.remove(p) 72 | q_values = [q for q in primes if nMin <= p * q <= nMax] 73 | if q_values: 74 | q = random.choice(q_values) 75 | break 76 | print(p, q) 77 | n = p * q 78 | phi = (p - 1) * (q - 1) 79 | 80 | #generate public key 1