from Crypto.Cipher import CAST
import sys
import argparse
 
 
def main():
 
	parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
	parser.add_argument("-e", "--encrypt", help="encrypt carbon file", required=False)
	parser.add_argument("-d", "--decrypt", help="decrypt carbon file", required=False)
 
	try:
		args = parser.parse_args()
	except IOError as e:
		parser.error(e)
		return 0
 
	if len(sys.argv) != 3:
		parser.print_help()
		return 0
 
	key = b"\x12\x34\x56\x78\x9A\xBC\xDE\xF0\xFE\xFC\xBA\x98\x76\x54\x32\x10"
	iv = b"\x12\x34\x56\x78\x9A\xBC\xDE\xF0"
 
	cipher = CAST.new(key, CAST.MODE_OFB, iv)
 
	if args.encrypt:
		plaintext = open(args.encrypt, "rb").read()
		while len(plaintext) % 8 != 0:
			plaintext += b"\x00"
		data = cipher.encrypt(plaintext)
		open(args.encrypt + "_encrypted", "wb").write(data)
	else:
		ciphertext = open(args.decrypt, "rb").read()
		while len(ciphertext) % 8 != 0:
			ciphertext += b"\x00"
		data = cipher.decrypt(ciphertext)
		open(args.decrypt + "_decrypted", "wb").write(data)
 
if __name__ == "__main__":
	main()