-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenecrypt.py
More file actions
177 lines (129 loc) · 5.57 KB
/
Copy pathgenecrypt.py
File metadata and controls
177 lines (129 loc) · 5.57 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import argparse
import base64
import binascii
import zlib
import os
import hashlib
def compress_and_encode(text):
compressed = zlib.compress(text.encode())
return base64.urlsafe_b64encode(compressed).decode()
def decode_and_decompress(encoded):
compressed = base64.urlsafe_b64decode(encoded.encode())
return zlib.decompress(compressed).decode()
def text_to_genetic(text):
hex_str = binascii.hexlify(text.encode())
bin_str = bin(int(hex_str, 16))[2:].zfill(8 * ((len(hex_str) + 1) // 2))
return ''.join(['ATGC'[int(bin_str[i:i + 2], 2)] for i in range(0, len(bin_str), 2)])
def text_to_genetic_with_password(text, password):
hex_str = binascii.hexlify(text.encode())
hex_int = int(hex_str, 16)
updated_int = hex_int + int(password)
updated_hex = f"{updated_int:x}".zfill(len(hex_str))
bin_str = bin(int(updated_hex, 16))[2:].zfill(
8 * ((len(updated_hex) + 1) // 2))
return ''.join(['ATGC'[int(bin_str[i:i + 2], 2)] for i in range(0, len(bin_str), 2)])
def complement_sequence(seq):
comp_map = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}
return ''.join(comp_map[i] for i in seq)
def genetic_to_text(genetic_code):
bin_str = ''.join({
'A': '00', 'T': '01', 'G': '10', 'C': '11'
}[c] for c in genetic_code)
chars = [chr(int(bin_str[i:i + 8], 2)) for i in range(0, len(bin_str), 8)]
return ''.join(chars)
def key_encoding(password):
hashed = hashlib.sha256(password.encode()).hexdigest()
short_hex = hashed[:8]
return str(int(short_hex, 16))
def encrypt_text_mode(text):
vault = text_to_genetic(text)
print("[+] Genetic Code:", vault)
complement = complement_sequence(vault)
print("[+] Complement Code:", complement)
password = genetic_to_text(complement)
print("[+] Extracted Password:", password)
secret = key_encoding(compress_and_encode(password))
print("[+] Numeric Encoded Secret:", secret)
final_genetic = text_to_genetic_with_password(text, secret)
print("[+] Final Genetic Code with Key:", final_genetic)
def encrypt_file_mode(filepath):
if not os.path.isfile(filepath):
print("[-] File does not exist.")
return
with open(filepath, 'r') as f:
content = f.read()
print("[*] File Content:\n", content)
vault = text_to_genetic(content)
complement = complement_sequence(vault)
password = genetic_to_text(complement)
secret = key_encoding(compress_and_encode(password))
print("[+] Numeric Encoded Secret:", secret)
final_genetic = text_to_genetic_with_password(content, secret)
with open(filepath, 'w') as f:
f.write(final_genetic)
print("[+] Encrypted Genetic Code written to file.")
def decrypt_genetic_code_with_numeric_secret(genetic_code, numeric_secret):
print("[*] Decrypting Final Genetic Code with Numeric Secret...")
bin_str = ''.join({'A': '00', 'T': '01', 'G': '10', 'C': '11'}[
c] for c in genetic_code)
encoded_int = int(bin_str, 2)
original_int = encoded_int - int(numeric_secret)
original_hex = f"{original_int:x}"
if len(original_hex) % 2 != 0:
original_hex = '0' + original_hex
try:
original_bytes = binascii.unhexlify(original_hex)
original_text = original_bytes.decode()
print("[+] Decrypted Original Text:", original_text)
except Exception as e:
print("[-] Decryption failed:", str(e))
def decrypt_file_mode(filepath, numeric_secret):
if not os.path.isfile(filepath):
print("[-] File does not exist.")
return
with open(filepath, 'r') as f:
genetic_code = f.read().strip()
print("[*] Genetic Code from File:\n", genetic_code)
# Convert genetic code to binary
bin_str = ''.join({'A': '00', 'T': '01', 'G': '10', 'C': '11'}[
c] for c in genetic_code)
encoded_int = int(bin_str, 2)
try:
original_int = encoded_int - int(numeric_secret)
original_hex = f"{original_int:x}"
if len(original_hex) % 2 != 0:
original_hex = '0' + original_hex
original_bytes = binascii.unhexlify(original_hex)
original_text = original_bytes.decode()
print("[+] Decrypted Original Text:\n", original_text)
with open(filepath, 'w') as f:
f.write(original_text)
print("[+] File content replaced with decrypted text.")
except Exception as e:
print("[-] Decryption failed:", str(e))
def main():
parser = argparse.ArgumentParser(description="GeneCrypt CLI Tool")
parser.add_argument(
"-m", "--mode", choices=["encrypt", "decrypt"], required=True, help="Mode: encrypt or decrypt")
parser.add_argument(
"-t", "--type", choices=["text", "file"], required=True, help="Type of input: text or file")
parser.add_argument("-i", "--input", required=True,
help="Input text, genetic code, or file path")
parser.add_argument("-p", "--password", required=False,
help="Password (for decrypting final code)")
args = parser.parse_args()
if args.mode == "encrypt":
if args.type == "text":
encrypt_text_mode(args.input)
elif args.type == "file":
encrypt_file_mode(args.input)
elif args.mode == "decrypt":
if not args.password:
print("[-] Please provide the numeric secret using -p.")
return
if args.type == "text":
decrypt_genetic_code_with_numeric_secret(args.input, args.password)
elif args.type == "file":
decrypt_file_mode(args.input, args.password)
if __name__ == "__main__":
main()