Пример #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!'
Пример #2
0
def test_keygen():
    print '\t[+] generating 1024 bit RSA key... ',
    flush()
    key = publickey.generate_key(size = 1024)
    assert( key.size() == 1023 )
    print 'OK!'

    print '\t[+] generating 2048 bit RSA key... ',
    flush()
    key = publickey.generate_key(size = 2048)
    assert( key.size() == 2047 )
    print 'OK!'

    print '\t[+] generating 4096 bit RSA key... ',
    flush()
    key = publickey.generate_key(size = 4096)
    assert( key.size() == 4095 )
    print 'OK!'
Пример #3
0
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 )
Пример #4
0
def gen_key(filename, size):
    key = publickey.generate_key(size)

    print '\t[+] generated a %d-bit RSA key' % (key.size() + 1)
    publickey.export_key( filename, key )
    print '\t[+] exported private key to %s' % filename