RSA Encryption From Scratch - Math & Python Code

preview_player
Показать описание
Today we learn about RSA. We take a look at the theory and math behind it and then we implement it from scratch in Python.

◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚

🌐 Social Media & Contact 🌐

Timestamps:
(0:00) Intro
(0:25) Mathematical Theory
(29:15) Python Implementation
(42:30) Outro
Рекомендации по теме
Комментарии
Автор

Please keep this type of in-depth video coming! I learned a tremendous amount from your clear explanations, and am able to implement my own programs based on your clear, and logical, step-by-step walk through. Please, more like this!!! 😃

_base_
Автор

here is the code if you are lazy to write:

import random
import math


# n= p.q
#phi(n) = phi(p.q)=phi(p).phi(q) = (p-1). (q-1)
#phi(n) = (p-1.q-1)


def is_prime (number):
if number < 2:
return False
for i in range (2, number // 2 +1):
if number % i == 0:
return False
return True

def generate_prime (min_value, max_value):
prime = random.randint (min_value, max_value)
while not is_prime(prime):
prime = random.randint(min_value, max_value)
return prime


def mod_inverse(e, phi):
for d in range (3, phi):
if (d * e) % phi == 1:
return d
raise ValueError ("Mod_inverse does not exist!")

p, q = generate_prime(1000, 50000), generate_prime ( 1000, 50000)
while p==q:
q= generate_prime(1000, 50000)

n = p * q
phi_n = (p-1) * (q-1)

e = random.randint (3, phi_n-1)
while math.gcd(e, phi_n) != 1: #gcd=greater common denometer != not equal
e = random.randint (3, phi_n - 1)

d = mod_inverse(e, phi_n)


message = input("Enter your message to Encrypt ")


print ("Prime number P: ", p)
print ("Prime number q: ", q)
print ("Public Key: ", e)
print ("Private Key: ", d)
print ("n: ", n)
print ("Phi of n: ", phi_n, " Secret")




message_encoded = [ord(ch) for ch in message]

print ("Message in ASCII code: ", message_encoded)

# (m ^ e) mod n = c
ciphertext = [pow(ch, e, n) for ch in message_encoded]

print (message, " Ciphered in: ", ciphertext)

Decodemsg= [pow(ch, d, n) for ch in ciphertext]
print ("back to ASCII: ", Decodemsg)
msg = "".join (chr(ch) for ch in Decodemsg)
print("from ASCII to TEXT: ", msg)

Peacfull
Автор

I havent watched the video yet, but title alone and topic. Thumbs up.

efox
Автор

This "from the scratch" is sooo well done, Bravo

borisakelovic
Автор

Please make more videos like this it helps us code by understanding the concepts

aritraghosh
Автор

Good one. Yes, thorough explanation covering Maths and all is much beneficial than just demonstration of using some libraries. There are tons of people out there who would follow the second approach. And that makes you standout.

tanzir
Автор

thanks i really like this being on the cyber security side of things i have to teach myself coding basically so these videos are great on stuff like this and now i have an understanding of how rsa works mathematically so thanks!

Radical
Автор

I really like these more theory based videos explained from scratch. In this way, I really learned something deep.

huliang
Автор

Thanks - another really useful lecture and practical demo. Cheers 😃

paulthomas
Автор

I really like these more in depth videos. For me, learning the fundamental principles of how something works is by far the best way to understand it. Definitely do what has been suggested elsewhere: watch the theory then try and implement it in Python before seeing how it's done! More of these please, if you can!

sl
Автор

coding challange: only watch the math part and (try to) implement it yourself before watching the coding part

lennuard_
Автор

Great explanation and nice example in Python. Thanks.

nicolamombelli
Автор

Great video; thank you for sharing.. super helpful!

TritdawG
Автор

Yeah, I really wanted you to release a video on this topic from scratch

lintop
Автор

I found this usefull for my studies. Thanks :D

bartektrybaa
Автор

loving the content its helping me alot

silentkille
Автор

I live the mix of these kind of videos and cool usefull stuff (like pomodoro). 😊

Ungerlogik
Автор

I like such videos a lot. I'll definitely use this video in my upper sixth computer science course here in Germany since RSA is a topics on the curriculum. The implementaion will be in JAVA, though, since this is the language used in Years 11-13.

micleh
Автор

awesome, thanks for making this video, .

chyldstudios
Автор

really interesting these day i look in aes 256 bits encry^tion i don t know whitch is best but both are really interresting

tcgvsocg