本节内容:
文件分割+AES加密+Base64编码
专题教程:
AES加密解密算法与实现代码
python aes加密解密算法与模块用法教程
1、文件 EncodingSplitData.py
#!/usr/bin/env python
#
from Crypto.Cipher import AES
class mycrypt():
def __init__(self,key):
self.key = key
self.mode = AES.MODE_CBC
def myencrypt(self,text):
cryptor = AES.new(key,self.mode)
length = 16
count = text.count('')
if count < length:
add = (length-count) + 1
text = text + (' ' * add)
elif count > length:
add = (length-(count % length)) + 1
text = text + (' ' * add)
self.ciphertext = cryptor.encrypt(text)
return self.ciphertext
def mydecrypt(self,text):
cryptor = AES.new(key,self.mode)
plain_text = cryptor.decrypt(text)
return plain_text
text = "98789khjsajfilahfpoiwufipoasufipo"
key = "9878*(&^^&)0LLIu(*&^))#$@!KJLKJj"
en = mycrypt(key)
entext = en.myencrypt(text)
print entext
detext = en.mydecrypt(entext).rstrip()
print detext
2、文件 DecodingMergeData.py
#!/usr/bin/env python
#
import sys
import binascii
from os.path import exists
from Crypto.Cipher import AES
class mycrypt():
def __init__(self,key):
self.key = key
self.mode = AES.MODE_CBC
def mydecrypt(self,text):
cryptor = AES.new(key,self.mode)
plain_text = cryptor.decrypt(text)
return plain_text
en = mycrypt("9878*(&^^&)0LLIu(*&^))#$@!KJLKJj")
detext =
filename='D:google.zip.part1'
count = 0
extPos = filename.find('.part')
if extPos > 0:
file = filename[:extPos]
f1 = open(file, "wb")
while True:
count = count + 1
partFile = file +'.part'+ str(count)
if not exists(partFile):
break
else:
f2 = open(partFile, "r")
content = f2.read()
f2.close()
f1.write(en.mydecrypt(content).rstrip())
f1.flush()
f1.close()
else:
print 'File type? Can not combine!'
3、文件 DecodingMergeData.py
#!/usr/bin/env python
#
import sys
import binascii
from os.path import exists
from Crypto.Cipher import AES
class mycrypt():
def __init__(self,key):
self.key = key
self.mode = AES.MODE_CBC
def mydecrypt(self,text):
cryptor = AES.new(key,self.mode)
plain_text = cryptor.decrypt(text)
return plain_text
en = mycrypt("9878*(&^^&)0LLIu(*&^))#$@!KJLKJj")
detext =
filename='D:google.zip.part1'
count = 0
extPos = filename.find('.part')
if extPos > 0:
file = filename[:extPos]
f1 = open(file, "wb")
while True:
count = count + 1
partFile = file +'.part'+ str(count)
if not exists(partFile):
break
else:
f2 = open(partFile, "r")
content = f2.read()
f2.close()
f1.write(en.mydecrypt(content).rstrip())
f1.flush()
f1.close()
else:
print 'File type? Can not combine!'