예제 #1
0
 def round_fun(self, left_text, right_text):
     result = des.expand(right_text)
     if self.mode == "encrypt":
         result = des.xor(self.subkeys[str(self.round)], result)
     else:
         result = des.xor(self.subkeys[str(17 - self.round)], result)
     result = des.shuffle('inverseEbox', result)
     result = des.xor(result, left_text)
     self.round += 1
     return right_text, result
    def test_xor(self):
        """Test that two bit strings are correctly xor'ed."""
        a = '110010110011110110001011000011100001011111110101'
        b = '110110100100000101011100001000001110101110101111'
        c = '000100010111110011010111001011101111110001011010'
        self.assertEquals(des.xor(a, b), c)

        a = '10001100100001010111000101001110'
        b = '01110110101011011001000111000111'
        c = '11111010001010001110000010001001'
        self.assertEquals(des.xor(a, b), c)
    def test_xor(self):
        """Test that two bit strings are correctly xor'ed."""
        a = '110010110011110110001011000011100001011111110101'
        b = '110110100100000101011100001000001110101110101111'
        c = '000100010111110011010111001011101111110001011010'
        self.assertEquals(des.xor(a, b), c)

        a = '10001100100001010111000101001110'
        b = '01110110101011011001000111000111'
        c = '11111010001010001110000010001001'
        self.assertEquals(des.xor(a, b), c)
