Example #1
0
def test_key_export():
    print '\t[+] generating 2048-bit RSA test key... ',
    flush()
    masterkey   = publickey.generate_key( 2048 )
    print 'OK!'
    
    print '[+] exporting keypair to disk...'
    publickey.export_keypair( 'publickey_tests', masterkey )

    print '\t[+] attempting to reload keypair from disk... ',
    pubkey      = publickey.load_key( 'publickey_tests.pub' )
    prvkey      = publickey.load_key( 'publickey_tests.prv' )

    assert( prvkey.has_private() )
    assert( not pubkey.has_private() )

    print 'OK!'

    assert( pubkey == prvkey.publickey() )
    print '\t[+] cleaning up... ',

    try:
        os.unlink('publickey_tests.pub')
        os.unlink('publickey_tests.prv')
    except OSError as e:
        print 'FAILED!'
        print '\t[!] failed cleanup: %s' % e

        # failure to cleanup isn't a failure of the crypto library
        return
    else:
        print 'OK!'
        print '\t[+] cleanup successful!'
Example #2
0
def encrypt_file(filename, keyfile, crypted_filename = None, 
                 ascii_armour = False):
    if not crypted_filename:
        crypted_filename    = filename + '.rsa'

    plaintext               = load(filename)
    key                     = publickey.load_key(keyfile)
    ciphertext              = publickey.encrypt(key, plaintext)

    if not ascii_armour:
        ciphertext          = '\x00' + ciphertext
    else:
        ciphertext          = 'A' + base64.encodestring(ciphertext)

    print '[+] encrypted %s to %s...' % (filename, crypted_filename)
    dump(crypted_filename, ciphertext)
    print '[+] file written...'