def encrypt(self, msg): #message can be of any length... #this manages padding here.. cipher_txt = '' msg_chunks = \ [msg[x:x+Config.ENC_DEC_BLOCK_SIZE] for x in range(0,len(msg),Config.ENC_DEC_BLOCK_SIZE)] for i in range(0, len(msg_chunks) - 1): #doesn't require any padding... #generate a random-iv #??? iv = Random.new().read(AES.block_size) aesVar = AES.new(self.aes_key, AES.MODE_CBC, iv) cipher_txt += (iv + aesVar.encrypt(msg_chunks[i] + chr(self.counter))) self.counter = (self.counter + 1) % 256 last_plain_txt = msg_chunks[-1] + chr(self.counter) #this may not be a multiple of enc_dec_block_size... #so add necessary padding... #**CAREFUL: padding zeros at the end!!** #last_plain_txt = last_plain_txt + \ #chr(0)*(Config.ENC_DEC_BLOCK_SIZE-len(last_plain_txt)) l = Config.ENC_DEC_BLOCK_SIZE last_plain_txt = chr(l - len(last_plain_txt)%l)*\ ( l- len(last_plain_txt)%l) iv = Random.new().read(AES.block_size) aesVar = AES.new(self.aes_key, AES.MODE_CBC, iv) cipher_txt += (iv + aesVar.encrypt(last_plain_txt)) return cipher_txt
def encrypt(self, msg): #message can be of any length... #this manages padding here.. cipher_txt='' msg_chunks = \ [msg[x:x+Config.ENC_DEC_BLOCK_SIZE] for x in range(0,len(msg),Config.ENC_DEC_BLOCK_SIZE)] for i in range(0, len(msg_chunks)-1): #doesn't require any padding... #generate a random-iv #??? iv = Random.new().read(AES.block_size) aesVar = AES.new(self.aes_key, AES.MODE_CBC, iv) cipher_txt += (iv +aesVar.encrypt(msg_chunks[i]+chr(self.counter))) self.counter = (self.counter+1)%256 last_plain_txt = msg_chunks[-1] + chr(self.counter) #this may not be a multiple of enc_dec_block_size... #so add necessary padding... #**CAREFUL: padding zeros at the end!!** #last_plain_txt = last_plain_txt + \ #chr(0)*(Config.ENC_DEC_BLOCK_SIZE-len(last_plain_txt)) l = Config.ENC_DEC_BLOCK_SIZE last_plain_txt = chr(l - len(last_plain_txt)%l)*\ ( l- len(last_plain_txt)%l) iv=Random.new().read(AES.block_size) aesVar = AES.new(self.aes_key, AES.MODE_CBC, iv) cipher_txt += (iv +aesVar.encrypt(last_plain_txt)) return cipher_txt
def generateIV(self): self.iv = Random.new().read(AES.block_size) return self.iv
def generateIV(self): self.iv = Random.new().read(AES.block_size) return self.iv