2014-09-05 115 views
-1

这里是代码ValueError异常:输入字符串必须在蟒2.7 16的长度误差的倍数上的加密的数据文件进行解密的AES

import sys, os 
import hashlib 
import base64 
from Crypto.Cipher import AES 

class ED: 

    def __init__(self,infilep,infilen,outfilep): 

     self.BLOCK_SIZE= 32 
     self.IV = u'123456789' 
     self.INTERRUPT = '\1' 
     self.PAD = '\0' 
     self.SECRET = os.urandom(32) 
     self.infilepath = infilep 
     self.infilename = infilen 
     self.outfilepath = outfilep 


    def EncObj(self): 
     cipher = AES.new(self.SECRET, AES.MODE_CBC, self.IV) 
     return cipher 

    def DecObj(self): 
     decrypt_cipher = AES.new(self.SECRET, AES.MODE_CBC, self.IV) 
     return decrypt_cipher 


    def FileRead(self): 
     frname = self.infilepath + '/' + self.infilename 
     f = open(frname, 'rb') 
     frdata = f.read() 
     base64data = base64.b64encode(frdata) 
     return base64data 

    def AddPadding(self,data): 
     new_data = ''.join([data, self.INTERRUPT]) 
     new_data_len = len(new_data) 
     remaining_len = self.BLOCK_SIZE - new_data_len 
     to_pad_len = remaining_len % self.BLOCK_SIZE 
     pad_string = self.PAD * to_pad_len 
     return ''.join([new_data, pad_string]) 

    def enc(self,cipher_code,file_data): 
     encrypted_data = cipher_code.encrypt(file_data) 
     encrypted_contant = base64.b64encode(encrypted_data) 
     return encrypted_contant 

    def FileSave(self,fwname, fwdata): 
     f = open(fwname, 'w') 
     f.write(fwdata) 
     f.close 

    def StripPadding(self,data): 
     return data.rstrip(self.PAD).rstrip(self.INTERRUPT) 

    def dec(self,cipher_code, file_data): 
     decrypted = cipher_code.decrypt(file_data) 
     decrypted_contant = base64.b64encode(decrypted) 
     return decrypted_contant 

**强文本**这里是代码创建解密对象

obj2 = ED('infilepath', 'cipher.txt', 'outfilepath') 
    print obj2 
    datatodec = obj2.FileRead() 
    #print datatodec 
    decobject = obj2.DecObj() 
    data = obj2.decAES(decobject, datatodec) 
    finaldata = obj2.StripPadding(data) 
    print finaldata 

请帮助我。欣赏任何回应。

回答

0

首先解码基础64,然后解密。

相关问题