Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.7
- # pizza_arg.py
- # knocked together in record time (i.e., not very pythonic, readability prioritized, don't judge)
- # by sfsdfd / david@djstein.com ... hope this helps the effort
- # using arc4 sample code from here:
- # https://www.dlitz.net/software/pycrypto/api/current/Crypto.Cipher.ARC4-module.html
- # ...and using twofish code from here:
- # https://pypi.org/project/twofish/
- # make sure you install both libraries first:
- # pip install --user pycrypto twofish
- # ...you could use virtualenv if that's your thing, left as an exercise for the reader
- # usage:
- # pizza_arg.py test_arc4
- # - shows a brief demo of arc4 code to prove that it works
- # pizza_arg.py test_twofish
- # - shows a brief demo of twofish code to prove that it works
- # pizza_arg.py test_files
- # - saves some sample data to test_ciphertext, test_arc4_key, and test_twofish_key
- # pizza_arg.py [ciphertext_filename] [arc4_key_filename] [twofish_key_filename]
- # - serial decoding: ciphertext --> arc4 (using key) --> twofish (using key)
- # should be easy to reorganize the __main__ code if you want to do smth else, like
- # reverse the decryption order, brute-force against various combinations of files, etc.
- import codecs, os, sys
- from Crypto.Cipher import ARC4
- from twofish import Twofish
- def encode_arc4(plaintext, arcfour_key):
- arc4 = ARC4.new(arcfour_key)
- return arc4.encrypt(plaintext)
- def encode_arc4(plaintext, arcfour_key):
- arc4 = ARC4.new(arcfour_key)
- return arc4.encrypt(plaintext)
- def decode_arc4(ciphertext, arcfour_key):
- arc4 = ARC4.new(arcfour_key)
- return arc4.decrypt(ciphertext)
- def encode_twofish(plaintext, twofish_key):
- ciphertext = ''
- twofish = Twofish(twofish_key)
- while len(plaintext) > 0:
- ciphertext += twofish.encrypt(plaintext[:16].ljust(16))
- plaintext = plaintext[16:]
- return ciphertext
- def decode_twofish(ciphertext, twofish_key):
- plaintext = ''
- twofish = Twofish(twofish_key)
- while len(ciphertext) > 0:
- plaintext += twofish.decrypt(ciphertext[:16].ljust(16)).decode()
- ciphertext = ciphertext[16:]
- return plaintext
- if __name__ == '__main__':
- try:
- if len(sys.argv) == 2 and sys.argv[1] == 'test_arc4':
- print 'Testing arcfour:'
- arcfour_key = '12345'
- plaintext = 'The quick brown fox jumped over the lazy dogs.'
- print 'Original plaintext: ' + plaintext
- ciphertext = encode_arc4(plaintext, arcfour_key)
- print 'Ciphertext:' + ciphertext
- decoded_plaintext = decode_arc4(ciphertext, arcfour_key)
- print 'Plaintext: ' + decoded_plaintext; sys.exit(1)
- elif len(sys.argv) == 2 and sys.argv[1] == 'test_twofish':
- print 'Testing twofish:'
- twofish_key = '67890'
- plaintext = 'The quick brown fox jumped over the lazy dogs.'
- print 'Original plaintext: ' + plaintext
- ciphertext = encode_twofish(plaintext, twofish_key)
- print 'Ciphertext:' + ciphertext
- decoded_plaintext = decode_twofish(ciphertext, twofish_key)
- print 'Plaintext: ' + decoded_plaintext; sys.exit(1)
- elif len(sys.argv) == 2 and sys.argv[1] == 'test_files':
- plaintext = 'The quick brown fox jumped over the lazy dogs.'
- arcfour_key = '12345'
- twofish_key = '67890'
- twofish_encode = encode_twofish(plaintext, twofish_key)
- print 'Twofish encode: ' + twofish_encode
- arcfour_encode = encode_arc4(twofish_encode, arcfour_key)
- print 'ARC4 encode: ' + arcfour_encode
- with open('test_ciphertext', 'wb') as f:
- f.write(arcfour_encode)
- with codecs.open('test_arc4_key', 'w', 'utf-8') as f:
- f.write(arcfour_key + '\n')
- with codecs.open('test_twofish_key', 'w', 'utf-8') as f:
- f.write(twofish_key + '\n')
- print 'Wrote test files.'; sys.exit(1)
- elif len(sys.argv) <> 4:
- print 'Usage: pizza_arg.py ciphertext_file arcfour_key_file twofish_key_file'
- sys.exit(1)
- if os.path.isfile(sys.argv[1]) is False:
- print 'ciphertext_file: Invalid file name'; sys.exit(1)
- if os.path.isfile(sys.argv[2]) is False:
- print 'arcfour_key_file: Invalid file name'; sys.exit(1)
- if os.path.isfile(sys.argv[3]) is False:
- print 'twofish_key_file: Invalid file name'; sys.exit(1)
- ciphertext = open(sys.argv[1], 'rb').read()
- arcfour_key = codecs.open(sys.argv[2], 'r', 'utf-8').read().strip()
- twofish_key = codecs.open(sys.argv[3], 'r', 'utf-8').read().strip()
- arcfour_decode = decode_arc4(ciphertext, arcfour_key)
- print 'ARC4 decode: ' + str(arcfour_decode)
- twofish_decode = decode_twofish(arcfour_decode, str(twofish_key))
- print 'Twofish decode: ' + str(twofish_decode)
- except Exception as e:
- print 'Exception: ' + str(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement