-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRC4.py
59 lines (44 loc) · 1.15 KB
/
RC4.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
#!/usr/bin/env python
#
# Anne-Marie Oelschlager
# RC4.py
# A simple program to encrypt an input file with RC4.
#
import sys
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
def main():
# process command line arguments
if len(sys.argv) < 3:
print("Usage: fileEncrypt.py key iv file")
sys.exit()
# 1. file
name = sys.argv[1]
print("filename: %s" % name)
# 2. key
key = sys.argv[2]
print("key: %s" % key)
# open files
try:
inFile = open(name, 'r')
outFile = open(name + '.rc4', 'w')
except IOError:
print("Error: could not open file: %s" % name )
sys.exit()
# save data in inFile
data = inFile.read()
# save hex key to bytes
keyBytes = bytes(bytearray.fromhex(key))
# prep cipher
cipher = Cipher(algorithms.ARC4(keyBytes), mode=None, backend=backend)
# encrypt plain text
encryptor = cipher.encryptor()
ct = encryptor.update(data)
outFile.write(ct)
# closed files
inFile.close()
outFile.close()
return 0
if __name__ == '__main__':
main()