def encrypt_cbc(data, iv, key): enc_data = [] keylen = len(key) xor_with = binascii.hexlify(iv) for block in [data[i:i + keylen] for i in range(0, len(data), keylen)]: xored = hex_xor(binascii.hexlify(block), xor_with) enc_aes = aes_ecb_encrypt(binascii.unhexlify(xored), key) enc_data.append(''.join(enc_aes)) xor_with = binascii.hexlify(enc_aes) return ''.join(enc_data)
def decrypt_cbc(enc_data, iv, key): dec_data = [] keylen = len(key) xor_with = binascii.hexlify(iv) for block in [ enc_data[i:i + keylen] for i in range(0, len(enc_data), keylen) ]: aes_data_h = binascii.hexlify(aes_ecb_decrypt(block, key)) dec_data.append(''.join(hex_xor(aes_data_h, xor_with))) xor_with = binascii.hexlify(block) return (binascii.unhexlify(''.join(dec_data)))
def get_stat(x, s): s2 = "{:02x}".format(x) * (len(s) / 2) a1 = hex_xor(s, s2) out = unhexlify(a1) if (all(ord(c) < 128 for c in out)): out_spaces = out.count(' ') out_filter = filter(lambda x: ord(x.lower()) > 97 and x.isalpha(), list(out)) out_probs = map(lambda x: float(LetterProbs[ord(x.lower()) - 97]), out_filter) out_totals = (reduce(lambda x, y: x + y, out_probs, 0.0) + .2 * out_spaces) / len(out) return (out_totals, chr(x), out)
def hamming_hex(h1, h2): diff_bin = bin(int(hex_xor(h1, h2), 16))[2:] ham = reduce(lambda sum, e: sum + int(e), list(diff_bin), 0) return ham
def enc_rep(s,key): a=binascii.hexlify(s); b=binascii.hexlify((key*(len(s)/len(key)+1))[:len(str(s))]); return hex_xor(a,b);