import random
import util


class PublicKey(object):
    def __init__(self, e, n):
        self.e = e
        self.n = n

    def __repr__(self):
        return '{0}:{1}'.format(self.e, self.n)

    @classmethod
    def fromstring(cls, string):
        e, n = string.split(':')
        return cls(int(e), int(n))

    def encrypt(self, message):
        return util.power(message, self.e, self.n)


class PrivateKey(object):
    def __init__(self, d, n):
        self.d = d
        self.n = n

    def __repr__(self):
        return '{0}:{1}'.format(self.d, self.n)

    @classmethod
    def fromstring(cls, string):
        d, n = string.split(':')
        return cls(int(d), int(n))

    def decrypt(self, message):
        return util.power(message, self.d, self.n)


def get_key_pair(length):
    def get_e(phi):
        while True:
            x = random.randint(3, phi)
            if gcd(x, phi) == 1:
                return x

    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a

    p, q = util.get_primes(length)
    n = p * q
    phi = (p - 1) * (q - 1)
    e = get_e(phi)
    d = util.modular_inverse(e, phi)

    return PublicKey(e, n), PrivateKey(d, n)

@@ -148,37 +148,41 @@
        return '%7B
-0%7D:%7B1
+:0x%7D:%7B:1x
 %7D'.format(self.e
@@ -299,24 +299,28 @@
 ls(int(e
+, 16
 ), int(n
 ))%0A%0A    
@@ -303,32 +303,36 @@
 nt(e, 16), int(n
+, 16
 ))%0A%0A    def encr
@@ -544,13 +544,17 @@
 n '%7B
-0%7D:%7B1
+:0x%7D:%7B:1x
 %7D'.f
@@ -683,24 +683,28 @@
 ls(int(d
+, 16
 ), int(n
 ))%0A%0A    
@@ -695,16 +695,20 @@
 ), int(n
+, 16
 ))%0A%0A    

Store rsa keys as hex numbers