GithubHelp home page GithubHelp logo

borchand / rsa-encryption Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 364 KB

This is a project based on my SRP (specialised study project) from Danish high school. This repository implement RSA encryption in python

Python 100.00%
rsa-algorithm

rsa-encryption's Introduction

SRP - RSA encryption

This is a project based on my SRP (specialised study project) from Danish high school. The code implementation can be fund under the folder python_code.

RSA encryption

RSA encryption is developed by Ron Rivest, Adi Shamir and Len Adleman in 1977. It is based on the prime factorization problem. In order to encrypt and decrypt, two public keys๐Ÿ”‘๐Ÿ”‘ ($n$, $e$) and a private key๐Ÿ” $d$ is needed. They can be fund by the following steps:

  1. Choose two prime numbers $p$ and $q$, the set $n=p\cdot q$
  2. Calculate $\phi(n) = (p - 1)(q - 1)$
  3. Choose a whole number, so $0 < e <\phi(n)$ and $\text{gcd}(e, \phi(n)) = 1$
  4. Calculate $d$ so, $ed \equiv 1 (\text{mod } \phi(n))$

Now that the keys are fund, we can use them to encrypt. As follows

$$ c = m^e (\text{mod } n) $$

where $m$ is the messeage and $0 < m < n$. Note that $m$ needs to be smaller than $n$. This is why my implementation of RSA splits each sentence into individual letter.

When decrypting we use

$$ m = c^d (\text{mod } n) $$

Why does this work? Let start by substituting $c$ in when calculating $m$

$$ m = c^d (\text{mod } n) = (m^e (\text{mod } n))^d (\text{mod } n) $$

This can be written as

$$ m = m^{ed}(\text{mod } n) $$

Since $\text{gcd}(m, n) = 1$, we can rewrite $m$ as

$$ m = m^{ed(\text{mod } \phi(n))}(\text{mod } n) $$

The key $d$ is chosen such that $ed \equiv 1 (\text{mod } \phi(n))$. This can also be written as $ed(\text{mod } \phi(n)) = 1$. Therefore we can now write

$$ m = m^{1}(\text{mod } n) = m (\text{mod } n) $$

This only applies when $\text{gcd}(e, \phi(n) = 1)$.

The prime factorization problem

To break the RSA encryption we need to find the private key $d$, which is calculated based on $e$ and $\phi(n)$. Luckily we know the $e$ as it is a public key. So, we "only" need to find $\phi(n)$. We know $\phi(n) = (p-1)(q-1)$. This means we need to find $p$ and $q$ in order to find $\phi(n)$. This is where the other key $n$ comes into play. We know

$$ n = p\cdot q $$

We can use Fermat's method to find $p$ and $q$. Fermat's method tells us that $n$ is an uneven whole number, that can factorized only if there is a solution to $n = x^2-y^2$ where $x$ and $y$ must be whole numbers. Since $x$ and $y$ ar whole number $n$ can be written as

$$ n = x^2-y^2 = (x+y)(x-y) $$

To find $x$ and $y$ we use

$$ x^2 - n = y^2 $$

When a solution where $x$ and $y$ are whole numbers is fund, we can now find $p$ and $q$

$$ n = (x+y)(x-y) = p\cdot q $$

$$ \Rightarrow p = x+y \wedge q = x-y $$

rsa-encryption's People

Contributors

borchand avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.