import sys
#from pdb import set_trace as trace
import binascii

import SSLCrypto

def myCallback(type, num):
    if type == -1:
        print
    elif type == 0:
        sys.stdout.write(".")
    elif type == 1:
        sys.stdout.write("+")
    else:
        sys.stdout.write("*")
    sys.stdout.flush()

print "Seeding random number generator"
SSLCrypto.randomseed("Here is some semi-random data")

print "Generating RSA objects"
rsaAlice = SSLCrypto.rsa()
rsaBob = SSLCrypto.rsa()
rsaAlice2Bob = SSLCrypto.rsa()
rsaBob2Alice = SSLCrypto.rsa()

print "Generating key for Alice"
#rsaAlice.generateKey(2048, 3, myCallback)
#pubAlice = rsaAlice.pubKey() # get Alice's pubkey

print "Generating key for Bob"
rsaBob.generateKey(4096, 3, myCallback)
pubBob = rsaBob.pubKey() # get Bob's pubkey

print "Exchanging public keys"
rsaAlice2Bob.importPubKey(pubBob)
#rsaBob2Alice.importPubKey(pubAlice)

#print "Encrypting from Alice to Bob - max size=%d" % rsaAlice2Bob.maxSize()
plain2Bob0 = "Hello there, Bob."
print "Plaintext:"
print repr(plain2Bob0)
cipher2Bob = rsaAlice2Bob.encrypt(plain2Bob0)
print "Ciphertext:"
print repr(cipher2Bob)
plain2Bob1 = rsaBob.decrypt(cipher2Bob)
print "Decrypted Ciphertext"
print repr(plain2Bob1)
