No video

Encrypt files and strings with AES and Pycrypto

  Рет қаралды 26,344

Practical Python Solutions

Practical Python Solutions

Күн бұрын

💻Don't Forget to Subscribe to my Channel! 🖥
/ @practicalpythonsoluti...
------
Pycrypto is a collection of cryptographic modules for Python. It has secure hash functions and symmetric encryption algorithms. It has not been in development for quite some time and it’s kinda getting phased out. However, I think the interest stems from the ability to encrypt files with strong AES encryption using a password.
So that’s exactly what I’ll show you how to do in this video.
If you’re not sure what symmetric encryption is, or where it fits into the whole encryption framework, you can look at my original video on symmetric encryption, which gives a super quick and simple overview
First, we need to install the pycrypto library. This can be a bit tricky to install at times so best to run a pip3 list first to ensure that you do now have other conflicting encryption libraries installed. There are some know issues with Pycrypto and Pycryptodome for instance so that’s one you may need to remove if present.
The install is straightforward. I am using Python 3.8 here. I have it running on 3.6 and 3.7 as well.

Пікірлер: 68
@user-ou8lg4sk2g
@user-ou8lg4sk2g 4 жыл бұрын
thank you from Russia!
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 4 жыл бұрын
Даниил Ибрагимов, Пожалуйста!
@judeleon8485
@judeleon8485 4 жыл бұрын
Thanks so much. This was really helpful
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 4 жыл бұрын
Thanks for the feedback. Let me know if are interested in other aspects of encryption / Python.
@pascalsimo8968
@pascalsimo8968 4 жыл бұрын
really helpful 🤩🤩🤩🤩
@pascalsimo8968
@pascalsimo8968 4 жыл бұрын
@@practicalpythonsolutions-b4478 is there a way to hide data inside image or Dicom pixel data? (Stegonography) thank you
@codewithashok7856
@codewithashok7856 2 жыл бұрын
@@pascalsimo8968 ive done this project for my client
@nanaabenanyamekye9708
@nanaabenanyamekye9708 3 жыл бұрын
Thanks a ton!
@nora4656
@nora4656 3 жыл бұрын
thank you for the video, but i tried the code and i got an error (ValueError: Data must be padded to 16 byte boundary in CBC mode)
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
The keys and data must be in multiples of 16 bytes or 128 bits. I put a function in to pad the data: def pad_message(file): while len(file) % 16 != 0: file = file + b'0' return file you can see the file at: github.com/pmahon2016/symmetric_encryption/blob/master/file_pycrypto_encrypt.py
@SaiRam-yd4px
@SaiRam-yd4px 2 жыл бұрын
hello sir i am getting this error Object type cannot be passed to C code
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 2 жыл бұрын
Hi there - where are you getting this message. Can you narrow it down to a particular line of code?
@00rss
@00rss 2 жыл бұрын
I also get that problem, try this code from Crypto.Cipher import AES import hashlib password = b"mypassword" key = hashlib.sha256(password).digest() mode = AES.MODE_CBC IV = b"This is an IV456" cipher = AES.new(key, mode, IV) message = b"1234567890123456" mensaje_encryptado = cipher.encrypt(message) print(mensaje_encryptado)
@jeffsmith8280
@jeffsmith8280 2 жыл бұрын
@@practicalpythonsolutions-b4478 me too but the issue im having , when ever i want to encrypt the variable like this ,ciphertext = cipher.encrypt(padding_message) print(ciphertext) i get the error Object type cannot be passed to C code, pls how can fix this
@osamaamarneh5762
@osamaamarneh5762 3 жыл бұрын
Thanks a lot
@thedarkknight579
@thedarkknight579 2 жыл бұрын
❤️
@soheil.322
@soheil.322 2 жыл бұрын
Tankyou bro this video very helpful😃
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 6 ай бұрын
You are very welcome. I am glad you found it useful!
@noaprevilon9414
@noaprevilon9414 Жыл бұрын
What's the name of the program u start the video in?
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 Жыл бұрын
pyCrypto?
@saravananaryan
@saravananaryan 3 жыл бұрын
The iv value should be same for both encryption and decryption??
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
Yes, will need to be the same
@cameronmann8825
@cameronmann8825 3 жыл бұрын
When trying to add the file, I get an error message "[Errno 13] Permission denied:" Any ideas on why this is/ how to fix it?
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
Cameron - how are you "adding the file" Can you look at the file attributes to see if there is actually a permission issue? If you are in PyCharm and having issues adding a file... you could create one and see if that works. Let me know where exactly you are getting the error
@cameronmann8825
@cameronmann8825 3 жыл бұрын
@@practicalpythonsolutions-b4478 I figured out the problem, thank you for the response. However now, I am able to encrypt the file but when I go to decrypt the file, I get the error "Data must be padded to 16 byte boundary in CBC mode." I feel like this is an easy fix, but can not figure it out. Thank you!
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
@@cameronmann8825 yeah, when you encrypted it, the data should ahve been padded github.com/pmahon2016/symmetric_encryption/blob/master/file_pycrypto_encrypt.py
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
@@cameronmann8825 See below the code. Make sure you padded when you encrypted github.com/pmahon2016/symmetric_encryption/blob/master/file_pycrypto_decrypt.py
@cameronmann8825
@cameronmann8825 3 жыл бұрын
@@practicalpythonsolutions-b4478 Thank you again for the quick response. When I encrypt the file I get a message saying it is not UTF-8 encoded, does this mean I encrypted the message correctly, or is there another problem. Thank you.
@sebastianestrada1311
@sebastianestrada1311 2 жыл бұрын
How do I decrypt this in node js?. With a text it works but with a file it doesn't.
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 6 ай бұрын
Hi. Sorry for the late reply. I am back online and I will create a short in the next few days to answer your question. Thank you for your understanding.
@pniavis
@pniavis 4 жыл бұрын
I think that padding with spaces or any other predefined character is insecure.
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 4 жыл бұрын
You are probably correct but selecting a longer key lenght should help. But as suggested in the video, you should consider using Pycryptodome instead of Pycrypto going forward as it fixes some of the flaws in Pycrypto.
@oubaidas7100
@oubaidas7100 3 жыл бұрын
I could not install pycrypto. can you help ?
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
There is an issue with pycrypto and pycryptodome so make sure you don t have both installed. Do a “pip list” to verify. You can also uninstall pycrypto and reinstall. Type ‘pip remove pycrypto ‘ If not, let me know the error
@user-ry2lk6ol3s
@user-ry2lk6ol3s 4 жыл бұрын
hello, i cannot install pycrypto for python 3.8
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 4 жыл бұрын
you may need to uninstall pycryptodome first as it conflicts, and then install. Do a pip3 list to see what you have installed already.
@user-ry2lk6ol3s
@user-ry2lk6ol3s 4 жыл бұрын
@@practicalpythonsolutions-b4478 Command "C:\Users\sevav\PycharmProjects ike_click\venv\Scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\sevav\\AppData\\Local\\Temp\\pip-install-1zq3_eyk\\pycrypto\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().repla ce(' ', ' ');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\sevav\AppData\Local\Temp\pip-record-u8lllff5\install-record.txt --single-version-externally-managed --compile --install-headers C:\Users\sevav\PycharmProjects ike_click\ven v\include\site\python3.7\pycrypto" failed with error code 1 in C:\Users\sevav\AppData\Local\Temp\pip-install-1zq3_eyk\pycrypto\
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 4 жыл бұрын
@@user-ry2lk6ol3s this is on Windows. Did you get this during the install or when you tried to import?
@user-ry2lk6ol3s
@user-ry2lk6ol3s 4 жыл бұрын
Python Practical Solutions - By Paul Mahon on install
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 4 жыл бұрын
Всеволод Овчинииков can you do a “pip3 list”
@MyDavidsen
@MyDavidsen 3 жыл бұрын
This code is not running in Python 3.7 and upper. must be modified with unit64
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
Hi not sure what that means but this code was run on 3.8 and other versions. What exactly does not run. i.e. what command are you typing and the error associated?
@MyDavidsen
@MyDavidsen 3 жыл бұрын
@@practicalpythonsolutions-b4478 Thanks tor quick response! I fixed the issue and I paste the code below if someone else needs it. just a small tips: Avoid installing Pycrypto library because it gives you a bunch of errors. Instead, use Pycryptodome library. Encryption part from Crypto.Cipher import AES import hashlib pas = input("Enter your password to encrypt: ").encode('utf-8') msg = input("Enter the message you want to encrypt: ").encode('utf-8') def padding(msg): while len(msg) % 16 != 0: msg = msg+b" " return msg key = hashlib.sha3_256(pas).digest() cipher = AES.new(key, AES.MODE_CBC) msg_pad = padding(msg) encrypt_msg = cipher.encrypt(msg_pad) with open('data', 'wb') as wfile: wfile.write(cipher.iv) wfile.write(encrypt_msg) Decryption part from Crypto.Cipher import AES import hashlib pas = input("Enter your password to encrypt: ").encode('utf-8') key = hashlib.sha3_256(pas).digest() with open('data', 'rb') as rfile: cipher = AES.new(key, AES.MODE_CBC, rfile.read(16)) decrypt_msg = cipher.decrypt(rfile.read()) decrypt_msg = decrypt_msg.decode('utf-8') print(decrypt_msg.rstrip()) Thx
@llIIllIlIIllX_XIillIIllIIllIll
@llIIllIlIIllX_XIillIIllIIllIll 2 жыл бұрын
where is the github link? ....
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 2 жыл бұрын
github.com/pmahon2016/symmetric_encryption.git
@twinklestar5270
@twinklestar5270 3 жыл бұрын
Can u show us aes ctr mode?
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
Yes will do. Thanks for mentioning
@bhawanasharma6445
@bhawanasharma6445 3 жыл бұрын
hey why password is there? what purpose does it serve
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
the password is really the key in this case. Or at least the key is derived from the password. If this doesn't answer your question ..give me the specific line of code but that is generally the case
@bhawanasharma6445
@bhawanasharma6445 3 жыл бұрын
@@practicalpythonsolutions-b4478 Traceback (most recent call last): File "C:\Users\Bhawana\PycharmProjects\pythonProject\main.py", line 16, in encrypted_message = cipher.encrypt(padded_message) File "C:\Users\Bhawana\AppData\Local\Programs\Python\Python39\lib\site-packages\Crypto\Cipher\_mode_cbc.py", line 178, in encrypt c_uint8_ptr(plaintext), File "C:\Users\Bhawana\AppData\Local\Programs\Python\Python39\lib\site-packages\Crypto\Util\_raw_api.py", line 232, in c_uint8_ptr raise TypeError("Object type %s cannot be passed to C code" % type(data)) TypeError: Object type cannot be passed to C code
@bhawanasharma6445
@bhawanasharma6445 3 жыл бұрын
i am getting this error
@ardiannewstar1213
@ardiannewstar1213 3 жыл бұрын
bro i had a problem with instaling pycrypto
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
Hi - there is a problem with pycrypto and pycryptodome. Make sure you do NOT have both installed. Run a "Pip[3] list" to confirm. if not, uninstall and reinstall - ie. pip[3] uninstall pycrypto. If all that fails ....give me the specific error when you import the library
@ardiannewstar1213
@ardiannewstar1213 3 жыл бұрын
@@practicalpythonsolutions-b4478 Package Version --------------- --------- certifi 2020.6.20 cffi 1.14.0 chardet 3.0.4 crypto 1.4.1 cryptography 2.9.2 cycler 0.10.0 idna 2.10 imageio 2.8.0 kiwisolver 1.2.0 matplotlib 3.2.2 Naked 0.1.31 numpy 1.19.0 Pillow 7.1.2 pip 20.2.4 pycparser 2.20 pyparsing 2.4.7 python-dateutil 2.8.1 PyYAML 5.3.1 requests 2.24.0 shellescape 3.8.1 six 1.15.0 urllib3 1.25.11 this is my installed list
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
not sure w/o the actual error. But what would be great is to setup a virtual env to test it separately :below is on a linux or mac but google it if different for you: pip3 install virtualenv mkdir test_pycrypto cd test_pycrypto virtualenv test_py source test_py/bin/activate run pip3 to install pycrypto in the separate env to see if it works there.
@maxxhiaro2470
@maxxhiaro2470 3 жыл бұрын
from Crypto.Cipher import AES File "C:\Users\Lol\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\Crypto\__init__.py", line 35, in from StringIO import StringIO ModuleNotFoundError: No module named 'StringIO' ...ok now i can burn my pc
@practicalpythonsolutions-b4478
@practicalpythonsolutions-b4478 3 жыл бұрын
Make sure that you do not have pycryptodome installed . Do a “pip3 list” to confirm
How to use Python File Encryption with a Password Key? (Part 4)
7:42
Practical Python Solutions
Рет қаралды 12 М.
GPG/PGP Free Data Encryption with Python
27:36
Practical Python Solutions
Рет қаралды 26 М.
The Joker saves Harley Quinn from drowning!#joker  #shorts
00:34
Untitled Joker
Рет қаралды 71 МЛН
طردت النملة من المنزل😡 ماذا فعل؟🥲
00:25
Cool Tool SHORTS Arabic
Рет қаралды 16 МЛН
Data Encryption with Pycryptodome & AES
10:11
Practical Python Solutions
Рет қаралды 19 М.
AES Explained (Advanced Encryption Standard) - Computerphile
14:14
Computerphile
Рет қаралды 1,2 МЛН
Python3 Advanced Tutorial 10 - PyCrypto
20:31
DrapsTV
Рет қаралды 43 М.
TLS & HTTPS with Python - Digital Certificates
18:21
Practical Python Solutions
Рет қаралды 11 М.
TailsOS Guide For The Ultra Paranoid
35:32
Mental Outlaw
Рет қаралды 421 М.
Encryption program in Python 🔐
8:41
Bro Code
Рет қаралды 115 М.
Tmux has forever changed the way I write code.
13:30
Dreams of Code
Рет қаралды 968 М.
RSA Private & Public Key Encryption in Python
12:42
NeuralNine
Рет қаралды 52 М.
AES Encryption In Python
12:18
BasselTech
Рет қаралды 38 М.