def c15(s): """ This function attempts to check whether PKCS#7 padding is being used properly in the given string If the padding is wron, it raises an exception """ # if PKCS#7 padding were used then the last byte must indicate the same pad_chr = s[-1] try: size = ord(pad_chr) Flag = True # the last size bytes must be equal to size for a valid PKCS#7 padding # if not, we have wrong padding here for i in range(size): if ord(s[-i-1]) != size: Flag = False break # raise exception if we don't have proper padding if Flag == True: print base.pkcs7_unpad(s) print s[:-ord(s[-1])] else: raise Exception except: # raise exception if any of the operations throws an exception print "exception!" raise Exception
def c10(filename): f = open(filename,"r") data = f.readlines() f.close() pad = "\x04" decoded_data = base.base64_to_hex(''.join(data).strip()).decode('hex') block_size = 16 key = "YELLOW SUBMARINE" iv = ''.join(["\x00" for i in range(block_size)]) blocks = [str(decoded_data[i*block_size : (i+1)*block_size]) for i in range(int(len(decoded_data)/block_size))] op = [] size = len(blocks) for i in range(size): pt = base.AES_ECB_decrypt(blocks[i],key) res = base.equal_size_xor(bytearray(pt),bytearray(iv)) res = base.bytearray_to_ASCII(res) op.append(res) iv = blocks[i] plaintext = base.pkcs7_unpad(''.join(op)) print plaintext