Exemple #1
0
def ctr_decrypt(cipher, key, nunce):
    ctr = 0
    blocks = split_to_blocks(cipher)
    result = ""
    for block in blocks:
        bin_nunce = plaintobin(nunce)
        bin_ctr = '{0:064b}'.format(ctr)
        new_nunce = binary_string(xor(bin_nunce, bin_ctr, 64))
        enc_ctr = des_encrypt(new_nunce, key)
        bin_enc = plaintobin(enc_ctr)
        bin_block = plaintobin(block)
        xored = binary_string(xor(bin_enc, bin_block, 64))
        ctr += 1
        result += xored
    return result
Exemple #2
0
def ofb_decrypt(cipher, key, iv):
    blocks = split_to_blocks(cipher)
    result = ""
    for block in blocks:
        iv = des_encrypt(iv, key)
        bin_iv = plaintobin(iv)
        bin_block = plaintobin(block)
        xored = binary_string(xor(bin_block, bin_iv, 64))
        result += xored
    return result
Exemple #3
0
def cbc_encrypt(plain, key, iv):
    blocks = split_to_blocks(plain)
    result = ""
    for block in blocks:
        bin_block = plaintobin(block)
        bin_iv = plaintobin(iv)
        xored = binary_string(xor(bin_block, bin_iv, 64))
        iv = des_encrypt(xored, key)
        result += iv
    return result
Exemple #4
0
def cfb_encrypt(plain, key, iv):
    blocks = split_to_blocks(plain)
    result = ""
    for block in blocks:
        enc_iv = des_encrypt(iv, key)
        bin_iv = plaintobin(enc_iv)
        bin_block = plaintobin(block)
        xored = binary_string(xor(bin_block, bin_iv, 64))
        iv = xored
        result += xored
    return result
Exemple #5
0
def cbc_decrypt(cipher, key, iv):
    blocks = split_to_blocks(cipher)
    result = ""
    for block in blocks:
        bin_iv = plaintobin(iv)
        code = des_decrypt(block, key)
        bin_code = plaintobin(code)
        xored = binary_string(xor(bin_code, bin_iv, 64))
        iv = block
        result += xored
    return result