def main(): for test_file, key, n, expected_hash in TEST_FILES: with open(os.path.join(TEST_FILES_DIR, test_file), 'rb') as f: out_stream = StringIO.StringIO() ciphersaber.process(False, f, key, n, out_stream) hash = hashlib.md5(out_stream.getvalue()).hexdigest() if hash == expected_hash: print '%s OK' % test_file else: print 'Failed to decrypt %s' % test_file print 'Expected hash: %s' % expected_hash print 'Resulting hash: %s' % hash return 1 print 'All files decrypted correctly!' return 0
def decrypt(encrypted_text, key, n=__N_DEFAULT): fin = StringIO.StringIO(base64.standard_b64decode(encrypted_text)) fout = StringIO.StringIO() ciphersaber.process(False, fin, key, n, fout) return fout.getvalue()
def encrypt(plain_text, key, n=__N_DEFAULT): fin = StringIO.StringIO(plain_text) fout = StringIO.StringIO() ciphersaber.process(True, fin, key, n, fout) return base64.standard_b64encode(fout.getvalue())