Python Implementation Method for RC4 file encryption,

Source: Internet
Author: User
Tags crc32

Python Implementation Method for RC4 file encryption,

This article describes how to implement RC4 file encryption in python. Share it with you for your reference. The specific analysis is as follows:

Based on the RC4 stream encryption algorithm, the extended 16*16 S box and 32-byte key are used.
At present, it should be safer.
 
I just learned python and it's hard to call it.
In addition, it has been implemented in both VC and python. The two platforms can encrypt and decrypt each other, which is very rewarding.
 
The following is the implementation in python3.0 and needs to be slightly modified under 2.x.

# For python 3.0 # from Libo import struct, sys, OS, binascii "RC4 encryption algorithm 16x16 S box encryption unit: short" def RC4 (pkey, keylen, pin, dlen): N = 65536 S = list (range (N) j = 0 for I in range (N ): j = (j + S [I] + pkey [I % keylen]) % N temp = S [I] S [I] = S [j] S [j] = temp I = j = 0 pout = B ''for x in range (dlen): I = I + 1 j = (j + S [I]) % N temp = S [I] S [I] = S [j] S [j] = temp pout + = struct. pack ('h', pin [x] ^ S [(S [I] + S [j]) % N]) return (p Out) # bytes-> includef Coding (data): if (len (data) % 2): data + = B '\ 0' dlen = len (data) // 2 return (struct. unpack (str (dlen) + 'h', data) # short-> bytesdef unCoding (data): d = B ''for I in range (len (data )): d + = struct. pack ('h', data [I]) return (d) # generate a 32-byte key def CreatKey (Keyt): pl = len (Keyt) key = B ''r = 0 for I in range (32): k = (Keyt [r % pl] + I) % 256 Key + = struct. pack ('B', k) r + = 1 return Key # update Key def UpdataKey (Keyt): Key = UnCoding (Keyt) # Shift Key = Key Left Loop [1:] + struct. pack ('B', Key [0]) tem = 0 # sum for I in range (len (Key): tem + = Key [I]; keyo = B ''# Xor for I in range (len (Key): Keyo + = struct. pack ('B', (Key [I] ^ tem) % 256) tem + = Keyo [I]> 3 tem = tem % 256 return (Coding (Keyo )) if _ name _ = '_ main _': # obtain the input file if len (sys. argv) = 1: filename = input ('source file: ') else: filename = sys. argv [1] try: fin = open (filename, 'rb') before T: p Rint ('failed to open the file! ') Input () sys. exit () print (filename) # Open the output file if filename [-4:] = '. RC4 ': eID = 1 key = input ('input decryption key :'). encode () ofilename = filename [:-4] else: eID = 2 key = input ('input encryption key :'). encode () ofilename = filename + '. RC4 'key = Coding (CreatKey (key) key = UpdataKey (key) # process duplicate names while OS. path. exists (ofilename): ofilename = OS. path. dirname (ofilename) + '\ replica' + OS. path. basename (ofilename) fout = open (ofilename, 'wb') Print (ofilename) # decrypt if eID = 1: # Read File length filelen = struct. unpack ('I', fin. read (4) [0] print ('flielen = ', filelen,' \ n ...... ') while 1: # Read block size ps = fin. read (2) if not ps: # End of the file break packsize = struct. unpack ('h', ps) [0] # Read data dd = fin. read (packsize) # decrypt dd = Coding (dd) x = RC4 (key, len (key), dd, len (dd) key = UpdataKey (key) # crc = struct. unpack ('I', fin. read (4) [0] if binascii. crc32 (x )! = Crc: print ('crc32 verification error! ', Crc, binascii. crc32 (x) input () sys. exit () fout. write (x) # Fill the fout at the end of the cropping. truncate (filelen) # encrypted elif eID = 2: # Get the file length fin. seek (0, 2) filelen = fin. tell () print ('flielen = ', filelen,' \ n ...... ') fin. seek (0, 0) fout. write (struct. pack ('I', filelen) while 1: # Read data dd = fin. read (65534) if not dd: # End of the file break # End filling srl = len (dd) if srl % 2: srl + = 1; dd + = B '\ 0' # crc = struct. pack ('I', binascii. crc32 (dd) # encrypt data dd = Co Ding (dd) x = RC4 (key, len (key), dd, len (dd) key = UpdataKey (key) # Write to the file fout. write (struct. pack ('h', srl) fout. write (x) fout. write (crc) fin. close () fout. close () print ('OK! ') Input ()

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.