ValueError:对于AES,在python 2.7中解密加密数据文件时,输入字符串必须是长度错误的16倍

时间:2014-09-05 21:08:39

标签: python encryption 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'1234567890123456'
        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

请帮帮我。感谢任何回应。

1 个答案:

答案 0 :(得分:0)

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