-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbruteforce.py
More file actions
93 lines (70 loc) · 2.61 KB
/
bruteforce.py
File metadata and controls
93 lines (70 loc) · 2.61 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from rcncipher import encrypt, decrypt
import random
import time
#attempt to brute force the key after taking an input
def text_to_128_bit_hex_chunks(text):
# Convert text to bytes
text_bytes = text.encode('utf-8')
# Calculate the number of 128-bit chunks
num_chunks = (len(text_bytes) + 15) // 16
# Pad the text_bytes to make its length a multiple of 16 bytes
padded_bytes = text_bytes.ljust(num_chunks * 16, b'\0')
# Divide the padded bytes into 128-bit chunks
chunks = [padded_bytes[i:i+16] for i in range(0, len(padded_bytes), 16)]
# Convert each chunk to hexadecimal representation
hex_chunks = [chunk.hex() for chunk in chunks]
# print(hex_chunks)
return hex_chunks
def text_to_hex(text):
# Convert text to bytes
text_bytes = text.encode('utf-8')
# Convert bytes to hexadecimal representation
hex_representation = text_bytes.hex()
return hex_representation
def hex_to_text(hex_string):
# Convert hexadecimal string to bytes
byte_string = bytes.fromhex(hex_string)
# Decode bytes to text using UTF-8 encoding
text = byte_string.decode('utf-8')
return text
# Function to generate a random 128-bit key
def generate_key():
key = ""
for _ in range(128):
key += str(random.randint(0, 1))
return key
# Brute force algorithm to test against
def brute_force_attack():
# Iterate through all possible 128-bit keys
for i in range(2**128):
key = bin(i)[2:].zfill(128)
# Generate a random key
key = generate_key()
#convert key to hex
key = hex(int(key, 2))[2:]
# if(i == 10):
# key = "696e646961".zfill(32)
# print("Generated Key:", key, len(key))
ct = "3f766df87f5e353255aa5fb0cdb7a3d7"
# Call your algorithm function here with the generated key
result = decrypt(ct, key)
# Check if the result indicates success
print("Result:", result)
if result == "636f66666565".zfill(32):
return key
else:
print("Failed:", key)
return None
if __name__ == "__main__":
# Start time for brute force attack
start_time = time.time()
# Perform brute force attack
found_key = brute_force_attack()
# End time for brute force attack
end_time = time.time()
if found_key:
print("Brute force attack successful!")
print("Found Key:", found_key)
else:
print("Brute force attack failed!")
print("Time taken for brute force attack:", end_time - start_time, "seconds")