RSA Encryption From Scratch - Math & Python Code
Unlock all features
FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.
Unlock all features
FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.
Unlock all features
FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.
Unlock all features
FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.
Unlock all features
FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.
Related videos
Scikit-Learn Full Crash Course - Python Machine Learning
NeuralNine
37.2k views
latexify: Turn Python Functions Into Math Formulas
NeuralNine
19.1k views
Matplotlib Full Python Course - Data Science Fundamentals
NeuralNine
266.3k views
K-Means Clustering From Scratch in Python (Mathematical)
NeuralNine
42.6k views
Google Calendar Automation in Python
NeuralNine
61.2k views
Load Environment Variables From .env Files in Python
NeuralNine
100.4k views
Gradient Descent From Scratch in Python - Visual Explanation
NeuralNine
50.9k views
Live Face Recognition in Python
NeuralNine
216.1k views
Makefiles in Python For Professional Automation
NeuralNine
51.3k views
Optimize Your Python Programs: Code Profiling with cProfile
NeuralNine
53.5k views
Top Comments (10)
coding challange: only watch the math part and (try to) implement it yourself before watching the coding part
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)
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!!! 😃
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
lerady knew the RSA process, but having it done in code makes it very clear and easy to follow.
This "from the scratch" is sooo well done , Bravo
This is the best explanation of RSA I've ever seen
Thank you, thank you, thank you. Crazy how a YouTuber can explain better than professors
that was very good explaination for RSA algorithm . thank u
I really like these more theory based videos explained from scratch. In this way, I really learned something deep.
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
Top Comments (10)
coding challange: only watch the math part and (try to) implement it yourself before watching the coding part
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)
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!!! 😃
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
lerady knew the RSA process, but having it done in code makes it very clear and easy to follow.
This "from the scratch" is sooo well done , Bravo
This is the best explanation of RSA I've ever seen
Thank you, thank you, thank you. Crazy how a YouTuber can explain better than professors
that was very good explaination for RSA algorithm . thank u
I really like these more theory based videos explained from scratch. In this way, I really learned something deep.