Пример #1
0
def decrypt(c):
	global p, q, SK
	
	if not check_state():
		return
	
	e1 = SK[1] % (p - 1)
	c1 = exp.fast(c, e1, p)
	e2 = SK[1] % (q - 1)
	c2 = exp.fast(c, e2, q)
	
	ext = gcd.extended(p, q)
	
	return (c1 * ext[4] * q + c2 * ext[3] * p) % SK[0]
Пример #2
0
def encrypt(m):
	global PK
	
	if not check_state():
		return
	
	return exp.fast(m, PK[1], PK[0])
Пример #3
0
def fermat_test(base, prime):
    p1 = prime - 1

    if exp.fast(base, p1, prime) == 1:
        return True
    else:
        return False
Пример #4
0
def dec_string():
	global SK, p, q
	
	if not check_state():
		return
	
	ctext = STR.hex_to_int(base64.b64decode(input("Encrypted message: ")))
	size = STR.modSize(SK[0])
	output = ""
	
	while ctext:
		aux3 = STR.list2Int(ctext[:size + 2])
		assert aux3 < SK[0]
		c1 = exp.fast(aux3, SK[1] % (p - 1), p)
		c2 = exp.fast(aux3, SK[1] % (q - 1), q)
		ext = gcd.extended(p, q)
		aux4 = (c1 * ext[4] * q + c2 * ext[3] * p) % SK[0]
		output += STR.int2Text(aux4, size)
		ctext = ctext[size + 2:]
	
	print("Message:")
	print(output)
Пример #5
0
def miller_rabin_test(base, n, s, d):
    neg = n - 1
    m = exp.fast(base, d, n)
    # a^d kong. 1 (mod n) akkor prím, vagy
    if m == 1:
        return True

    # r {0, 1, ..., s-1}
    else:
        r = 1
        m = exp.fast(base, d, n)

        if m == -1 or m == neg:
            return True

        while r < s:
            m = exp.fast(m, 2, n)

            if m == -1 or m == neg:
                return True

            r = r + 1

    return False
Пример #6
0
def enc_string():
	global PK
	
	if not check_state():
		return
	
	ptext = str(input("Message: "))
	size = STR.modSize(PK[0])
	output = []
	
	while ptext:
		nbytes = min(len(ptext), size - 1)
		aux1 = STR.text2Int(ptext[:nbytes])
		assert aux1 < PK[0]
		aux2 = exp.fast(aux1, PK[1], PK[0])
		output += STR.int2List(aux2, size + 2)
		ptext = ptext[size:]
	
	s = str(base64.b64encode(bytearray(STR.get_hex_string(output), 'utf8')))[2:-1]
	
	print("Encrypted message:")
	print(s)