|
#解密 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(key),dd,len(dd))
key = UpdataKey(key)
#crc
crc = struct.unpack('I',fin.read(4))[0]
if binascii.crc32(x)!=crc:
print('CRC32校验错误!',crc,binascii.crc32(x))
input()
sys.exit()
fout.write(x)
#裁剪末尾填充位
fout.truncate(filelen)
#加密
elif 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'\0'
#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() |
|
|