-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionsc6.py
123 lines (100 loc) · 4.28 KB
/
functionsc6.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import string
# bit-wise hamming distance
def hamming_dist(bytesA, bytesB):
def bytes_to_bits(byte_block):
# Initialize an empty string to store the binary representation
bit_string = ""
# Iterate through each byte in the byte string
for byte in byte_block:
# Convert each byte to its binary representation
# and concatenate it to the bit string
bit_string += format(byte, '08b')
return bit_string
#print(bytesA, len(bytesA))
#print(bytesB, len(bytesB))
max_len = max(len(bytesA), len(bytesB))
bytesA = bytesA.ljust(max_len, b'\x00')
bytesB = bytesB.ljust(max_len, b'\x00')
straA_10 = bytes_to_bits(bytesA)
straB_10 = bytes_to_bits(bytesB)
#print(straA_10, len(straA_10))
#print(straB_10, len(straB_10))
count = 0
for i in range(len(straA_10)):
#print(straA_10[i],straB_10[i],straA_10[i] != straB_10[i])
if straA_10[i] != straB_10[i]:
count += 1
#print(count)
return count
#Print a list of binary data in hex
def lists_to_binary_hex(lists):
for sublist in lists:
binary_hex_list = [hex(item) for item in sublist]
print(binary_hex_list)
# Expected frequency of characters in English text
# Values are taken from https://en.wikipedia.org/wiki/Letter_frequency
expected_frequency = {
' ': 0.13000, 'e': 0.12702, 't': 0.09056, 'a': 0.08167, 'o': 0.07507,
'i': 0.06966, 'n': 0.06749, 's': 0.06327, 'h': 0.06094, 'r': 0.05987,
'd': 0.04253, 'l': 0.04025, 'c': 0.02782, 'u': 0.02758, 'm': 0.02406,
'w': 0.02360, 'f': 0.02228, 'g': 0.02015, 'y': 0.01974, 'p': 0.01929,
'b': 0.01492, 'v': 0.00978, 'k': 0.00772, 'j': 0.00153, 'x': 0.00150,
'q': 0.00095, 'z': 0.00074
}
#This function takes a block of bytes and XORs each byte with a single-byte key.
# The bytes(byte ^ key for byte in block) line performs the XOR operation for each byte in the block.
def xor_single_byte(block, key):
return bytes(byte ^ key for byte in block)
def evaluate_histogram(data):
# Calculate the frequency of each ASCII character
total_characters = len(data)
histogram = {char: data.count(bytes([ord(char)])) / total_characters for char in string.printable}
return histogram
def find_repeating_key_xor_key(blocks):
key = b''
for block in blocks:
#In Python, float('inf') represents positive infinity, which is a special floating-point value
#that is greater than any other real number. This is commonly used as an initial value for variables
#that need to be initialized with the maximum possible value, particularly in scenarios where you want
#to find the minimum value among a set of values.
best_score = float('inf')
best_key = None
for possible_key in range(256):
xored_block = xor_single_byte(block, possible_key)
histogram = evaluate_histogram(xored_block)
# Calculate the sum of squared differences between expected and observed frequencies
score = sum((histogram[char] - expected_frequency.get(char, 0)) ** 2 for char in string.printable)
if score < best_score: #The less discrepancy the better.
best_score = score
best_key = possible_key
key += bytes([best_key])
return key
def find_repeats(lst):
counts = {}
for item in lst:
if item in counts:
counts[item] += 1
else:
counts[item] = 1
repeats = [item for item, count in counts.items() if count > 1]
return repeats
def detect_AES(lst):
if len(set(lst)) != len(lst):
return True
else:
return False
def find_string_in_file(file_path, search_string):
try:
with open(file_path, 'r') as file:
line_number = 0
found = False
for line in file:
line_number += 1
if search_string in line:
print(f"Found '{search_string}' in {file_path} at line {line_number}:")
print(line.strip()) # Print the line where the string was found
found = True
if not found:
print(f"'{search_string}' not found in {file_path}")
except FileNotFoundError:
print(f"Error: File '{file_path}' not found")