-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxorencrypt.py
More file actions
60 lines (44 loc) · 1.42 KB
/
xorencrypt.py
File metadata and controls
60 lines (44 loc) · 1.42 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
import sys
def xor_encrypt(data: bytes, key: bytes) -> bytes:
"""
Applies XOR encryption to the given data using the provided key.
data : bytes
Raw binary data to encrypt.
key : bytes
XOR key that will be repeated throughout the data.
Returns
-------
bytes
Encrypted (or decrypted) byte sequence.
"""
encrypted = bytearray()
key_length = len(key)
for index, byte in enumerate(data):
key_byte = key[index % key_length]
encrypted.append(byte ^ key_byte)
return bytes(encrypted)
def main():
# Ensure a file was provided
if len(sys.argv) < 2:
print("Usage: python xorencrypt.py <input_file>")
return
input_filename = sys.argv[1]
# Read input file in binary mode
try:
with open(input_filename, "rb") as file:
plaintext = file.read()
except FileNotFoundError:
print(f"[-] Error: File '{input_filename}' not found.")
return
# XOR encryption key (change as needed)
key = b"key"
# Encrypt the file contents
ciphertext = xor_encrypt(plaintext, key)
# Create output filename
output_filename = f"{input_filename.rsplit('.', 1)[0]}_encrypted.bin"
# Write encrypted data to output file
with open(output_filename, "wb") as file:
file.write(ciphertext)
print(f"[+] XOR encryption complete: {output_filename}")
if __name__ == "__main__":
main()