Because the discovery of the basic tutorial I recommended before the site has been very perfect, do not repeat the writing, so Ben Wang Lai to do exercises together.
First, the principle of encryption
I remember when I learned C + + when I learned the input and output stream, when Mr. Wang taught us to write a small encryption program, so this time since the study of Python This small exercise of course not let go (in fact, this encryption program I used C++,java have written can be said to be relatively ripe). The principle of encryption is to iterate through each byte of the file, and then the corresponding encryption operation is encrypted, the decryption time is inverse is the decryption operation.
For example, we read the first byte of the file data is 20 (read every bit of data is 0~255, because 8 bits is 1 bytes, so the binary 00000000~11111111 representation of the unsigned number range is 0~255), encryption we can choose to add 1 on this basis, Then the 1th byte after the encryption is 21, if we add all bytes 1, then the equivalent of the file is encrypted (note that if it is 255 and 1 will be out of bounds, so the boundary value needs special processing, such as 256, we write directly 0). Decryption time we each byte minus 1 (of course, the boundary value needs to handle itself, such as the previous encryption of 0, we directly write 255). Of course this decryption is not very easy to crack that, so we can define a string to encrypt, such as "123456", we encrypt the 1th byte with 1, the 2nd byte and 2 to encrypt, to the 7th we again with the 1 encryption, until all the bytes are looped with this string to complete the encryption, Does this increase the difficulty of the crack that?
Of course, I'm using an XOR or encryption algorithm, and one of the most magical places is to exchange the values of 2 variables without any variables. Like what
a=10b=20a^=bb^=aa^=bprint(a)print( b)# output #Ten #
is not very magical, so encryption using the XOR can also be very convenient to encrypt and restore
Second, the program effect demonstration
1. Create a TXT file (of course, the picture, sound, all format files can be), here convenient demo I choose a txt file
2. Run a well-written Python script
The encrypted file is nothing to see.
3. Decrypt (we enter the wrong password once, of course, the higher the password approximation, the more content will be restored, of course, if it is a picture or sound then you can not play the wrong one)
Third, the Wang wrote the encryption source (if there is insufficient please correct)
1 #Author: Smallfoxdog Little fox Dog2 #Date: March 20, 2018 13:57:043 #function: Encryption and decryption of files4 5 ImportOS6 7 #Main function8 defMain ():9 GetInput ()Ten One #Input Parameters A defgetinput (): - - #get parameters for an operation the while(True): - -Oper = input ("Please enter the action (e: Encrypt D: Decrypt):") - + if(oper=="e" oroper=="D"): - Break + Else: A Print("input wrong, please re-enter!") at - #get the file password - while(True): - -password= input ("Please enter your password:") - in if(len (password) = =0): - Print("The password cannot be empty!") to Else: + Break - the #gets the file path of the operation * while(True): $ Panax NotoginsengPath=input ("Please enter the file path:") - the Try: +F_read = open (path,"RB") A except: the Print("file not found, please check if the path exists! ") + Else: - Break $ $ #for encryption or decryption operations - if(oper=="e"): - Encrypt (Path,password) the elif(oper=="D"): - Decrypt (path, password)Wuyi the #Encrypt - defEncrypt (Path,password): Wu - #because there may be libraries that can get this information directly, but it's not difficult to write an algorithm to get the information. AboutFilefullname = Path.split (OS.PATH.SEP)#Os.path.sep is the operating system's file delimiter $FileName = Filefullname[len (filefullname) -1].split (".") [0] -Filesuffix = Filefullname[len (filefullname) -1].split (".") [1] - - #Print ("File Full name:", Filefullname[len (Filefullname)-1]) A #Print ("File name:", FileName) + #print ("File suffix:", filesuffix) the -fileparent = Path[0:len (path)-len (Filefullname[len (filefullname)-1])] $Newfilename="Encryption _"+filefullname[len (filefullname)-1] thenewfilepath=fileparent+NewFileName the the #Print ("File Parent path:", fileparent) the #print ("New file name:", NewFileName) - #print ("New File Full path:", Newfilepath) in theF_read = open (path,"RB") theF_write = open (Newfilepath,"WB") About theCount=0#Current Password Encrypted index the the #we use XOR or cyclic encryption + forNowinchF_read: - forNowbyteinchNow : theNewbyte=nowbyte^ord (password[count%len (password)])BayiCount+=1 the f_write.write (bytes ([newbyte])) the - f_read.close () - f_write.close () the the Print("Wang ~ File encryption complete ^_^") the the #decryption (because we take the XOR or decryption, so it's actually the same as the encryption algorithm) - defDecrypt (path, password): the #because there may be libraries that can get this information directly, but it's not difficult to write an algorithm to get the information. theFilefullname = Path.split (OS.PATH.SEP)#Os.path.sep is the operating system's file delimiter theFileName = Filefullname[len (filefullname)-1].split (".") [0]94Filesuffix = Filefullname[len (filefullname)-1].split (".") [1] the the #Print ("File Full name:", Filefullname[len (Filefullname)-1]) the #Print ("File name:", FileName)98 #print ("File suffix:", filesuffix) About -fileparent = Path[0:len (path)-Len (Filefullname[len (filefullname)-1])]101NewFileName ="Decryption _"+ Filefullname[len (filefullname)-1]102Newfilepath = Fileparent +NewFileName103 104 #Print ("File Parent path:", fileparent) the #print ("New file name:", NewFileName)106 #print ("New File Full path:", Newfilepath)107 108F_read = open (path,"RB")109F_write = open (Newfilepath,"WB") the 111Count = 0#Current Password Encrypted index the 113 #we use XOR or cyclic encryption the forNowinchF_read: the forNowbyteinchNow : theNewbyte = nowbyte ^ ord (password[count%len (password)])117Count + = 1118 f_write.write (bytes ([newbyte]))119 - f_read.close ()121 f_write.close ()122 123 Print("Wang ~ File decryption complete ^_^")124 theMain ()
Everyone also to write a belongs to own encryption small program bar ^_^. Encryption and decryption can be optimized in a method, before the beginning of the first thought to use the first addition to encrypt, subtract to decrypt, and finally the use of XOR encryption so encryption and decryption is the same, you can optimize the code OH. You can also try it in other ways Oh, welcome to Exchange ^_^
Basic Python tutorial 3--teach you to use Python to do a simple encryption program (also basic what Ah, directly to practice it, with the source code)