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
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.
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
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
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
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
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
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!
I already love it! Encryption is sick, can’t wait for you to finish :)
There are 2 different sets of keys that are
mathematically linked, or in other words, magic.