Navigate Select ESC Close

RSA Encryption From Scratch - Math & Python Code

2023-05-08 Science & Technology
42.0k
1.4k
75
NeuralNine
NeuralNine
475.0k subscribers

Unlock all features

FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.

Description

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 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm Timestamps: (0:00) Intro (0:25) Mathematical Theory (29:15) Python Implementation (42:30) Outro

Top Comments (10)

@BandetPandaCoin 2023-10-17

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)

34 2 replies
@_base_2 2023-07-25

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!!! 😃

22
@Blue-Robin 2025-07-13

Not gonna lie, this was the best explanation I've ever seen so far. I've gone through textbooks, random forums, etc. but this one explained the math very very well and got rid of the knowlege gaps I had

6
@tanzir3678 2023-07-13

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.

3
@borisakelovic9930 2024-04-01

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

2
@Facefur1 2025-06-14

lerady knew the RSA process, but having it done in code makes it very clear and easy to follow.

2
@mattanderson2074 2024-08-13

A year late to the party, but thanks for the great explanation and the fantastic demo in Python. I'm familiar with the concepts of RSA already, but it's always good to have it explained clearly for folk who haven't looked at the concepts and logic behind it and your explanation was great.

1
@nect0locus 2024-11-25

This is the best explanation of RSA I've ever seen

1
@huliang9001 2023-12-17

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

0
@raimo7911 2024-12-19

Thank you, thank you, thank you. Crazy how a YouTuber can explain better than professors

0

Unlock the Data Inside
Turn Videos into Knowledge

  • Get FREE 10/day: transcripts, summaries, chats
  • Chat with videos, export text & PDF
  • $1 free API credit for RAG, chatbots & research

Free forever plan • All features unlocked

App screenshot