How to Encrypt and Decrypt Files using Python

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • Support me on Patreon to access all the source code for my tutorials and join a private community of Python Programmers:
    / misha_sv
    In this tutorial we will explore how to encrypt and decrypt files using Python.
    Encryption is a process of converting information into some form of a code to hide its true content. The only way to access the file information then is to decrypt it.
    ⭐️ Timeline
    0:00 - Introduction
    1:21 - Creating encryption key
    3:29 - Loading encryption key
    5:12 - Encrypting a file
    8:18 - Decrypting a file
    11:24 - Conclusion
    📄 Resources
    - Full article with Python code: pyshark.com/encrypt-and-decry...
    - cryptography module: pypi.org/project/cryptography/
    - cryptography module documentation: cryptography.io/
    🔗 My Social Media
    - CZcams: / @mishasv
    - Website: pyshark.com
    - LinkedIn: / mikhail-sidyakov
    - TikTok: / mishamisha_sv
    - Instagram: / mishamisha_sv
    - Twitter: / mishamisha_sv
    - GitHub: github.com/misha-pyshark
    🎬 My CZcams Equipment
    - Microphone (Blue Yeti): amzn.to/3IeIsLg
    - Keyboard (Razer Ornata V2): amzn.to/3aeJIBt
    - Mouse (Logitech G403): amzn.to/3ReLUK4
    - Headphones (Bose Quiet Comfort 35 II): amzn.to/3uqidMq
    💸 Donations
    💵 One-Time Donations: www.paypal.com/donate/?hosted...
    💰 Patreon: / misha_sv
    --------------------------------------------------------------------------------------------------------------
    ⭐️ Tags
    - Encrypt Python
    - Encrypt files Python
  • Věda a technologie

