def decrypt_file( filename, keyfile, decrypted_filename = None ): if not decrypted_filename: decrypted_filename = filename.rstrip('.rsa') ciphertext = load(filename) if '\x00' == ciphertext[0]: ciphertext = ciphertext[1:] elif 'A' == ciphertext[0]: ciphertext = base64.decodestring(ciphertext[1:]) iv = load(ivfile) key = load(keyfile) plaintext = publickey.decrypt(key, ciphertext) print '[+] decrypted %s to %s...' % (filename, decrypted_filename) dump(decrypted_filename, plaintext) print '[+] file written...'
def test_crypto(): message = 'This is a test message. 0123456789ABCDEF' print '\t[+] generating a 2048-bit RSA key... ', flush() key = publickey.generate_key( 2048 ) assert( key.size() == 2047 ) print 'OK!' print '\t[+] can we encrypt with this key? ', flush() assert( key.can_encrypt() ) print 'yes' print '\t[+] encrypting \'%s\'...' % message print '\t[+] using %d-bit RSA key' % (key.size() + 1) ct = publickey.encrypt( key, message ) pt = publickey.decrypt( key, ct ) print '\t[+] decryped ciphertext as \'%s\'' % pt assert( pt == message )