Batch Programming: Lesson 8 (Advanced encryption/decryption script)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey technology crazy here and welcome to episode 8 of the batch Series so please sit back and [Music] enjoy okay so today's lesson is actually going to be the last lesson at least for now and the reason behind that is once you guys learn what you guys are going to learn in this lesson you guys are going to be set on batch I'm running over things to teach you and to tell you the truth batch can only go so far so it's a great entry program programming language and that was the idea of this series is to get people programming and it it's excellent entry programming language but it's uh very limited in the sense that uh it seems like a lot of the the parts of match were like last minute thoughts um so there's lots of exceptions so this is how you do it except in this case in which you do it this way and then and except in this case you do it with this way and it's just not very consistent it's very hard to use it's not very logical and stuff like that when you get more and more advanced now with other programming languages it's more standard and it's a lot more logical and they're based off the ideas that we've learned in these lessons so uh it's fairly easy to learn a different language once you've learned batch they are a little bit different so we'll take a little bit of learning but uh hopefully you guys will get into that anyways what are we going to be doing today well instead of teaching one concept we're going to be making a script and in that script we'll be pulling together all of the ideas we've made in these uh lessons so far at least most of them and we're going be making this really cool encryptor and de encryptor uh batch files and the way it works is much more advanced than other videos you'll find off YouTube where uh all the ones I could find were just like uh AAL C Cal G gals H like simple encryption sort of things that you you did in elementary school today we're actually going to be doing much more advanced uh version of that we're going to be assigning a letter or a number to every letter and then we're going to be taking that and multiplying it by a code that we put in and what we we we don't need to just multiply we could actually do anything and we could do multiply by that code divide by 6.2 and so on and so we we can make this thing really Advanced and we'll get to that in a second but let me just without further Ado let me just show you what we are going to be doing so I type in a code like this could be any number we're going to try 1 two three and this is a test gives you this encrypted uh numbers here and now we can open up our D encryptor and type in our code 1 2 3 text and it'll say this is a test and if if we don't have our code correct it won't actually work okay so we're going to start with set local space enable delayed expansion now this is probably one of the most complicated subjects and Concepts that we're going to learn in today's lesson and it's the idea of expanding variables right before we need them rather than right before the command now I know that probably still doesn't make any sense to you but I have examples to show you uh so over here we have a simple thing that says set message to equal hello and then Echo hello and then pause uh now the way the batch actually works is it actually checks for variables before it executes the line of command or code and then it injects those variables in the command before it runs it so uh here it's saying set message to equal hello and then over here it's like oh there's a variable in this line I will look that up and it looks it up and it says message equals hello it puts hello in there and then it executes the line of code and in most cases that's perfectly fine and in that case it does exactly what we want it to it says says hello just like that now in some cases it's not so friendly and this is where batch gets confusing compared to other languages so over here we have a if statement just like we learned in our last video and as we learned if statement is one command so this whole thing here is actually one command it's all it's on multiple lines we can stick it on one line if we really wanted to but it's all one command so the way the batch works is it goes through here it says set status to equal problem and then over here it says if statement and says oh there's a variable in there I will check the variable and checks the variable ensure enough status equals problem and it uses that variable throughout the whole if statement so when it gets to over here it says Echo status it's actually going to say Echo problem and even though we just set status to equal no problem now if we run this says problem now uh so now within this if statement it set status to no problem so if we were to go outside of the loop or the if statement I expect it to say no problem just like that uh but if we're working with the variable inside of it it does not have the most updated variable it just uses uh the variable outside of the command now we're going to be working with lots of Loops today so we have to work around this and a simple way of getting around this is or enable delayed expansion and let me just show you right here uh for example let's get rid of this one over here this is how we use it so we have set enabled delayed expansion over here we can actually get rid of this cuz we already have it up there uh set status to equal problem and then over here we have problem equals problem and then inside we have status equals no problem now look at this instead of using two percent signs we're going to use two uh exclamation marks and that's saying use the enabled delayed expansion variable so it uses the most updated variable inside a loop not the one set outside of a loop and this is important because what we're going to be doing today is we're going to be setting some text outside of a loop so we're going to be setting the like text is like this is a test and then we're going to stick it inside of Loop and it's going to go through through every single letter and uh convert it into a word but the thing is we don't want it to uh refer to the text outside of loop we want to have it updated inside the loop so if that doesn't make any sense I have a website in the description that will explain it very well and it uses the exact same examples that I showed in this video and it goes more in depth but I didn't want to spend 10 minutes explaining this one concept so let's move on okay so let's start off by asking the user for some input so let's store this in the variable code and it will make it equal text so so it'll ask for text and stort in the variable code the other variable we're going to set is uh the character variable so CH h a RS and uh you can make that whatever you want but for this example we're going to use that and we're going to make this equal the alphabet so in our example we're going to do a from C but technically you could uh uh adjust the coding so you could accept any other characters like numbers and um all that type of stuff but we're just going to encode all the letters from A to Z everything else we'll leave uh and we're going to start with zero and the reason why is because we're going to make a loop that Loops through 27 times CU there's 27 characters in here and we want zero to line up with zero a to line up with one B to line up with two if we didn't have a zero here uh a would line up with um nothing zero and then B would line up with one and so on and so on but we don't want that so uh we're just going to add a zero there it's a temporary fix there's other ways of doing it but that's the way I'm going to do it today now now we're going to make a loop that Loops through 27 times and the way we do that is four and then forward slash oops not not question mark forward slash L and that's for um going through uh it's like Loop it stands for Loop I'm pretty sure I'm 99% sure anyways and then we have a variable here so 2% signs n which is different than what we dealt with before we usually have a variable or our text inside two present signs this is a different type of variable and I'll go over that in a second uh and then in Brackets we're have three different numbers in here the very first number is our starting number which is going to be one our second number will be our incrementing number so every single time it goes around uh the loop it will increment by this number so we're going to have that as one and then our final number will be 27 actually we only need 26 uh because there's 26 characters in the alphabet uh and then we're going to do do percent um open and close just like that so now if we Echo percent percent sign n this is actually stands for the character uh or the number that it's currently on in the loop so the very first time going around it'll Echo one and then Echo 2 and so on and so on so now if we actually try this it's going to Echo 1 through 26 just like that so it's going to assign the letter A to one and B to whatever whatever uh so now inside this Loop what we're going to be doing is making another loop that Cycles through all the letters in in the the variable characters so the way we do that is four and instead of doing um L we do forward slash uh we do f f is for cycling through a string uh sometimes you want to cycle through a string to find characters or uh to delete certain characters in our case we're just trying to find characters and replace characters so uh we do for slf we make another variable and we'll call this one C for characters and then in uh this character right uh this thing here now we have to do something a little bit special um if we were to have this equal characters just like this it's not actually going to cycle through each character uh it's just going to make percent percent sign c equal from here to here uh so it's just using this this string and it's putting it in this Loop um which is not going to help us at all we want this to actually equal the very first character and then we want to equal the second character and so on and so on and the reason why we want to do that is because inside here we're going to make all uh C's equal NS so all the A's will equal ones and all the B's will equal twos and and so on and so on and the way we do this is I'm just going to copy this code here and then explain it just like this okay so what we're doing is we're opening up the character very uh character uh and the way we open it up is we have a coal in there and then we're asking for a certain character and and the very first time it goes around it will ask for one one which is character one of line one and then character two of line one and then so on and so on uh but this can be represented by the percent percent sign n because every single time it goes around it uh it keeps that number in percent sign percent sign n uh and then we're going to have do Open Bracket close bracket so what this is going to do is it's going to cycle from here to here 26 times and then inside here it's going to cycle through uh character one and then character two and character three and character four so and it will store it in the uh variable percent percent sign C so now if we Echo um let's do character see uh percent percent sign C equals percent sign percent sign n what it's going to do is it's going to uh just make like this little mini thing here it's going to say AAL 1 b equal 2 so on and so on all the way to X or Z whatever you want if you're American all those Americans out there uh anyways now what should we do next now we're going to open up our code and search for all those characters and replace them uh so the way we're going to be doing this here is we're going to let me just copy and paste this in so I can explain it here uh we're going to be set our variable code which is what we asked for and then we're going to open up that code just like we did over here and expand it but instead of doing comma one we're going to do equal sign so just for example here let's just do a equals uh 1 so what it's going to do is it's going to take up our variable code open it up look for all the A's and replace it with one and it's going to do this 26 times um so if if we run this here and we type in some text let's just do like and so it has an A in there it's going to oh uh we forgot to Echo our code at the very end exclamation mark U and code and and you can see all the A's were all equal ones now it's going to go through 26 times and every single time we want it to change so the next time we go into round we want this to equal b equals 2 next time C equals 3 and so on and so on and since we have these variables here that represent those numbers all we have to do is do percent sign percent sign C equals percent sign percent sign n so the first time going around it will look for all the ones and or look for all the A's and make them into ones and then next time it'll make all the the the twos go into um and so on and so on anyways moving on let's just try it let's try it here uh so if you do test you can see that it gave us a number here and you can see on each side there's 20s and that's because there's two T's so I'm guessing if we type in t it will equal 20 and that that sounds about right okay so let's make our D encryptor now let's just copy all of our text from our encryptor go to our decryptor now we're just going to run everything backwards so the way we're going to do this is uh we've changed all the characters into numbers all we have to do is numbers back into characters and right now this is not very secure it's just lining up numbers but we're going to actually take those numbers and and do some stuff to it so uh first off let's just see if this works um we run our encryptor a equals 1 we go to our D encryptor we do one and it equals oops I didn't do that uh one 1 equals a now there is a big problem with this uh type of our our idea of doing it now if I do testing you're going to notice it did a whole bunch of letter uh numbers here now if we copy it go to De encryptor and paste it it's going to come out really weird and the reason why is because um these numbers are actually not separated 14 could be 1 4 or 14 uh and and that's caused when there's uh when there's uh when the numbers are more than or less than one character so from one to nine uh all of the letters like a will be one B will be C two b b two and then C will be three um some of the them are lined up with one character uh numbers now the easy solution is just to start counting at 10 so if we start at 10 and uh then we have to add uh one one two three four five six seven 8 9 10 so it's going to start counting on the 10th character and make so a will equal 10 B will equal 11 c will equal 12 and so on and so on this is the easy way of doing it I'm sure there's other ways of doing it that's just my way of doing it so now if we do this now if we take this and copy it go to our ID encryptor paste it and we switch these around now that everything is to two character number uh we should be able to oh yeah we forgot one more thing uh since it's going from 10 and starting at 10 it will no longer end at 26 it will end at 36 just like that because now there is 36 characters in here so it's going to start at 10 go from here and stop at here uh so now if we run this we'll do testing and now there's everything is actually um two characters if we paste this it goes back to testing and if we just just for this example here let me just put a dash uh so C equals dashn and what this will do is it will separate all of our numbers together so now you can see every single one of our letters stands for a two character number now um and this will help uh now we're also going to run into other problems um if we don't have this Dash here like this and I copy from A to Z and I put this in here it's going to give us a large number twice as large technically if you go to ID encryptor we going to run into another problem here you can see that it did ABCD F G H JK but sometimes some of these letters for example uh here we have 12 and 22 there it's three numbers which one is it it could be 12 or 22 and our thing is not going to take that very well so the easy fix is is just to put a dash between each number just like that um so if we put a dash there and a Dash there now when we run our encryption everything is two letters and everything is separated so when it goes through and it searches for 14 it knows that it's 14 it won't think that it's one or if it's two or something like that it will only look for these these these numbers right here um so now if we take this and we take it and put it into our decompiler thing you can see testing and it put a uh Dash there and that reminds me we actually don't need a dash here okay so now that we have a two-digit number separated by a dash Associated to each letter in the alphabet we can actually manipulate that number and multiply it by our secret code that we're going to input and the way our de encryptor is going to work is if you don't have that code correct it won't actually reverse engineer correctly and you won't get your text uh and you'll get it as we go along here so let's just ask for a code here and we'll uh set this as uh input uh code and then here we'll ask for code just like that oops I did it's wrong there equal sign uh so it's going to ask for code put it into the variable input code and now we're going to be doing some math now I don't know if you guys remember but in I think it was the second episode or something like that uh we learned how to do math and the way you do it is you do SL uh set and then forward slash a and then you want to do assign it a variable so we'll assign it math and we'll make it equal percent sign percent sign n times our our input code so percent sign input code just like that so it's going to take our number that we're on and multiply it by our input code now this theoretically could be anything and we'll be we'll make it a little bit harder you could have this any equation that you want you can can have this multiply by 7.6 / 4 * 17 multiply by your input code uh you could you can make this whatever you want as long as it's exact same as the de encryptor uh it will work perfectly backwards so um now that we have that set we're going to be making a loop to inject this math variable in place of this n now I don't know perfectly how to do this so I made up my own way uh you can't just take a variable like this and put it in inside of here because it has to be a temporary variable and the easiest way I could I found out how to do it is just do another loop uh so we just did another loop uh slf and then we assigned it like let's say F uh and then in and then we're going to um put our math variable inside of there so just like this exclamation mark math exclamation mark quotation mark close and then do the following command oops like that and we're move this inside of here uh so what we're doing is remember it's looping through here 26 times I believe and then it's going through each character here 26 times and then inside here it's going to make another loop and use the variable math and math equals n * input code and then it says uh so every single time it goes around math um percent s percent sign F will equal math uh so we can simply just change that to F and that's the one way I figured out there might be another way I'm not sure but now if we try this and we have our input code let's try 12 and text let's make this test you can see it actually multiplied each number by 12 now if we take this whole thing and we copy it make put it into our D encryptor and we change F and C now if we run this we'll do test oh oops I have to type in a code let's do 1 two 3 4 or 1 two three and do test it will give us this number here we can now take this number go into our d encryptor 1 two 3 and then paste and it goes back to test now it has these dashes in there and we'll get rid of that right now um one thing I should mention is when you're past or copying and pasting in in CMD I'm not sure if I went over this but you highlight it and you press enter so you right click and you press Mark you highlight and press enter and that will copy it you cannot press contrl C uh just to put that out there so to get rid of all those dashes let's do it okay so first off we want to remove the dash from our de encryptor because when we change it back from a number to a letter we don't need to add another Dash they're already separated by one dash uh and now what we're going to be doing is outside of all of our Loops uh before we Echo our code back we're going to make another loop that goes through our code and finds all of our um all of of our dashes and changes it into nothing it uses the exact same thing or the concept as we're using right here we're making all the fs equal C's we're going to make all the dashes equal nothing and uh the way we're going to be doing it let me just copy and paste it here because I don't want to type the whole function in again um but here we have another loop we have F in code so it's using the variable code and then it's going to it what it's doing is it's setting uh it's opening up code and then looking for dashes and making it equal nothing and then what this will do is now if I Echo code uh let's try code 123 test now if we copy all of this open up our D encryptor and one two 3 just like that it'll do test and it removes all of the dashes now we're going to take it one steps further by saying uh you can take a text file and drag it onto our encryptor it will output to encrypted text file then you can drag that encrypted text file onto our D encryptor and it will make a de encrypted text file and the way we're going to be doing this is uh very similar to all of the other lessons that we've learned uh we can take our text and instead of asking for text now we're going to make it equal input.txt now this time every single time it runs it will look for an input.txt and then um we have to do an equals or greater than equal sign so it's taking all of the input and squeezing it into the variable code now alternatively if we don't want to have always have the input like have it set as input.txt we can have it as percent sign one and as you guys learned last time what this will do is percent sign one is the full file path of whatever you drag onto the batch file so if we drag this onto the batch file uh percent sign one will equal properties it'll equal uh users Justin desktop and then input.txt so it's just it's an easy way of doing it uh we can do the same thing on our d encryptor uh so we'll take whatever we drag on to it and make it equal code and then over here uh we can go to our encryptor instead of echoing our code we can actually Echo our code and squeeze it into a encrypted txt just like that and we take all of our de encrypted and squeeze it into a d encrypted decrypted t XT just like that so now let's just save both those now if we take open up our input let's make this like this is a test exclamation mark let's add an exclamation mark see what happens uh now if we drag this into encrypted it will ask for a code let's do one two three and it's done now if we open up our encrypted here's all of our encrypted text we can take this and drag it onto our d encryptor 1 two 3 it's done and now it's says this is a test now just to confirm see if we don't type in our number correctly uh let's drag our encrypted onto here and let's do like like that so let's see what happens our de encrypted txt as you can see is gibberish you cannot read it because we did not type in our proper code so I guess that concludes this video this was a very very long video if you got to the end and you probably have a big headache now so I suggest watching a few times if you guys don't understand it feel free to put it in the comments below and if you do understand it please help the other people I'm not always able to respond to every comment and you guys helping each other is always the best ever so anyways I guess that concludes this video please stay tuned to next week I have a special surprise coming up but I I can't tell you yet but uh stay tuned and congrats to getting to the end of the video cuz I know that was a very long video anyways I guess that concludes this video please comment rate subscribe if you enjoyed these tutorials please leave comments below and suggestions for future videos I'm open to anything anyways that's all from technology crazy goodbye
Info
Channel: Technologycrazy
Views: 135,655
Rating: undefined out of 5
Keywords: cool batch script, encryption, decryption, script, how to, cool script, batch, lesson 8, loop, loops, for /L, for /F, hacking, advanced
Id: c8HIe3bKSfA
Channel Id: undefined
Length: 25min 4sec (1504 seconds)
Published: Sun Oct 20 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.