Navigate Select ESC Close

RSA Encryption From Scratch - Math & Python Code

2023-05-08 Science & Technology
42.0k
1.4k
75
NeuralNine
NeuralNine
470.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)

@lennuard_6998 2023-05-08

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

45 2 replies
@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)

33 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

5
@Facefur1 2025-06-14

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

2
@borisakelovic9930 2024-04-01

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

2
@nect0locus 2024-11-25

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

1
@raimo7911 2024-12-19

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

0
@maral-q6k 2025-09-23

that was very good explaination for RSA algorithm . thank u

0
@huliang9001 2023-12-17

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

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