예제 #4
0
    def des(pict, key):
        result = b''
        for i in range(len(pict) // 4):
            block = pict[i * 4:(i + 1) * 4]
            exp_block = expansion(block)
            xor_block = xor(exp_block, key)
            six_bits = divide_six_bits(xor_block)
            subs = []
            for j in range(8):
                table = subtable(j + 1)
                subs.append(substitution(six_bits[j], table))
            sub = concat_substitution_result(subs)
            perm = permutation(sub)
            result += perm

        return result
예제 #5
0
파일: CFB.py 프로젝트: Andos25/USTC
def decrypt(ciphertext, key, IV, s_length):
    key = des.trans_bin(key)
    ciphertext = des.hex_trans_bin(ciphertext)
    IV = des.trans_bin(IV)[:64]
    if len(key) < 64 or len(IV) < 64:
        print "the length of key or IV is too short, at least 64, process will be stoped, please change and restart"
        return
    keylist = des.getkeylist(key)
    # keylist = keylist[::-1]
    new_text = str()
    for i in range(0, len(ciphertext), s_length):
        cipher = ciphertext[i:i+s_length]
        plain = des.address_block(IV, keylist)
        plain = des.ip1_replacement(plain[32:]+plain[:32])
        new_text += des.xor(plain[:s_length], cipher)
        IV = IV[s_length:] + cipher
    plaintext = des.to_chr(new_text)
    return plaintext
예제 #6
0
파일: CBC.py 프로젝트: Andos25/USTC
def encrypt(plaintext, key, IV):
    plaintext += ' ' * ((8-len(plaintext)%8)%8)
    plaintext = des.trans_bin(plaintext)
    key = des.trans_bin(key)
    IV = des.trans_bin(IV)[:64]
    if len(key) < 64 or len(IV) < 64:
        print "the length of key or IV is too short, at least 64, process will be stoped, please change and restart"
        return
    keylist = des.getkeylist(key)
    new_text = str()
    for i in range(0, len(plaintext), 64):
        string = plaintext[i:i+64]
        string = des.xor(string, IV)      #IV xor with plaintext block
        string = des.address_block(string, keylist)
        IV = des.ip1_replacement(string[32:]+string[:32])
        new_text += IV
    ciphertext = des.bin_trans_hex(new_text)
    return ciphertext
예제 #7
0
파일: CBC.py 프로젝트: Andos25/USTC
def decrypt(ciphertext, key, IV):
    key = des.trans_bin(key)
    ciphertext = des.hex_trans_bin(ciphertext)
    IV = des.trans_bin(IV)[:64]
    if len(key) < 64 or len(IV) < 64:
        print "the length of key or IV is too short, at least 64, process will be stoped, please change and restart"
        return
    keylist = des.getkeylist(key)
    keylist = keylist[::-1]
    new_text = str()
    for i in range(0, len(ciphertext), 64):
        cipher = ciphertext[i:i+64]
        string = des.address_block(cipher, keylist)
        string = des.ip1_replacement(string[32:]+string[:32])
        new_text += des.xor(string, IV)  #IV xor with cipher block
        IV = cipher
    plaintext = des.to_chr(new_text)
    return plaintext
예제 #8
0
파일: CFB.py 프로젝트: Andos25/USTC
def encrypt(plaintext, key, IV, s_length):
    # plaintext += ' ' * ((8-len(plaintext)%8)%8)
    plaintext = des.trans_bin(plaintext)
    key = des.trans_bin(key)
    IV = des.trans_bin(IV)[:64]
    if len(key) < 64 or len(IV) < 64:
        print "the length of key or IV is too short, at least 64, process will be stoped, please change and restart"
        return
    keylist = des.getkeylist(key)
    new_text = str()
    for i in range(0, len(plaintext), s_length):
        string = plaintext[i:i+s_length]
        cipher = des.address_block(IV, keylist)
        cipher = des.ip1_replacement(cipher[32:]+cipher[:32])
        cipher = des.xor(cipher[:s_length], string)
        new_text += cipher
        IV = IV[s_length:] + cipher
    ciphertext = des.bin_trans_hex(new_text)
    return ciphertext
def main():
    # validation could be added but since input is hardcoded there is no need to do that
    key = "133457799BBCDFF1"
    plaintext = "GIACHANA"

    # perform validity checks on input
    if len(plaintext) < 8:
        print(
            "[-] Plaintext must be at least 64 bits (or 6 characters) long. Padding is not supported (yet)."
        )
        print("Exiting...")
        exit(0)

    if len(key) < 16:
        print("[-] Key must be at least  128 bits (or 16 characters) long.")
        print("Exiting...")
        exit(0)

    print("[+] Starting")
    # split the key and get the 16 subkeys
    future_subkeys = None

    # create one thread for the creation of the 16 subkeys and another one for the
    # initial block calculation
    with concurrent.futures.ThreadPoolExecutor() as executor:
        future_subkeys = executor.submit(get_keys, key)
        # thread for blocks
        future_block = executor.submit(initial_permutation, plaintext)

    # get the subkeys
    subkeys = future_subkeys.result()

    # get the ip of the block
    block = future_block.result()
    # split the block
    l, r = des.split_block(block)

    print(" starting rounds...")
    result = list()
    # start the 16 rounds
    for i in range(ROUNDS):
        # expand r to 48
        exp_r = des.expand(r, destables.E)
        # xor r with key
        x = des.xor(exp_r, subkeys[i])
        # apply SBOXes to x (splitting is done internally)
        x = des.substitute(x)
        # perform permutation
        x = des.permutation(x, destables.P)
        # xor x with r
        x = des.xor(l, x)
        l = r
        r = x

    # perform the final permutation
    result += des.permutation(r + l, destables.FP)
    print("[+] ENCRYPTION SUCCESSFUL")

    # print ciphertext
    ciphertext = bit_lst_to_string(result)
    print("\n\nciphertext: %r" % ciphertext)

    # print hex format
    hex_ciphertext = ''.join([hex(int(ord(i)))[2:] + " " for i in ciphertext])
    print("\tas hex: ", hex_ciphertext)

    # write to file
    print("\nWriting to file: 'ciphertext.txt'")
    with open('ciphertext.txt', "w") as fout:
        fout.write(ciphertext)
        fout.write("\n\n\n")
        fout.write(hex_ciphertext)
예제 #10
0
 def test_xor(self):
     a = [1, 1, 0, 1, 1, 0, 1, 1]
     b = [0, 1, 1, 0, 1, 0, 1, 0]
     expected = [1, 0, 1, 1, 0, 0, 0, 1]
     self.assertEqual(des.xor(a, b), expected)