Python Encryption Tutorial with PyCrypto
Skills:
AI Productivity Tools60%
Key Takeaways
Encrypts and decrypts sensitive information using PyCrypto in Python
Full Transcript
hello and welcome to my tutorial on encryption with python someone made this uh request for this video so I went ahead and made it uh it's going to be a little bit longer than most of the videos by a little bit I mean a lot of it um simply because there's a lot of information to pass on with this um and I can't really make it in chunks because it's kind of needs to be all together so um if you don't like long tutorials I'm sorry encryption isn't going to be for you anyway so to start we're going to need some sort of encryption software because you're not going to be able to encrypt yourself it's pretty complex stuff so you'll want to get at least some sort of import uh for encrypting so you'll want to go this is the website for pi crypto which is probably the most popular encryption tool with python um I would just download the latest version you just click here and you'll download the tarball um if you don't have um I'm not really talk too much about this if you don't have anything to extract the tarball with you can use program called seven zip it's like 7-Zip or something like that and that'll get the files out and if you don't know how to actually open up the or install the file it's usually pretty easy uh let me open up a command prompt hello I'm going to go ahead and re-record this I just noticed in the video that my command prompt is probably one of the worst colors possible for seeing what's actually being said so we'll just change it here so when you open up the command prompt now again this is a command prompt not the python um interpreter um let's say you've got the file on your desktop you'll use CD for change directory and you'll choose desktop like it it's basically change directory into something that you're already in but you can type out like the full directory too if you want but anyway that's just uh command prompt lingo so now that you're on desktop um I've got p cryp crypto on my desktop so I'm going to say CD into Pi crypto I've got like it's like a file folder basically for p crypto uh what did I do wrong oh we didn't have the Y there Pi Pi crypto there we go and then so once you're in the P crypto uh folder uh you you should see like a bunch of stuff in that folder like this is the folder here You' got build blah all this stuff and if you scroll down here's a setup . py that's the file that will basically set up um whatever install that you want to do and so the way that you do that is you just you type in Python setup setup.py and then install now I've already installed it myself so I'm not going to do it but then you would just hit enter here and that will uh install P crypto and really other any other import that has uh setup.py in it once you've got it installed and downloaded from here um generally you're going to well first I'll teach you how to do it but most you know if you're encrypting something it's because you're transferring it or maybe storing it for yourself or something but encryption and decryption kind of go together um and so I'm going to have to show you them both and what I'm going to do is just kind of split them up and I'll have an encryption file over here and then a decryption file over here that way you can kind of understand what's going on so to start off uh once you have uh P crypto uh set up properly you'll need to do this import you'll do from P or from crypto do Cipher import AES now there are various forms of encryption AES is just kind of one of the many forms um and generally um there's two different types of um I guess encryption processes there's symmetric uh key algorithms and this is where you have the same key for encryption and decryption which is what AES has um and then there's also asymmetric key algorithms where you have one key for encrypting and then one key for decrypting uh generally the more accepted version of encryption is indeed AES and you want 128 um bit encryption just for the record we we'll talk about that that in just a moment as we get to it but yeah AES is probably the most popular form of encryption anyways um so that's what we're going to use uh the next import that you're going to need is base 64 and what Bas 64 is used for is actually encoding and you don't want to get that confused with encryption they're two very different things um basically encoding is for putting data in a specific format um so it can be recognized by it's kind of like you know you convert something to a string or something like it needs to be in a specific format to be read by something that's what B 64 is doing it's encoding it kind of like encoding a URL so it can be recognized it's it's the same stuff um so you'll need base 64 for encoding and then um the third and final import that we're going to need is import OS and the reason for OS and what we want an OS for is for the function called Ur random um which is kind of an accepted producer of Randomness that's suitable for cryptology um simple random isn't really as is accepted I mean you could use it but people prefer you random over that so moving right along now we're going to Define our encryption function so we'll say encryption and then within here we're going to do uh we'll just say our variable or parameter rather is going to be private info and uh so the first thing that we want to um specify is block size oops block size and basically you can choose between 16 24 and 32 for AES and that's going to corresponds to bits like these are bytes and then it corresponds to bits so um you could do 32 bytes for 256bit encryption or you can do 16 bit or 16 bytes for 128 bits and so on so we're going to do 16 bit um or 16 bytes for 128bit encryption which is the accepted um just as an aside if you're if you're trying to learn how to do encryption because you accept maybe like people's credit card information online or something like that you're kind of like obligated to encrypt their data and the basic uh I'm trying to think like the widely accept the one that the US government backs is 128bit AES encryption so um that's what I suggest you use if you're liable for any of the information that you're storing but also the reason they support it is because it's good um just another uh side basically a you can go as low as like a 32bit encryption and um basically with today's computational power a decent computer would have that key just by Brute Force techniques only they could have that key cracked in probably like less than an hour so uh 32-bit keys are not very protective at all um a 64-bit key is getting closer uh it would take probably like a few months with a decent computer and that's only if you know for if you ran through all 100% of the combinations like if you if you don't have um like it's highly highly likely that you're not going to run through 100% of the combinations before you find the correct one um but anyway with 128bit this considered pretty safe at the moment just because it would just take like centuries um with a bunch of computers to crack it uh but obviously over time as processors get better and better you're going to need better encryption so anyways moving right along after block size the next thing you have to Define is padding um this is the only thing that really confuses me it's like some sort of algorithm that you can use but you can put like anything in here like you can put like 99 all this kind of stuff like apparently like this open curly braces is like some sort of algorithm um if you understand that let me know I don't really understand it myself I just know that that's what people do so that's what I'm doing um and I guess the reason you use a padding at all is so it's for sure like whatever your um the values that you encrypt are a multiple of the block size and length so this is just like use some sort of padding to ensure that that is the case so it can actually be like understood and decrypted later on um so now the next thing is um if I just pad and to do this we're going to call Lambda and our parameter for Lambda will just be C or I'm sorry uh Lambda our parameter will be S and then we're going to say S Plus um block size minus the length of s and then block size block size times padding so what's happening here is we're act we're like um we're actually like going to P this is the process to actually pad the data with our padding up here um just as a quick aside let me show you what Lambda is since if you're not familiar with Lambda U you might not understand how this works um so I've just brought up a command prompt here um oops actually let's close that out I just need to have python up so if you're not familiar with Lambda um let me just show you what how Lambda really works it's kind of like a a really really quick way to embed a function in a function and it's like a oneliner function it's just it's just really easy to do it um so or really like a function and a variable anyway um so just as a quick example let's say we want to Define our variable as addition equals and then Lambda and this is basically like Lambda is like notifying python that we're about to do this really weird thing um and then kind of like a function you have to Define what your parameters are but there's no parentheses so you do like Lambda X and then just like at the end of a function you have to have a colon and then um you specify what you how you want that function to work so we're just going to say x + 5 okay so hit enter and now that's been defined so now what you can do is you can say addition and uh we'll do three so to should give us eight and sure enough it gave us eight and that's all thanks to this Lambda now the reason why it's actually used in encryption is it kind of it's it's used for abstraction of functions um so that's really the reason why you're using it within encryp with with encryption basically so moving right along now um we're going to Define how we want this to be encoded and so you're going to just we'll call this in code AS equals and then we'll hit hit up Lambda again C and then s um basically to correspond here um we want to call Base 64 and again this is base 64 is for encoding b64 I want to say encode and then uh so that's what you're actually going going to encode and then you want to encode whatever you know the C variable is uh or C parameter rather C do encrypt um then we want to encrypt the padded version of the this s uh variable basically or S parameter and uh close that again and one more time and I want to say okay yeah so we're done there now what we need to do is every basically every encryption has a key and so now we need to generate this key so we're going to call the key uh secret and that's going to be equals OS Ur random which is why we imported OS up here Ur random and then you have um block size here block size and then just so we can actually see it we'll want to say you know print encryption key and secret and now what we need to do is establish a cipher object basically um so to do that you're going to need Cipher equals as. and then secret we've add a space here and then finally we encode whatever our string is going to be so encoded we'll call it equals encode AES which is talking about here and now um our two variables because our encode AES is Lambda C comma s so there's two um parameters that were given here so now you have to specify the two parameters that you want so you've got Cipher and then private info oops and as you can see what what it's going to do now is it's going to do Cipher it's going to encrypt the cipher and then use or basically it's going to take the cipher encrypt with the padded version of s is the basic easiest way I can say that which wasn't very easy and then finally you can print out encrypted string comma and then encoded and now we have hopefully explained to you how encryption works and now let's see what it looks like for us and we'll call [Music] encryption encryption and then inside here is where we're going to put a secret message that is very sensitive and we want no one to read it even the government hit enter and as you can see here is our encryption key and then this is the actual encrypted info now just as an aside what you can do later on is you could just say Cipher equals and then um oh we misspelled string that's going to bother me anyway you could say secret I'm sorry not Cipher you could say secret equals so instead of generating a new one this is how you would generate a new key but instead of doing that like say you've already established a key that you like and you've maybe memorized for yourself you would just put this within there and then you're obviously like this is non as CI coding which so you have to have something up there um but I'll show you that in just a second because we're going to need it for the decryption anyways uh okay so um let's run this one more time since something went arai there actually let me close it so we don't get confused so again uh encryption private information you do not want the government to see close it okay and then here's our new encryption key cuz again we generated a new one but again you can't just generate new encryption Keys every time you need to generate just one if you're sending the data across somewhere you're going to need just one encryption key and so like both sources are going to need to know it um so anyway now over in decryption how do we actually decrypt this stuff now that we have the key anyways so same orts as before I'm just going to copy and paste them over and now we need to Define decryption and then within the parentheses we'll just say our parameter is encrypted string colon um padding we're going to say the exact same padding pretty sure have to keep that the same um now we Define decode decode a s and then we have our Lambda function Lambda C comma e and we're going to do c. decrypt using Bas 64. b64 decode because we're going to have to decode the end code of this information and then obviously the variable in there is going to be e now R strip and we're going to be stripping away the padding that we added before so I'm padding um now our encryption um not sure why I've done this encrypted string let's see I guess what I would want to do is just we'll say the key equals and we'll Define our key now which is this and once we run it it's going to tell me I've got some funky coding and what to put up here I don't really know what to put up there just yet so we'll wait till we get to that point um cuz this is clearly non asci characters um so the next thing we want is cipher equals as. new and we're going to want to use the key that we had before for the cipher and uh decoded then is going to equal decode as so obviously we're referencing up here um Cipher using that Cipher that we've just said and then we want to use it on the encrypted string close and then finally print decoded so hopefully we've not spelled anything wrong yeah sure okay so we need to add this to the top of the file hit okay but if you also just hit edit my file it'll go right up there for you so we'll use that save it we'll run it we'll bring it over here and then what we'll do we'll take this information here and we'll say decryption now we've already put this key into to here because this the python shell isn't going to automat like you would have to have this coding in there beforehand but what you could do is instead of saving the key in here since we already know it's going to be CP 1252 I'll show you guys in a second that you could take key out and then put it in this parameters here um because obviously you're not going to want the key to be in your script like you don't want to save the key anywhere so anyway um I guess you could have the key in there but generally you probably wouldn't want to if you wanted it to be super safe um you would probably want to be pulling the key from another file that possibly encrypted itself um what was I doing oh encrypted string so we just need to slap this misspelled encrypted string right on in there hit enter and sure enough it decrypts it into private information that you do not want the government to see and that's really it that's all you have to do to do encryption um so anyway I hope you guys enjoyed hope you maybe learned something and um until next time
Original Description
Sentdex.com
Facebook.com/sentdex
Twitter.com/sentdex
How to use python to encrypt sensitive information, and later decrypt it, using PyCrypto!
PyCrypto: https://www.dlitz.net/software/pycrypto/
The Code: http://sentdex.com/sentiment-analysisbig-data-and-python-tutorials/encryption-and-decryption-in-python-code-example-with-explanation/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 2 of 60
1
▶
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: AI Productivity Tools
View skill →Related Reads
📰
📰
📰
📰
This Is Why 99% Of AI Ads, Posts and Articles Are Terrible (It’s Not Your Prompt)
Medium · Startup
10 AI Tools That Save Me Hours Every Week
Medium · ChatGPT
Math Homework: Step-by-Step Solver & Scanner
Medium · AI
Passports, Playbooks, and Platforms: How Emerging Tech Is Changing Travel and Sports
Medium · AI
🎓
Tutor Explanation
DeepCamp AI