How to Encrypt and Decrypt Files using Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ย. 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/en...
    - cryptography module: pypi.org/proje...
    - cryptography module documentation: cryptography.io/
    🔗 My Social Media
    - TH-cam: / @mishasv
    - Website: pyshark.com
    - LinkedIn: / mikhail-sidyakov
    - TikTok: / mishamisha_sv
    - Instagram: / mishamisha_sv
    - Twitter: / mishamisha_sv
    - GitHub: github.com/mis...
    🎬 My TH-cam 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...
    💰 Patreon: / misha_sv
    --------------------------------------------------------------------------------------------------------------
    ⭐️ Tags
    - Encrypt Python
    - Encrypt files Python

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

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

    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 ! 😄😊😉🙂

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

      Can we take a look at it??

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

      Hi, if you find some time, can you share a script with me

  • @pdep
    @pdep ปีที่แล้ว +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  10 หลายเดือนก่อน

      Glad you enjoyed it!

  • @govinej
    @govinej 3 ปีที่แล้ว +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 ...

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

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

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

      Thank you!

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

    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!

    • @MishaSv
      @MishaSv  19 วันที่ผ่านมา

      Glad it was helpful!

  • @AdityaPandey-vu2tf
    @AdityaPandey-vu2tf ปีที่แล้ว +2

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

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

      Thank you!

  • @bhavikpunmiya9641
    @bhavikpunmiya9641 9 หลายเดือนก่อน +1

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

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

      Thank you!

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

    how can i encrypt files that are in a directory

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

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

    • @MishaSv
      @MishaSv  7 หลายเดือนก่อน +2

      I’m happy the tutorial is helpful!

    • @khalidhussain5830
      @khalidhussain5830 7 หลายเดือนก่อน +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  7 หลายเดือนก่อน

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

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

    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

  • @laythabdulkareem1887
    @laythabdulkareem1887 10 หลายเดือนก่อน +1

    Code works 100% 👍
    You're super amazing!

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

      Thank you!

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

    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

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

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

  • @romangeneral23
    @romangeneral23 2 ปีที่แล้ว +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  2 ปีที่แล้ว

      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.

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

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

    • @MishaSv
      @MishaSv  2 ปีที่แล้ว

      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.

  • @tomfiszelson1485
    @tomfiszelson1485 ปีที่แล้ว +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 ปีที่แล้ว +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.

  • @YohanceToye
    @YohanceToye 8 หลายเดือนก่อน +1

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

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

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

  • @Fullrusher
    @Fullrusher 2 ปีที่แล้ว

    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?

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

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

    • @MishaSv
      @MishaSv  3 ปีที่แล้ว +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/

  • @avneeshgupta1139
    @avneeshgupta1139 2 ปีที่แล้ว +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  2 ปีที่แล้ว +1

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

    • @avneeshgupta1139
      @avneeshgupta1139 2 ปีที่แล้ว

      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🙏🏻

  • @perrysatoshi8528
    @perrysatoshi8528 2 ปีที่แล้ว

    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!

  • @CRjosh17
    @CRjosh17 2 ปีที่แล้ว +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  2 ปีที่แล้ว

      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.

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

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

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

      You can use it for any file.

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

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

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

      You can if you have the encryption key.

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

    Hi what kind of encryption type are you using?

    • @MishaSv
      @MishaSv  2 ปีที่แล้ว

      Symmetric encryption with Fernet

  • @ycersps9638
    @ycersps9638 2 ปีที่แล้ว +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  2 ปีที่แล้ว +1

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

    • @ycersps9638
      @ycersps9638 2 ปีที่แล้ว

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

    • @MishaSv
      @MishaSv  2 ปีที่แล้ว +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 2 ปีที่แล้ว

      @@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?

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

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

    • @MishaSv
      @MishaSv  3 ปีที่แล้ว +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 ปีที่แล้ว +1

    Can it be used for doc files?

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

      Yes, it can be used on other file formats.

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

    does this work with image file?

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

      Yes, it should work with any file

  • @kfithen
    @kfithen ปีที่แล้ว +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 11 หลายเดือนก่อน

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

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

      Glad it helped!

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

    Can i unlock ehi files

    • @MishaSv
      @MishaSv  2 ปีที่แล้ว

      You can decrypt the files

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

    Can you send me the source?

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

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

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

    Thank!
    you

    • @MishaSv
      @MishaSv  3 ปีที่แล้ว

      You are welcome!

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

    how to encrypt and decrypt arcade

    • @MishaSv
      @MishaSv  2 ปีที่แล้ว

      What do you mean by arcade?

    • @atishdungdung7871
      @atishdungdung7871 2 ปีที่แล้ว

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

    • @MishaSv
      @MishaSv  2 ปีที่แล้ว

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

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

    VERY
    VER
    VE
    V
    V
    VE
    VER
    VERY

  • @govinej
    @govinej 3 ปีที่แล้ว +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 ...