def encrypt(plaintext, key, iv): if len(plaintext) // 16 or len(plaintext) < 16: plaintext = appendPadding(plaintext, _required_padding(len(plaintext))) if len(key) < 32: key = appendPadding(key, 32) cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC, iv) ciphertext = cipher.encrypt(plaintext.encode("utf-8")) return ciphertext
def des_encrypt(text, key, iv): sercetykey = md5(key)[:8].encode('utf-8') sercetyiv = md5(iv)[24:32].encode('utf-8') encryptor = DES.new(sercetykey, DES.MODE_CBC, sercetyiv) padded = appendPadding(text, DES.block_size, mode='CMS') ciphertext = encryptor.encrypt(padded) return ciphertext
def aes_cfb_enc(key,plain_txt,iv,mode,segment_size): #Encryption for AES-CFB global blocksize encryption_suite=AES.new(key,AES.MODE_CFB,iv,segment_size=segment_size) plain_txt_pad=appendPadding(plain_txt,blocksize=blocksize,mode=mode) cph_txt=encryption_suite.encrypt(plain_txt_pad) return cph_txt
def aes_cbc_enc(key,plain_txt,iv,mode): #Encryption of AES-CBC global blocksize encryption_suite = AES.new(key,AES.MODE_CBC,iv) plain_txt_pad=appendPadding(plain_txt, blocksize=blocksize, mode=mode) cph_txt=encryption_suite.encrypt(plain_txt_pad) return cph_txt
def des3_cbc_enc(key, plain_txt, iv, mode): #Encryption for DES3-CBC global blocksize encryption_suite = DES3.new(key, DES3.MODE_CBC, iv) plain_txt_pad = appendPadding(plain_txt, blocksize=blocksize, mode=mode) cph_txt = encryption_suite.encrypt(plain_txt_pad) return cph_txt
def des3_ecb_enc(key, plain_txt, mode): #Encryption for DES3-ECB global blocksize encryption_suite = DES3.new(key, DES3.MODE_ECB) plain_txt_pad = appendPadding(plain_txt, blocksize=blocksize, mode=mode) cph_txt = encryption_suite.encrypt(plain_txt_pad) return cph_txt
def aes_encrypt(text, key, iv): #2e1bc1e3ca65a4cb sercetykey = str(md5(key)[16:32]).encode('utf-8') #c7054589723df4a7 sercetyiv = str(md5(iv)[:16]).encode('utf-8') encryptor = AES.new(sercetykey, AES.MODE_CBC, sercetyiv) padded = appendPadding(text.encode('base64'), AES.block_size, mode='CMS') ciphertext = encryptor.encrypt(padded) return ciphertext.encode('base64')
def decrypt(ciphertext, key, iv): if len(key) < 32: key = appendPadding(key, 32) cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC, iv) plaintext = removePadding((cipher.decrypt(ciphertext)).decode(), _required_padding(len(ciphertext))) return plaintext
def appendPadding(string): return appendPadding(string, blocksize=AES_blocksize, mode='CMS')
def decrypt(self, data): initial_len = len(data) padded = appendPadding(data) return self.decrypter.encrypt(padded)[:initial_len]