Learn to Code via Tutorials on Repl.it!

← Back to all posts
RSA Encryption!

Note: This requires previous knowledge of Python, modulus, prime numbers, and coprime numbers.

# Welcome to the amazing world of cryptography!

That makes this sound incredibly boring, but it's actually kinda, in a very weird way, interesting. So, today we're going to be talking about the cryptography, mainly asymmetric encryption, which is really annoying to type out because I have the worst spelling ever. Oh, well, that's what autocorrect's for!

### What is encryption?

So, there are two types of encryption, `symmetrical encryption`, and `asymmetrical encryption`. Encryption requires keys to `encrypt` and `decrypt` messages. Symmetrical encryption uses only one `key` that both you and the receiver know. One good example of symmetrical encryption is Caesar's Cipher, where the `key` would be the amount you rotate the letters by.

### Asymmetric Encryption

Before we had `asymmetric encryption`, we used only symmetric encryption, which required us to meet in person to discuss the key. So, when the internet came around and we needed to `encrypt` our `messages`, we couldn't really discuss or key online, as that completely ruins the point of the key, as it's supposed to be `secret`. We could always encrypt our keys, but then we'd need to encrypt those keys, and then we'd need to encrypt those keys too... I think you see where this is going.

So that's where asymmetrical encryption comes in! There are 2 different sets of keys that are `mathematically linked`, or in other words, magic. No, seriously though. The keys are completely different, yet somehow, are able to encrypt and decrypt each other! It actually is magic! Here's an example set of 8-bit keys: `[7,118] and [25,118]`.
So let me explain how RSA encryption works. Let's say Bob wants to send a message for Alice's eyes only. He'll encrypt his message with Alice's `public key`, which everyone knows. But the only way to decrypt it, is to use Alice's private key, which only she knows.
So, let's start coding this in Python!

First, create a class named `RSAEncryption`, and add a function called `createKeys()`:

``````class RSAEncryption():
def createKeys(self):
pass``````

Now, let's get to the maths behind it all. We're going to be using 8-bit keys, because if I tried to create a commercial-grade 2048-bit key, I'd be waiting 300 years for it to create a key pair.
We need import `sympy` to get a random prime between `2`, and `256`.

``````import sympy
class RSAEncryption():
def createKeys(self):
N = 257
while N > 256:
prime1 = sympy.randprime(2,256)
prime2 = sympy.randprime(2,256)
if prime1 == prime2:
prime1 = sympy.randprime(2,256)
prime2 = sympy.randprime(2,256)
N = prime1 * prime2``````

Then we need to get the number of coprime numbers lesser than `N`, so the easy way to do this is multiply `prime1-1` by `prime2-1`. For this tutorial, `prime1` will be 7, and `prime2` will be 2, for the sake of simplicity. We'll call this number `E`. (It's actually a Greek symbol like this: φ)
So the public key will be a random prime number between `2`, and `E`. It will have a second value of `N` as well.

EDIT: Thanks to a REPL.IT BUG, half the tutorial I wrote got deleted! BIG THANKS!

Highwayman (1443)

I already love it! Encryption is sick, can’t wait for you to finish :)

Also:

There are 2 different sets of keys that are `mathematically linked`, or in other words, magic.

XD

Infiniti20 (30)

@Highwayman Seriously? That means so much to me that somebody actually wants me to finish, lol!

Infiniti20 (30)

@Infiniti20 If you want to see the finished project, it's up on my profile as `RSA Key Encryption`!

Highwayman (1443)

Haha of course! Encryption’s a pretty fun topic and to be honest I’ve been stumbling about with RSA for quite a while, literally years in fact.
@Infiniti20