Python implementation method of RC4 file encryption

Source: Internet
Author: User
Tags crc32 crc32 checksum unpack
This paper describes the Python implementation method of RC4 file encryption. Share to everyone for your reference. The specific analysis is as follows:

Based on the RC4 stream encryption algorithm, using the Extended 16*16 s box, the 32-byte key.
It should be more secure at the moment.

Just learning Python, it's hard to adjust.
And in the VC and Python under the implementation of each, two platforms can be decrypted with each other, a sense of accomplishment said.

Here is the implementation in python3.0, which requires a little modification at 2.x.

# for Python 3.0# from Li Bo import struct,sys,os,binascii "" "RC4 encryption algorithm 16*16 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 (pout) # Bytes->shortdef Coding (data): if (len (data)%2): data+=b ' Dlen = Len (data)//2 return (Struct.unpack (str (dlen) + ' H ', data)) # Short->bytesdef uncoding (dat A): D=b ' for I in range (len data): D + = Struct.pack (' H ', Data[i]) return (d) #产生32字节密钥def Creatkey (keyt): pl = Len (K EYT) key=b "r=0 for I in range (+): k= (keyt[r%pl]+i)%256 key+= struct.pack (' B ', K) r+=1 return key# update key def up DataKey (KEYT): key = Uncoding (KEYT) #循环左移 key = key[1:] + struct.pack (' B ', key[0]) tem=0 #求和 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 = TE     M% return (Coding (keyo)) if __name__ = = ' __main__ ': #获得输入文件 If Len (sys.argv) ==1:filename = input (' source file: ') Else: filename = sys.argv[1] Try:fin = open (filename, ' RB ') except:print (' Open file failed! ') input () Sys.exit () print (filename) #打开输出文件 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) #处理重名 while Os.path.exists (ofilename): Ofilename = Os.path.d    Irname (ofilename) + ' \ \ Copy ' + os.path.basename (ofilename) fout = open (Ofilename, ' WB ') print (ofilename) #解密 if eid==1:      #读文件长度 FileLen = struct.unpack (' I ', Fin.read (4)) [0] Print (' Flielen = ', FileLen, ' \ n ... ') while 1: #读块大小 Ps= Fin.read (2) if not PS: #文件结束 break PAcksize = Struct.unpack (' H ', PS) [0] #读数据 dd=fin.read (packsize) #解密 dd=coding (dd) x = RC4 (Key,len (k EY), Dd,len (dd)) key = Updatakey (key) #crc CRC = Struct.unpack (' I ', Fin.read (4)) [0] if BINASCII.CRC32 (x) !=crc:print (' CRC32 checksum error! ', CRC,BINASCII.CRC32 (x)) input () Sys.exit () fout.write (x) #裁剪末尾填充位 fout.truncate (FileLen) #加密 E    Lif eid==2: #获得文件长度 fin.seek (0,2) FileLen = Fin.tell () print (' Flielen = ', FileLen, ' \ n ... ') Fin.seek (0,0)        Fout.write (Struct.pack (' I ', FileLen)) while 1: #读数据 dd=fin.read (65534) if not DD: #文件结束        Break #末尾填充 srl = len (dd) if srl%2:srl+=1; Dd+=b ' #crc CRC = Struct.pack (' I ', BINASCII.CRC32 (DD)) #加密数据 dd=coding (dd) x = RC4 (Key,len (key) , Dd,len (dd)) key = Updatakey (key) #写入文件 Fout.write (Struct.pack (' H ', srl)) Fout.write (x) fout.write (CRC) Fin.close () Fout.close () print (' Ok! ') Input ()

Hopefully this article will help you with Python programming.

  • Related Article

    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.