Komentáře • 77

  • @imnoob2675
    @imnoob2675 Před rokem +10

    WOW ! Thanks ! Best tutorial ever ! I modified the script for make a Input for choose the file (path) you want to crypt . Really nice , its work with all documents (even the .png) ! Thanks so much ! 😄😊😉🙂

  • @govinej
    @govinej Před 3 lety +7

    Great video, it's very insightful and I learned alot...
    I'm going to build a password manager and I needed some form of basic encryption.
    You helped me out today thank you ...

  • @pdep
    @pdep Před rokem +1

    Nicely presented, just how I like it - organized, simple, straight-forward and, most importantly, no fluff, diversions, asides or unnecessary chatter. A really nice job. Thank you.

    • @MishaSv
      @MishaSv  Před 8 měsíci

      Glad you enjoyed it!

  • @pankajKushwaha085
    @pankajKushwaha085 Před 4 dny

    Thank you for this fantastic video on encryption and decryption! Your explanations were clear and insightful, making a complex topic easy to understand. I truly admire your ability to break down such intricate concepts so effectively. Keep up the great work!

  • @bhavikpunmiya9641
    @bhavikpunmiya9641 Před 7 měsíci +1

    You Really Earned a subscriber bro, I have learned alot and surely gonna apply in projects

  • @vapenhem1679
    @vapenhem1679 Před 2 lety +4

    Best video ive seen, it's sad that all copypaste videos are getting recommended over this. GREAT STUFF!!

  • @laythabdulkareem1887
    @laythabdulkareem1887 Před 8 měsíci +1

    Code works 100% 👍
    You're super amazing!

  • @AdityaPandey-vu2tf
    @AdityaPandey-vu2tf Před rokem +2

    Great sir , really very very impressive teaching way and knowlodge.

  • @khalidhussain5830
    @khalidhussain5830 Před 6 měsíci +1

    After so many hours I found you and thankyou !!!

    • @MishaSv
      @MishaSv  Před 6 měsíci +2

      I’m happy the tutorial is helpful!

    • @khalidhussain5830
      @khalidhussain5830 Před 6 měsíci +2

      @@MishaSv Bro , I created Encryption and Decryption app by your idea, in this app I provide some functionality like you can encrypt and decrypt text not only just text you can encrypt and decrypt the file and message which appear your files has been saved, After creating that app I got 2nd position in my class on Science Exhibition Day, and I am really gladd thankyou so much , this tutorial really help me

    • @MishaSv
      @MishaSv  Před 6 měsíci

      ⁠​⁠@@khalidhussain5830I’m happy it helped you! Subscribe to the channel and check out my other programming tutorials!

  • @Fullrusher
    @Fullrusher Před 2 lety

    How secure is this method, Im considering learning code again and thing I'd go back to making lil apps and found this subject to be interesting , do you think this would make a safe and secure enough encryption for an application?

  • @crazex193
    @crazex193 Před rokem +2

    how can i encrypt files that are in a directory

  • @quincychung5976
    @quincychung5976 Před rokem

    So the .key file type is considered as unlnown so read function doesnt work, anyone can help tell what needs to be done

  • @crispol4053
    @crispol4053 Před rokem

    I have similar code to encrypt multiple files, but it is really way too slow if the files are about 50+ files and have videos in it, I dont know what to do to speed it up

  • @perrysatoshi8528
    @perrysatoshi8528 Před 2 lety

    Hello and thank you for your video! I have managed to get my .py script, encrypted.
    But now i'm stuck on the next step, how to make it possible to run the encrypted .py file on raspberry.
    So for example i have script in .py, i want to share this script with other people, to enable them to run my script on their raspberry, but i want to keep the code within the script encrypted.
    Do you have any pointers for me on how to do this?
    Thanks in advance! And again thanks for the great video!

  • @booger8859
    @booger8859 Před 3 lety +1

    Thank!
    you

  • @romangeneral23
    @romangeneral23 Před 2 lety +1

    Hello,
    What do we do with the encryption key ? If for example I need to build a Production level app. How do I handle the encryption key ? Its bad practice to store it with the program. It is bad practice to hardcode the key into the code. What is the best approach ?

    • @MishaSv
      @MishaSv  Před 2 lety

      You can store it in some SFTP folder as a file which is read whenever you do operations with files in a production level app.

  • @CRjosh17
    @CRjosh17 Před 2 lety +1

    hi Misha. Thank you for the tutorial. Is there any way that I can just decrypt the file, and use those values in my code without having to create another file and write the decrypted data?

    • @MishaSv
      @MishaSv  Před 2 lety

      Yes you can!
      Use the code on lines 23-28 shown at 9:00 - 10:26. When you decrypt the file, it gets stored in a *decrypted* variable, and then you can use it in your code without having to write it out and read in again.

  • @joymackey8218
    @joymackey8218 Před 5 měsíci

    The only problem with encryption I tried it i encrypted a python script and tried running it it won't run it shows the encryption in cmd and invalid syntax

  • @YohanceToye
    @YohanceToye Před 6 měsíci +1

    i have an issue "ModuleNotFoundError: No module named 'cryptography'" any chance you know how to solve this

    • @MishaSv
      @MishaSv  Před 5 měsíci

      Have you installed the required module?
      If not, then open terminal and run:
      pip install cryptography

  • @subhendumondal7784
    @subhendumondal7784 Před rokem +1

    Nice tutorial... thank you...
    Can I decrypt any PHP files which is already Encrypted by this technique ?

    • @MishaSv
      @MishaSv  Před 5 měsíci

      You can if you have the encryption key.

  • @Runawaygeekchannel
    @Runawaygeekchannel Před 3 lety +1

    This was a symmetric key right? Assume using the asymmetric key pair works too?

    • @MishaSv
      @MishaSv  Před 3 lety +1

      There is a different way you should construct the keys for asymmetric encryption, because now you would need pairs to be created.
      Here is a good example how to do it: nitratine.net/blog/post/asymmetric-encryption-and-decryption-in-python/

  • @tomfiszelson1485
    @tomfiszelson1485 Před rokem +1

    hi, pretty sure that this account isn't active anymore, but if someone see this message, what's the use of opening the file with "rb" for example and not "r" i understand that b is binary but why do i am supposed to use that ? ( I'm french excuse my horrible english level )

    • @ChrisTheCringe
      @ChrisTheCringe Před rokem +1

      The 'b' stands for binary. Useful for reading and writing non-text or non-printable characters when you are doing projects such as this.

  • @avneeshgupta1139
    @avneeshgupta1139 Před 2 lety +1

    Bhai I have a encrypted .Gem file and I have its encryption key too then please can you tell me the way to undo it again into mp4 video please help🙏🏻

    • @MishaSv
      @MishaSv  Před 2 lety +1

      This is the closest I could find for your question: github.com/itay-grudev/encryption

    • @avneeshgupta1139
      @avneeshgupta1139 Před 2 lety

      Thankyou so much for your response🙏🏻
      Actually I am new in this field so that’s why I don’t know much about the source code and how to use this.It will be very great full if you make a program or share the tutorial to use the source code which you shared just now.Actually encrypted videos are my semester lectures that why I need it the most.Please help brother🙏🏻
      And please share the contact so that I will be in touch with you🙏🏻

  • @sararara3277
    @sararara3277 Před 2 lety +1

    Hi what kind of encryption type are you using?

    • @MishaSv
      @MishaSv  Před 2 lety

      Symmetric encryption with Fernet

  • @usefgadallah738
    @usefgadallah738 Před 2 lety +1

    The encryption key is not appearing in the mykey.key file. The file is empty.

    • @MishaSv
      @MishaSv  Před 2 lety

      You should try reinstalling the library and check again. What IDE are you using? I recommend to run it in VS Code, because if you need any additional plugins, it will let you download them right away, and you should see the key inside the .key file.

  • @kfithen
    @kfithen Před rokem +1

    Very helpful. I kept trying to learn this from ChatGPT, but it kept giving me the wrong answers and my code didn't work. Thank you!

    • @JarppaGuru
      @JarppaGuru Před 9 měsíci

      yes its chatgpt it not know is answer correct or not. you need allways check. i mean correct LOL

    • @MishaSv
      @MishaSv  Před 8 měsíci

      Glad it helped!

  • @ycersps9638
    @ycersps9638 Před 2 lety +1

    hi misha , the video was amazing , but to be honest I'm stuck at VS "cryptography.fernet " isn't working could you help me what I need to add to VS , its my first year as software eng student and that's 1 of my projects in python , could you help me please?

    • @MishaSv
      @MishaSv  Před 2 lety +1

      Did you install the library from the command line?
      pip install cryptography

    • @ycersps9638
      @ycersps9638 Před 2 lety

      @@MishaSv i've tried at like 55 times , doesn't work ;/ , idk what to do still clueless

    • @MishaSv
      @MishaSv  Před 2 lety +2

      This probably has to do with your Python interpreter in VS Code.
      What you want to do is open VS Code and press Ctrl+Shift+P and type "Python interpreter" you should see an option: "Python: Select Interpreter", click on it and you will see many Python interpreters available, try selecting one and see whether the underline will disappear in the code and you can run it. If it doesn't work, try a different Python interpreter.
      Here is the link explaining Python environments in VS Code: code.visualstudio.com/docs/python/environments

    • @ycersps9638
      @ycersps9638 Před 2 lety

      @@MishaSv I tried everything in the video but still the same problem, tbh I feel really bad and don't know how to do it, do you have discord by any chance you could teach me how to fix this problem?

  • @mohitkumargautam3692
    @mohitkumargautam3692 Před rokem +1

    Can I use it for all my python file or it is only for CSV file?

    • @MishaSv
      @MishaSv  Před rokem

      You can use it for any file.

  • @mahdiirandost7940
    @mahdiirandost7940 Před rokem +1

    Hello
    All my data was encrypted by (stop/djvu ransomware).
    Can I decrypt my data using Python?
    Help me please

    • @SuccessUniversityYT
      @SuccessUniversityYT Před 11 měsíci +1

      no unless you have the key

    • @mahdiirandost7940
      @mahdiirandost7940 Před 11 měsíci

      Now all my files are converted to tgvv.
      When I searched in CZcams, different people were able to decode it using tools

      czcams.com/video/c8Lvrf8NCAU/video.html
      czcams.com/video/fdSRSQDWAbA/video.html
      czcams.com/video/YfOPNWHseYo/video.html
      czcams.com/video/c5vs6Fso3Kk/video.html
      czcams.com/video/0onW0njNSZA/video.html
      These are the examples that I saw in CZcams.
      Do you think my information will be correct with the above methods?@@SuccessUniversityYT

  • @MPG187
    @MPG187 Před 3 lety +3

    Can you please use a larger font size? I can't see!!!

    • @MishaSv
      @MishaSv  Před 3 lety +3

      Thank you for your feedback! I will create larger font in the future videos!
      You can also access the content of the tutorial with step by step instructions on my Python blog:
      pyshark.com/encrypt-and-decrypt-files-using-python/

  • @benniiiz
    @benniiiz Před rokem +1

    Can it be used for doc files?

    • @MishaSv
      @MishaSv  Před rokem

      Yes, it can be used on other file formats.

  • @amaanashiqaa
    @amaanashiqaa Před rokem +1

    does this work with image file?

    • @MishaSv
      @MishaSv  Před rokem +1

      Yes, it should work with any file

  • @tharunkavishka8434
    @tharunkavishka8434 Před 2 lety +1

    Can i unlock ehi files

    • @MishaSv
      @MishaSv  Před 2 lety

      You can decrypt the files

  • @booger8859
    @booger8859 Před 3 lety +1

    VERY
    VER
    VE
    V
    V
    VE
    VER
    VERY

  • @atishdungdung7871
    @atishdungdung7871 Před 2 lety +1

    how to encrypt and decrypt arcade

    • @MishaSv
      @MishaSv  Před 2 lety

      What do you mean by arcade?

    • @atishdungdung7871
      @atishdungdung7871 Před 2 lety

      @@MishaSv sorry Misha actually it was a spelling mistake can you tell me how to encrypt and decrypt arcade

    • @MishaSv
      @MishaSv  Před 2 lety

      I still don't really understand what you mean by arcade.

  • @znqf
    @znqf Před rokem +1

    Can you send me the source?

    • @MishaSv
      @MishaSv  Před rokem

      pyshark.com/encrypt-and-decrypt-files-using-python/

  • @TechnoTerror-fk9zd
    @TechnoTerror-fk9zd Před 12 dny

    Nice but your site is malware.

  • @govinej
    @govinej Před 3 lety +4

    Great video, it's very insightful and I learned alot...
    I'm going to build a password manager and I needed some form of basic encryption.
    You helped me out today thank you ...