SHOW:
|
|
- or go back to the newest paste.
1 | #!/usr/bin/env python3 | |
2 | ||
3 | # pizza_arg_3.py | |
4 | # knocked together in record time (i.e., not very pythonic, readability prioritized, don't judge) | |
5 | # by sfsdfd / david@djstein.com ... hope this helps the effort | |
6 | ||
7 | - | # now translated into python3 |
7 | + | # *** now translated into python3 *** |
8 | ||
9 | # using arc4 sample code from here: | |
10 | # https://www.dlitz.net/software/pycrypto/api/current/Crypto.Cipher.ARC4-module.html | |
11 | # ...and using twofish code from here: | |
12 | # https://pypi.org/project/twofish/ | |
13 | ||
14 | # make sure you install both libraries first: | |
15 | # pip3 install --user pycrypto twofish | |
16 | # ...you could use virtulenv if that's your thing, left as an exercise for the reader | |
17 | ||
18 | # usage: | |
19 | # pizza_arg_3.py test_arc4 | |
20 | # - shows a brief demo of arc4 code to prove that it works | |
21 | # pizza_arg_3.py test_twofish | |
22 | # - shows a brief demo of twofish code to prove that it works | |
23 | # pizza_arg_3.py test_files | |
24 | # - saves some sample data to test_ciphertext, test_arc4_key, and test_twofish_key | |
25 | # pizza_arg_3.py [ciphertext_filename] [arc4_key_filename] [twofish_key_filename] | |
26 | # - serial decoding: ciphertext --> arc4 (using key) --> twofish (using key) | |
27 | # should be easy to reorganize the __main__ code if you want to do smth else, like | |
28 | # reverse the decryption order, brute-force against various combinations of files, etc. | |
29 | ||
30 | import codecs, os, sys | |
31 | from Crypto.Cipher import ARC4 | |
32 | from twofish import Twofish | |
33 | ||
34 | def encode_arc4(plaintext, arcfour_key): | |
35 | arc4 = ARC4.new(arcfour_key) | |
36 | return arc4.encrypt(plaintext) | |
37 | ||
38 | def encode_arc4(plaintext, arcfour_key): | |
39 | arc4 = ARC4.new(arcfour_key) | |
40 | return arc4.encrypt(plaintext) | |
41 | ||
42 | def decode_arc4(ciphertext, arcfour_key): | |
43 | arc4 = ARC4.new(arcfour_key) | |
44 | return arc4.decrypt(ciphertext) | |
45 | ||
46 | def encode_twofish(plaintext, twofish_key): | |
47 | ciphertext = b'' | |
48 | twofish = Twofish(twofish_key.encode()) | |
49 | while len(plaintext) > 0: | |
50 | ciphertext = b''.join([ciphertext, twofish.encrypt(plaintext[:16].ljust(16).encode())]) | |
51 | plaintext = plaintext[16:] | |
52 | return ciphertext | |
53 | ||
54 | def decode_twofish(ciphertext, twofish_key): | |
55 | plaintext = '' | |
56 | twofish = Twofish(twofish_key.encode()) | |
57 | while len(ciphertext) > 0: | |
58 | plaintext += twofish.decrypt(ciphertext[:16].ljust(16)).decode() | |
59 | ciphertext = ciphertext[16:] | |
60 | return plaintext | |
61 | ||
62 | if __name__ == '__main__': | |
63 | try: | |
64 | if len(sys.argv) == 2 and sys.argv[1] == 'test_arc4': | |
65 | print('Testing arcfour:') | |
66 | arcfour_key = '12345' | |
67 | plaintext = 'The quick brown fox jumped over the lazy dogs.' | |
68 | print('Original plaintext: ' + plaintext) | |
69 | ciphertext = encode_arc4(plaintext, arcfour_key) | |
70 | print('Ciphertext:' + str(ciphertext)) | |
71 | decoded_plaintext = decode_arc4(ciphertext, arcfour_key) | |
72 | print('Plaintext: ' + decoded_plaintext.decode('utf-8')); sys.exit(1) | |
73 | elif len(sys.argv) == 2 and sys.argv[1] == 'test_twofish': | |
74 | print('Testing twofish:') | |
75 | twofish_key = '67890' | |
76 | plaintext = 'The quick brown fox jumped over the lazy dogs.' | |
77 | print('Original plaintext: ' + plaintext) | |
78 | ciphertext = encode_twofish(plaintext, twofish_key) | |
79 | print('Ciphertext:' + str(ciphertext)) | |
80 | decoded_plaintext = decode_twofish(ciphertext, twofish_key) | |
81 | print('Plaintext: ' + decoded_plaintext); sys.exit(1) | |
82 | elif len(sys.argv) == 2 and sys.argv[1] == 'test_files': | |
83 | plaintext = 'The quick brown fox jumped over the lazy dogs.' | |
84 | arcfour_key = '12345' | |
85 | twofish_key = '67890' | |
86 | twofish_encode = encode_twofish(plaintext, twofish_key) | |
87 | print('Twofish encode: ' + str(twofish_encode)) | |
88 | arcfour_encode = encode_arc4(twofish_encode, arcfour_key) | |
89 | print('ARC4 encode: ' + str(arcfour_encode)) | |
90 | with open('test_ciphertext', 'wb') as f: | |
91 | f.write(arcfour_encode) | |
92 | with codecs.open('test_arc4_key', 'w', 'utf-8') as f: | |
93 | f.write(arcfour_key + '\n') | |
94 | with codecs.open('test_twofish_key', 'w', 'utf-8') as f: | |
95 | f.write(twofish_key + '\n') | |
96 | print('Wrote test files.'); sys.exit(1) | |
97 | elif len(sys.argv) != 4: | |
98 | print('Usage: pizza_arg.py ciphertext_file arcfour_key_file twofish_key_file') | |
99 | sys.exit(1) | |
100 | if os.path.isfile(sys.argv[1]) is False: | |
101 | print('ciphertext_file: Invalid file name'); sys.exit(1) | |
102 | if os.path.isfile(sys.argv[2]) is False: | |
103 | print('arcfour_key_file: Invalid file name'); sys.exit(1) | |
104 | if os.path.isfile(sys.argv[3]) is False: | |
105 | print('twofish_key_file: Invalid file name'); sys.exit(1) | |
106 | print('Two-step decipher:') | |
107 | ciphertext = open(sys.argv[1], 'rb').read() | |
108 | arcfour_key = codecs.open(sys.argv[2], 'r', 'utf-8').read().strip() | |
109 | twofish_key = codecs.open(sys.argv[3], 'r', 'utf-8').read().strip() | |
110 | arcfour_decode = decode_arc4(ciphertext, arcfour_key) | |
111 | print('ARC4 decode: ' + str(arcfour_decode)) | |
112 | twofish_decode = decode_twofish(arcfour_decode, str(twofish_key)) | |
113 | print('Twofish decode: ' + str(twofish_decode)) | |
114 | except Exception as e: | |
115 | print('Exception: ' + str(e)) |