Example #1
0
def encrypt_ascii(text_to_encrypt):
    f = FeistelCipher()
    cipher = f.encrypt(text_to_encrypt, "sandvest")
    decrypted = f.decrypt(cipher.tobytes(), "sandvest")
    decrypted_text = decrypted.tobytes()
    decrypted_text = decrypted_text.replace("\x00", "")
    return decrypted_text
Example #2
0
def triple_encrypt_ascii(text_to_encrypt):
    f = FeistelCipher()
    key = "stiansandvestiansandv"
    cipher = f.triple_encrypt(text_to_encrypt, key)
    decrypted = f.triple_decrypt(cipher.to01(), key)
    decrypted_text = decrypted.tobytes()
    decrypted_text = decrypted_text.replace("\x00", "")
    return decrypted_text
Example #3
0
 def test_duration_of_single_block_encryption(self):
     f = FeistelCipher()
     key = "stiansandvestiansandv"
     start = time.time()
     f.triple_encrypt("ssssssss", key)
     end = time.time()
     print("Single block triple encryption: %fms" % ((end - start) * 1000))
     self.assertLess((end - start), 0.015)
Example #4
0
    def test_duration_of_triple_ascii_encryption(self):

        f = FeistelCipher()
        key = "stiansandvestiansandv"
        start = time.time()
        f.triple_encrypt(self.long_string, key)
        end = time.time()
        print("Long string triple encryption: %fms" % ((end - start) * 1000))
        self.assertLess((end - start), 0.4)
Example #5
0
    def test_avalanche(self):
        """
        A change of one bit in the plaintext, results in 26-42 bit flips in the
        ciphertext using the data below.

        The function iterates the initial binary plaintext, flips the bit at the
        current index, performs an encryption of the new plaintext and counts
        the number of bits that has flipped in the new ciphertext compared to
        the initial ciphertext. The bit that was flipped in the plaintext gets
        flipped back before the next iteration.

        The test also stores the minimum and maximum number of bit flips
        observed during the process.
        """

        key = "stiansandvestiansandv"

        des = FeistelCipher()
        text_to_encrypt = "10101101001011110100101111010010"
        cipher1 = des.triple_encrypt(text_to_encrypt, key)

        text_to_encrypt = bitarray(text_to_encrypt)
        min = None
        max = 0

        for idx, val in enumerate(text_to_encrypt):
            if val:
                text_to_encrypt[idx] = False
            else:
                text_to_encrypt[idx] = True

            cipher2 = des.triple_encrypt(text_to_encrypt.to01(), key)
            number_of_bit_flips = 0
            for i, bit in enumerate(cipher1):
                if bit != cipher2[i]:
                    number_of_bit_flips += 1

            if idx == 0:
                min = number_of_bit_flips

            if number_of_bit_flips < min:
                min = number_of_bit_flips

            if number_of_bit_flips > max:
                max = number_of_bit_flips

            if val:
                text_to_encrypt[idx] = False
            else:
                text_to_encrypt[idx] = True

        print("Lowest number of bit flips: %d" % min)
        print("Highest number of bit flips: %d" % max)

        self.assertGreaterEqual(min, 20)
Example #6
0
    def test_binary_encryption(self):
        """
        Verify that encryption/decryption works for binary input as well.
        """

        des = FeistelCipher()
        text_to_encrypt = "1010010100101010100001"
        cipher = des.encrypt(text_to_encrypt, "sandvest")
        decrypted = des.decrypt(cipher.to01(), "sandvest")
        decrypted = parse_decrypted_cipher(len(bitarray(text_to_encrypt)), decrypted.to01())
        self.assertEquals(text_to_encrypt, decrypted)
Example #7
0
    def test_duration_of_ascii_encryption(self):
        """
        Ensure that the encryption time is kept fairly low.
        """

        f = FeistelCipher()
        key = "sandvest"
        start = time.time()
        f.encrypt(self.long_string, key)
        end = time.time()
        print("Long string single encryption: %fms" % ((end - start) * 1000))
        self.assertLess((end - start), 0.2)
Example #8
0
def main():

    feistel = FeistelCipher()

    while True:
        encrypt = raw_input('\nChoose method:\n1. Encrypt\n2. '
                            'Decrypt\n3. Exit\n')

        if encrypt == '3':
            break

        text = raw_input('Enter plaintext or ciphertext: ')
        key = raw_input('Enter 168 bit key (21 characters): ')

        if encrypt == '1':
            result = feistel.triple_encrypt(text, key)
            print 'encrypting ...\n\n'
        elif encrypt == '2':
            result = feistel.triple_decrypt(text, key)
            print 'decrypting ...\n\n'

        print ('Binary: ' + result.to01())
        print ('UTF-8: ' + result.tobytes())