-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextArmor.py
More file actions
221 lines (196 loc) · 8.22 KB
/
TextArmor.py
File metadata and controls
221 lines (196 loc) · 8.22 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# TextArmor
# A tool that encodes and decodes text with password.
# Author - WireBits
import os
import sys
import base64
import getpass
def show_banner():
print("+─────────────────────────────────+")
print("| ╔╦╗╔═╗═╗ ╦╔╦╗╔═╗╦═╗╔╦╗╔═╗╦═╗ |")
print("| ║ ║╣ ╔╩╦╝ ║ ╠═╣╠╦╝║║║║ ║╠╦╝ |")
print("| ╩ ╚═╝╩ ╚═ ╩ ╩ ╩╩╚═╩ ╩╚═╝╩╚═ |")
print("+─────────────────────────────────+")
print("| Text Encryption/Decryption Tool |")
print("+─────────────────────────────────+")
print("| Author : WireBits |")
print("+─────────────────────────────────+")
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def process():
clear_screen()
show_banner()
print("+──────+")
print("| Mode |")
print("+──────+")
print("╰┈➤ Base64 : b64")
print("╰┈➤ Hexadecimal : hex")
print("╰┈➤ Binary : bin")
print("╰┈➤ Octal : oct")
mode = get_input("╰┈➤ Select Mode ⮞ ", ['b64', 'hex', 'bin', 'oct'])
print("+─────────────────+")
print("| Encryption Type |")
print("+─────────────────+")
print("╰┈➤ Encode : e")
print("╰┈➤ Decode : d")
action = get_input("╰┈➤ Select Encryption Type ⮞ ", ['e', 'd'])
message = get_message_input()
if message is None:
return
key = getpass.getpass("╰┈➤ Enter password (supports special characters) ⮞ ").strip()
if not key:
print("╰┈➤ [!] Password cannot be empty!")
return
backup_choice = input("╰┈➤ Do you want to backup password? (yes/no) ⮞ ").strip().lower()
if backup_choice in ['yes', 'y']:
try:
with open("password_backup.txt", "w", encoding="utf-8") as f:
f.write(key)
print("╰┈➤ Password backed up to password_backup.txt")
except Exception as e:
print(f"╰┈➤ Error saving password backup: {e}")
try:
if mode == 'b64':
result = encode_base64(key, message) if action == 'e' else decode_base64(key, message)
elif mode == 'hex':
result = encode_hex(key, message) if action == 'e' else decode_hex(key, message)
elif mode == 'bin':
result = encode_binary(key, message) if action == 'e' else decode_binary(key, message)
elif mode == 'oct':
result = encode_octal(key, message) if action == 'e' else decode_octal(key, message)
else:
raise ValueError("Invalid mode.")
handle_output(result)
except Exception as e:
print(f"\nError: {e}")
def get_message_input():
print("+──────────+")
print("| Data |")
print("+──────────+")
print("╰┈➤ String : s")
print("╰┈➤ .txt file : f")
data_type = get_input("╰┈➤ Select Data ⮞ ", ['s', 'f'])
if data_type == 's':
return input("╰┈➤ Enter the text ⮞ ")
else:
while True:
file_path = input("╰┈➤ Enter path to the .txt file ⮞ ").strip()
if not os.path.isfile(file_path):
print("╰┈➤ [!] File not found. Please try again!\n")
continue
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
def handle_output(result):
print("+───────────+")
print("| View Mode |")
print("+───────────+")
print("╰┈➤ .txt file : t")
print("╰┈➤ Direct Output : show")
while True:
view_choice = input("╰┈➤ Select View ⮞ ").strip().lower()
if view_choice == 't':
filename = input("╰┈➤ Enter file name (without extension) ⮞ ").strip()
if not filename.endswith(".txt"):
filename += ".txt"
try:
with open(filename, 'w', encoding='utf-8') as f:
f.write(result)
print(f"╰┈➤ Output saved to: {filename}")
except Exception as e:
print(f"Error saving file: {e}")
break
elif view_choice == 'show':
print("╰┈➤ Final Output : \n")
print(result)
break
else:
print("╰┈➤ [!] Invalid input! Please try again!\n")
def encode_base64(key, message):
enc = [chr((ord(message[i]) + ord(key[i % len(key)])) % 256) for i in range(len(message))]
return base64.urlsafe_b64encode("".join(enc).encode()).decode()
def decode_base64(key, message):
try:
decoded = base64.urlsafe_b64decode(message).decode()
except Exception:
raise ValueError("Invalid Base64 input.")
return ''.join([chr((256 + ord(decoded[i]) - ord(key[i % len(key)])) % 256) for i in range(len(decoded))])
def encode_hex(key, message):
return ''.join([hex((ord(message[i]) + ord(key[i % len(key)])) % 256)[2:].zfill(2) for i in range(len(message))])
def decode_hex(key, message):
if len(message) % 2 != 0:
raise ValueError("Invalid Hex input.")
dec = []
for i in range(0, len(message), 2):
hex_val = int(message[i:i+2], 16)
key_c = key[(i // 2) % len(key)]
dec.append(chr((256 + hex_val - ord(key_c)) % 256))
return ''.join(dec)
def encode_binary(key, message):
return ' '.join([bin((ord(message[i]) + ord(key[i % len(key)])) % 256)[2:].zfill(8) for i in range(len(message))])
def decode_binary(key, message):
bits = message.split()
dec = []
for i, b in enumerate(bits):
try:
val = int(b, 2)
except Exception:
raise ValueError("Invalid Binary input.")
key_c = key[i % len(key)]
dec.append(chr((256 + val - ord(key_c)) % 256))
return ''.join(dec)
def encode_octal(key, message):
return ' '.join([oct((ord(message[i]) + ord(key[i % len(key)])) % 256)[2:].zfill(3) for i in range(len(message))])
def decode_octal(key, message):
parts = message.split()
dec = []
for i, p in enumerate(parts):
try:
val = int(p, 8)
except Exception:
raise ValueError("Invalid Octal input.")
key_c = key[i % len(key)]
dec.append(chr((256 + val - ord(key_c)) % 256))
return ''.join(dec)
def get_input(prompt, valid_values):
while True:
user_input = input(prompt).strip().lower()
if user_input in valid_values:
return user_input
print("╰┈➤ [!] Invalid input! Please try again!\n")
try:
import readline
except ImportError:
try:
import pyreadline as readline
except ImportError:
try:
import pyreadline3 as readline
except ImportError:
readline = None
if readline:
import glob
def complete_path(text, state):
line = readline.get_line_buffer().split()
if not line:
return [None][state]
else:
return (glob.glob(os.path.expanduser(text) + '*') + [None])[state]
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(complete_path)
def main():
while True:
process()
cont = input("\n╰┈➤ Do you want to continue? (yes/no) ⮞ ").strip().lower()
if cont not in ['yes', 'y']:
print("\n╰┈➤ [!] Exiting the tool. Goodbye!")
break
if __name__ == "__main__":
try:
os.system('clear')
main()
except KeyboardInterrupt:
print('\n╰┈➤ [!] Closing the tool. Goodbye!')
exit(0)
except Exception as e:
print('╰┈➤[!] ERROR: ' + str(e))