Beispiel #1
0
def generate_keypair(bits):
    p = primes.generate_prime(bits / 2)
    q = primes.generate_prime(bits / 2)
    n = p * q
    # print(p)
    # print(q)
    return PrivateKey(p, q, n), PublicKey(n)
Beispiel #2
0
def generate_keypair_P(bits):
    #p = 43
    #q = 41
    #while True:
    #	i = random.randint(0,1032)
    #	p = primelist[i]
    #	j = random.randint(0,1032)
    #	q = primelist[j]
    #	if p != q:
    #		break
    p = primes.generate_prime(bits / 2)
    q = primes.generate_prime(bits / 2)
    n = p * q
    return PrivateKey_P(p, q, n), PublicKey_P(n)
Beispiel #3
0
def encrypt(pub, plain):
    # print "plain : ", plain
    while True:
        r = primes.generate_prime(long(round(math.log(pub.n, 2))))
        if r > 0 and r < pub.n:
            break

    x = pow(r, pub.n, pub.n_sq)
    # print x
    cipher = (pow(pub.g, plain, pub.n_sq) * x) % pub.n_sq
    return cipher

    #plain = plain * (-1)
    '''
  # test case
  tmp = pow(modinv(16, 225), 2, 225)
  print "test mod inv : ", tmp
  x = pow(2, 15, 225)
  cipher = (tmp * x) % 225
  '''
    '''
  tmp = pow(modinv(pub.g, pub.n_sq), abs(plain), pub.n_sq)
  x = pow(r, pub.n, pub.n_sq)
  cipher = (tmp * x) % pub.n_sq
  '''
    return cipher
Beispiel #4
0
def preprocess(pub):
    while True:
        r = primes.generate_prime(long(round(math.log(pub.n, 2))))
        if r > 0 and r < pub.n:
            break
    x = pow(r, pub.n, pub.n_sq)
    return x
def generate_keypair_R(bits):
    #p = 43
    #q = 41
    """
	while True:
		i = random.randint(0,len(primelist))
		p = primelist[i]
		j = random.randint(0,len(primelist))
		q = primelist[j]
		if p != q:
			break
	"""
    p = primes.generate_prime(bits / 2)
    q = primes.generate_prime(bits / 2)
    n = p * q
    return Keys(p, q, n)
Beispiel #6
0
def encrypt(pub, plain):
    while True:
        r = primes.generate_prime(long(round(math.log(pub.n, 2))))
        if r > 0 and r < pub.n:
            break
    x = pow(r, pub.n, pub.n_sq)
    cipher = (pow(pub.g, plain, pub.n_sq) * x) % pub.n_sq
    return cipher
Beispiel #7
0
def encrypt(pub, plain):
    while True:
        r = primes.generate_prime(long(round(math.log(pub.n, 2))))
        if r > 0 and r < pub.n:
            break
    x = pow(r, pub.n, pub.n_sq)
    cipher = (pow(pub.g, plain, pub.n_sq) * x) % pub.n_sq
    return cipher
def encrypt(pub, plain):
    pub = PublicKey(int(pub))
    while True:
        r = primes.generate_prime(int(round(math.log(pub.n, 2))))
        if r > 0 and r < pub.n:
            break
    x = pow(r, int(pub.n), pub.n_sq)
    cipher = (pow(pub.g, int(plain), pub.n_sq) * x) % pub.n_sq
    return cipher
Beispiel #9
0
def encrypt(pub, plain):
    plain = long( plain * (10**global_exp) )
    #print plain
    while True:
        r = primes.generate_prime(long(round(math.log(pub.n, 2))))
        if r > 0 and r < pub.n:
            break
    x = pow(r, pub.n, pub.n_sq)
    cipher = (pow(pub.g, plain, pub.n_sq) * x) % pub.n_sq
    return cipher
Beispiel #10
0
def encrypt(pub, plain):
    while True:
        r = primes.generate_prime(long(round(math.log(pub.n, 2))))
        if r > 0 and r < pub.n:
            break
    x = pow(r, pub.n, pub.n_sq)
# if the plaintext is a negative number, loop around n^2
    if plain < 0:
      plain = plain + pub.n
    cipher = (pow(pub.g, plain, pub.n_sq) * x) % pub.n_sq
    return cipher
Beispiel #11
0
def encrypt_P(pub, plain):
    if plain > pub.n:
        print("Condition not satisfied (0<plain<n)")
    else:
        while True:
            r = primes.generate_prime(long(round(math.log(pub.n, 2))))
            if r > 0 and r < pub.n:
                break
        k1 = pow(pub.g, plain, pub.n_sq)
        k2 = pow(r, pub.n, pub.n_sq)
        cipher = k1 * k2 % pub.n_sq
    return cipher, r
def generate_keys(iNumBits=256, iConfidence=32):
    #p is the prime
    #g is the primitve root
    #x is random in (0, p-1) inclusive
    #h = g ^ x mod p

    p = primes.generate_prime(iNumBits)
    # p = find_prime(iNumBits, iConfidence)
    g = find_primitive_root(p)
    g = modexp( g, 2, p )
    x = random.randint( 1, (p - 1) // 2 )
    h = modexp( g, x, p )

    publicKey = PublicKey(p, g, h, iNumBits)
    privateKey = PrivateKey(p, g, x, iNumBits)

    return privateKey, publicKey
Beispiel #13
0
def generate_keypair(bits, s):
    p = primes.generate_prime(bits / 2)
    q = primes.generate_prime(bits / 2)
    n = p * q
    return PublicKey(n, s), PrivateKey(n, p, q, s)
Beispiel #14
0
        self.n_sq = n * n
        self.g = n + 1

    def __repr__(self):
        return '<PublicKey: %s>' % self.n

def generate_keypair(bits):
    p = primes.generate_prime(bits / 2)
    q = primes.generate_prime(bits / 2)
    n = p * q
    return PrivateKey(p, q, n), PublicKey(n)

def encrypt(pub, plain):
	"""Encrypts a plaintext using pub pulic key"""
    while True:
        r = primes.generate_prime(long(round(math.log(pub.n, 2))))
        if r > 0 and r < pub.n:
            break
    x = pow(r, pub.n, pub.n_sq)
    cipher = (pow(pub.g, plain, pub.n_sq) * x) % pub.n_sq
    return cipher

def e_add(pub, a, b):
    """Add one encrypted integer to another"""
    return a * b % pub.n_sq

def e_add_const(pub, a, n):
    """Add constant n to an encrypted integer"""
    return a * modpow(pub.g, n, pub.n_sq) % pub.n_sq

def e_mul_const(pub, a, n):
Beispiel #15
0
def generate_keypair(bits):
    p = primes.generate_prime(bits / 2)
    q = primes.generate_prime(bits / 2)
    n = p * q
    return PrivateKey(p, q, n), PublicKey(n)
Beispiel #16
0
def getRandomModNPrime(n):
    a = 0
    while not inModNStar(a, n) and inModN(a, n):
        a = primes.generate_prime(long(round(math.log(n, 2))))
    return a
Beispiel #17
0
def generate_keypair(bits, s):
    p = primes.generate_prime(bits / 2)
    q = primes.generate_prime(bits / 2)
    n = p * q
    return PublicKey(n, s), PrivateKey(n, p, q, s)
Beispiel #18
0
def check_generate_prime(bits):
    assert primes.is_probably_prime(primes.generate_prime(bits))
def check_generate_prime(bits):
    assert primes.is_probably_prime(primes.generate_prime(bits))