-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
56 lines (45 loc) · 1.83 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import time
from algorithm import Algorithm
from ciphers.transpositionCipher import TranspositionCipher
from key import random_key
from plain_text import get_plain_text
# Change algorithms
# algo: Algorithm = Algorithm.GA
# algo: Algorithm = Algorithm.PSO
# algo: Algorithm = Algorithm.JAYA
# algo: Algorithm = Algorithm.DE
algo: Algorithm = Algorithm.EJAYA
algo_str: str = Algorithm.get_algo(algo)
key_length = 8
population_size = 100
max_iteration = 100
cipher_length = 500
text = get_plain_text(cipher_length)
encryption_key = ''.join(str(k) for k in random_key(key_length))
# convert text to cipher
CIPHER = TranspositionCipher().encrypt(text, encryption_key)
print(f"\nBegin {algo_str} for finding transposition cipher key\n")
print("Parameters:")
print("**************************************")
print(f"key length: {key_length}")
print(f"cipher length: {cipher_length}")
print(f"population_size: {population_size}")
print(f"max_iteration {max_iteration}")
print("**************************************")
print(f"\n-------------Starting {algo_str} algorithm-------------\n")
algo_obj = Algorithm.get_algo_obj(algo, CIPHER, population_size, max_iteration, key_length)
# start clock
start_time = time.time()
best_individual = algo_obj.run(log = True)
# end clock
end_time = time.time()
decryption_key = "".join([str(i) for i in best_individual.key])
print(f"\n-------------{algo_str} algorithm completed-------------\n")
print(f"Encryption Key {encryption_key}")
print("\nBest Key Found:")
# Decrypt the ciphertext using the key found by PSO
decrypt = TranspositionCipher().decrypt(CIPHER, decryption_key)
print(f"\n\nDecryption Key: {decryption_key} \tfitness: {best_individual.fitness}\n")
print(f"Decrypted Text: {decrypt}\n")
# Print the time taken to run the algorithm
print(f"Execution time of the {algo_str} algorithm: {end_time - start_time} seconds\n")