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