美文网首页
python AES文件加解密

python AES文件加解密

作者: 半大人 | 来源:发表于2020-06-25 21:21 被阅读0次

    下面代码,是我在
    https://blog.csdn.net/chouzhou9701/article/details/89339625
    里,的代码基础上修改添加了文件加解密部分的代码

    import os
    from Crypto.Cipher import AES
    
    class Aescrypt():
        def __init__(self,key,model):
            self.key = self.add_16(key)
            self.model = model
            self.iv = '0000000000000000'
    
        def add_16(self,par):
            if type(par) == str:
                par = par.encode()
            while len(par) % 16 != 0:
                par += b'\x00'
            return par
    
        def aesencrypt(self,text):
            text = self.add_16(text)
            if self.model == AES.MODE_CBC:
                self.aes = AES.new(self.key,self.model,self.iv) 
            elif self.model == AES.MODE_ECB:
                self.aes = AES.new(self.key,self.model) 
            self.encrypt_text = self.aes.encrypt(text)
            return self.encrypt_text
    
        def aesdecrypt(self,text):
            if self.model == AES.MODE_CBC:
                self.aes = AES.new(self.key,self.model,self.iv) 
            elif self.model == AES.MODE_ECB:
                self.aes = AES.new(self.key,self.model) 
            self.decrypt_text = self.aes.decrypt(text)
            self.decrypt_text = self.decrypt_text.strip(b"\x00")
            return self.decrypt_text
        
        def encrypt_file(self,filepath,encrypt_filepath=''):
            #加载数据
            with open(filepath,'rb') as f:
                data=f.read()
            #加密数据
            data=self.aesencrypt(data)
            #夹加密好的数据写到加密文件上
            if not encrypt_filepath:
                filepath=os.path.split(filepath)
                encrypt_filepath=filepath[0]+'/encrypt_'+filepath[1]
            with open(encrypt_filepath,'wb') as f:
                f.write(data)
    
        def decrypt_file(self,encrypt_filepath,decrypt_filepath=''):
            #加载数据
            with open(encrypt_filepath,'rb') as f:
                data=f.read()
            #加密数据
            data=self.aesdecrypt(data)
            #夹加密好的数据写到加密文件上
            if not decrypt_filepath:
                encrypt_filepath=os.path.split(encrypt_filepath)
                decrypt_filepath=encrypt_filepath[0]+'/decrypt_'+encrypt_filepath[1]
            with open(decrypt_filepath,'wb') as f:
                f.write(data)
                
                
    
    if __name__ == '__main__':
        passwd = "123456781234567"
        aescryptor = Aescrypt(passwd,AES.MODE_CBC) # CBC模式
        # aescryptor = Aescrypt(passwd,AES.MODE_ECB,"") # ECB模式
        text = b"hello world"
        en_text = aescryptor.aesencrypt(text)
        print("密文:",en_text)
        text = aescryptor.aesdecrypt(en_text)
        print("明文:",text)
        #对文件的加密解密
        aescryptor.encrypt_file('e://hello.txt')
        aescryptor.decrypt_file('e://encrypt_hello.txt')
    
    

    相关文章

      网友评论

          本文标题:python AES文件加解密

          本文链接:https://www.haomeiwen.com/subject/mjdefktx.html