RSA Encryption From Scratch - Math & Python Code

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ม.ค. 2025

ความคิดเห็น • 67

  • @_base_2
    @_base_2 ปีที่แล้ว +18

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

  • @efox29
    @efox29 ปีที่แล้ว +4

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

  • @borisakelovic9930
    @borisakelovic9930 9 หลายเดือนก่อน

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

  • @tanzir3678
    @tanzir3678 ปีที่แล้ว +2

    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.

  • @nect0locus
    @nect0locus 2 หลายเดือนก่อน

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

  • @mattanderson2074
    @mattanderson2074 5 หลายเดือนก่อน +1

    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.

  • @sl3262
    @sl3262 ปีที่แล้ว

    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!

  • @raimo7911
    @raimo7911 หลายเดือนก่อน

    Thank you, thank you, thank you. Crazy how a TH-camr can explain better than professors

  • @Revamped1953
    @Revamped1953 ปีที่แล้ว +2

    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!

  • @huliang9001
    @huliang9001 ปีที่แล้ว

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

  • @lennuard_6998
    @lennuard_6998 ปีที่แล้ว +33

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

    • @micleh
      @micleh ปีที่แล้ว +3

      Exactly. I often do this in my lessons (flipped classroom principle): watching a video at home, ironing out diffuculties in class and them implementing it in the lessons so as to figure out if the theory has been understood.

    • @certitolla202
      @certitolla202 ปีที่แล้ว +2

      Done 😸✅✅

  • @aritraghosh999
    @aritraghosh999 7 หลายเดือนก่อน +1

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

  • @BandetPandaCoin
    @BandetPandaCoin ปีที่แล้ว +21

    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)

  • @lintop3908
    @lintop3908 ปีที่แล้ว

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

  • @Abomin81onVlog
    @Abomin81onVlog ปีที่แล้ว +1

    I enjoyed this video greatly.

  • @WahranRai
    @WahranRai ปีที่แล้ว +1

    27:43 Which calculator did you use

    • @garydunken7934
      @garydunken7934 10 หลายเดือนก่อน

      Microsoft Windows calculator

    • @WahranRai
      @WahranRai 10 หลายเดือนก่อน

      @@garydunken7934 Thank you, i already found it !

  • @aryanlivi3553
    @aryanlivi3553 11 หลายเดือนก่อน

    Great Explanation!!

  • @silentkille4
    @silentkille4 ปีที่แล้ว

    loving the content its helping me alot

  • @ahmehhhd
    @ahmehhhd ปีที่แล้ว

    amazing video. we need more of these

  • @paulthomas1052
    @paulthomas1052 ปีที่แล้ว

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

  • @bartektrybaa3922
    @bartektrybaa3922 ปีที่แล้ว

    I found this usefull for my studies. Thanks :D

  • @micleh
    @micleh ปีที่แล้ว

    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.

  • @huliang9001
    @huliang9001 ปีที่แล้ว

    Really awesome! Thanks bro!😀

  • @TritdawG
    @TritdawG ปีที่แล้ว

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

  • @nicolamombelli2149
    @nicolamombelli2149 ปีที่แล้ว

    Great explanation and nice example in Python. Thanks.

  • @ghAmputeeAtheist
    @ghAmputeeAtheist ปีที่แล้ว +1

    you can get the inverse of e mod phi with pow(e,-1,phi)

  • @Ungerlogik
    @Ungerlogik ปีที่แล้ว

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

  • @khairysuleiman4
    @khairysuleiman4 ปีที่แล้ว

    Thanks Mr. Best!

  • @rubenenrique3210
    @rubenenrique3210 25 วันที่ผ่านมา

    dude, you are awesome, ty

  • @prosodyspeaks4036
    @prosodyspeaks4036 ปีที่แล้ว

    yeah! more like this!

  • @user-ku6gw1wm6b
    @user-ku6gw1wm6b 5 หลายเดือนก่อน

    Nice one.

  • @amirabas8817
    @amirabas8817 5 หลายเดือนก่อน

    Brother, you didn't put any corner of Python outside of your empire
    That's impressive 👏

  • @cpp705
    @cpp705 ปีที่แล้ว

    thanks i needed it

  • @harsharya828
    @harsharya828 ปีที่แล้ว

    Thanks for this knowledge.

  • @nejathakan5521
    @nejathakan5521 ปีที่แล้ว

    Keep this kind of videos please.

  • @salahtamimy5322
    @salahtamimy5322 9 หลายเดือนก่อน

    Thank you 👍

  • @RajapuRishitha-jc6xy
    @RajapuRishitha-jc6xy 8 หลายเดือนก่อน

    If n is known, since n can be written as the product of p and q only(given p and q are prime numbers), we can find p and q right?

  • @chyldstudios
    @chyldstudios ปีที่แล้ว

    awesome, thanks for making this video,.

  • @PawitSahare
    @PawitSahare ปีที่แล้ว

    And awesome video bro

  • @JO06
    @JO06 ปีที่แล้ว

    Thank you brother

  • @piotrmazgaj
    @piotrmazgaj ปีที่แล้ว

    Very nice.

  • @seeesh_
    @seeesh_ ปีที่แล้ว

    Hi, I would like to ask some Pandera tutorial to do data quality, doing some advance transformation on data. That's possible?

  • @tcgvsocg1458
    @tcgvsocg1458 ปีที่แล้ว

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

  • @BandetPandaCoin
    @BandetPandaCoin ปีที่แล้ว

    nice work but where you selected 7, and that part i did not get it completely how did you come up with 7. I have wachted that part again and it was not clear for me. but still your explanation was very good. thank you. keep up the good work.

  • @JarppaGuru
    @JarppaGuru 11 หลายเดือนก่อน

    26:26 bob know e n and he make c. and m=c^d mod n
    r we clear yet? how hard is find out d? bob know all but d. we can brute force private key so long as we get our original m=15
    alice public key need hold something its e and n
    alice privet key have to have some number and p q. n and phi we can calculate and e is just mod inverse d
    so how hard brute force m=c^d mod n. if we know all but not d xD

  • @DroughtBee
    @DroughtBee 8 หลายเดือนก่อน

    Doesn’t this leave itself open to frequency analysis because all the letters are just given (effectively for our purposes) random numbers? Why is this better than just any other mono-alphabetical code?

    • @christopherjenkins6174
      @christopherjenkins6174 หลายเดือนก่อน

      Yes, it would actually. But you have to consider use cases. Asymmetric encryption isn’t used for large file transfers or communication for the most part. It’s used to transfer other keys, signatures, and other small pieces of data. If you’re transferring a large amount of data, then you should 100% use symmetric encryption because AES employs confusion and diffusion. Dunno if you care any more, but hopefully this helps someone else too!

  • @bhagyalakshmi1053
    @bhagyalakshmi1053 ปีที่แล้ว

    How to create vlu ?

  • @JarppaGuru
    @JarppaGuru 11 หลายเดือนก่อน

    29:01 so how hard brute force private key if we know s n and m. bob can do s=m^d mod n too
    mean try every thing 141=15^d mod 143
    why we sign using d and n when we should sign using phi_n
    alices=m^d mod phi_n-1
    bobs=alices^m mod e = 1 if message and sign match at least all test i calculated LOL
    now bob cant brute force alice d... or i have formula for that do its same simple LOL
    but u made tutorial i say dont do s=m^d mod n you give away private key. he knows all but one. you need have atleast 2 unkown number in equation to make it harder

  • @user-ku6gw1wm6b
    @user-ku6gw1wm6b 3 หลายเดือนก่อน

    More of this

  • @danielw8620
    @danielw8620 ปีที่แล้ว

    May I ask what your educational background is? Are you self taught, did you go to college for a Math degree/CS degree? Thanks

  • @CthRage8946
    @CthRage8946 6 หลายเดือนก่อน

    You can make someone very rich!

  • @raspberryPi1337
    @raspberryPi1337 ปีที่แล้ว

    Hello, how old are you?

  • @Andr_e_y
    @Andr_e_y 7 หลายเดือนก่อน

    It's a shame there's no salt here. Thank you😊

  • @ElaidTebabkha
    @ElaidTebabkha หลายเดือนก่อน

    Thanks this , look this code : from math import gcd, sqrt
    import os
    from random import randint
    def is_prime(n: int) -> bool:
    d = 0
    for i in range(2, int(sqrt(n + 1))):
    if n % i == 0:
    d += 1
    if d == 0:
    return True
    return False
    def choose_p_q() -> set:
    p = randint(1000, 1000000 + 1)
    q = randint(1000, 1000000 + 1)
    while not is_prime(p) or not is_prime(q) or not (p != q):
    p = randint(1000, 1000000 + 1)
    q = randint(1000, 1000000 + 1)
    return (p, q)
    def generate_keys(pq: set) -> set:
    p, q = pq
    n = p * q
    phi = (p - 1) * (q - 1)
    e = randint(1000, phi)
    while gcd(e, phi) != 1:
    e = randint(1000, phi)
    d = pow(e, -1, phi)
    return (n, e, d)
    def encrypt(message: str, n_keys: set) -> list[int]:
    n, e, _ = n_keys
    return [pow(ord(char), e, n) for char in message]
    def decrypt(message: list[int], n_keys: set) -> str:
    n, _, d = n_keys
    return "".join([chr(pow(num, d, n)) for num in message])
    def main():
    pq = choose_p_q()
    n_keys = generate_keys(pq)
    try:
    message = input("Enter message to encrypt by RSA : ")
    except TypeError as e:
    print(e)
    print(f"(n,public_key,private_key) = {n_keys}")
    encrypted_message = encrypt(message, n_keys)
    print(f"Encrypted message: {encrypted_message}")
    decrypted_message = decrypt(encrypted_message, n_keys)
    print(f"Decrypted message: {decrypted_message}")
    if __name__ == "__main__":
    os.system("clear")
    main()

  • @zstar8397
    @zstar8397 ปีที่แล้ว

    Hey hope you are doing alright just I wanna say that
    GOD loved the world so much he sent his only begotten
    son Jesus to die a brutal death for us so that we can have eternal life and we can all accept this amazing gift this by simply trusting in Jesus, confessing that GOD raised him from the dead, turning away from your sins and forming a relationship with GOD.

  • @PawitSahare
    @PawitSahare ปีที่แล้ว

    Don't reply this comment