예제 #1
0
def factoriza_n(e, d, n):
    assert(e!=0 and d!=0)
    factores=[]
    a=0
    de=d*e
    de=de-1
    a=am.potencia2(de)
    b=de//am.potencia_modular(2, a, n)
    x=random.randint(1, n-1)
    divisor=am.euclides_extendido(x, n)[0]
    if divisor!=1:
        return (divisor, n//divisor)

    y=am.potencia_modular(x, b, n)
    r1=(y-1)%n
    r2=(y+1)%n
    if r1==0 and r2==0:
        return "Fallo"
    while am.euclides_extendido(y, n)[0]==1:
        z=y
        y=am.potencia_modular(y, 2, n)
        r1=(y-1)%n
        r2=(y+1)%n
        if r2==0:
            return "Fallo"
        if r1==0:
            f1=am.euclides_extendido(z-1, n)[0]
            f2=am.euclides_extendido(z+1, n)[0]
            factores.append(f1)
            factores.append(f2)
            return factores
            break

    return factores
예제 #2
0
def descomposicion_ejer3(n, f1, f2):
    r1=f1+f2
    r2=abs(f1-f2)
    mcd1=am.euclides_extendido(r1, n)[0]
    if mcd1!=1:
        return (mcd1, n//mcd1)
    else:
        mcd2=am.euclides_extendido(r2, n)[0]
        if mcd2!=1:
            return (mcd2, n//mcd2)
    if am.es_primo(n, 50):
        return (n, 1)
예제 #3
0
def cifra_ax(m, c_pub):
    p=c_pub[0]
    alfa=c_pub[1]
    y=c_pub[2]
    assert (m<p)
    k=random.randint(2, p-2)
    while am.euclides_extendido(k, p-1)[0]!=1:
        k+=1
    r=am.potencia_modular(alfa, k, p)
    s=am.potencia_modular(y, k, p)
    s=s*m
    return (r,s)
예제 #4
0
def funcion_ax(p):
    if am.es_primo(p, 30):
        q=(p-1)//2
        alfa=random.randint(1, q)
        temp=am.potencia_modular(alfa, q, p)
        while(temp==1):
            alfa=random.randint(2, p-1)
            temp=am.potencia_modular(alfa, q, p)
        temp=random.randint(1, q)
        while am.euclides_extendido(temp, p-1)[0]!=1:
            temp+=1
        alfa=am.potencia_modular(alfa, temp, p)
        x=random.randint(2, p-2)
        global clave_x
        clave_x=x
        y=am.potencia_modular(alfa, x, p)
        #Clave publica: (p, alfa, y)
        res=(p, alfa, y)
        return res
    else:
        return("P: ", p, " no es primo")
예제 #5
0
def knapsack(n, u, lista, m):
    assert(n>sum(lista))
    assert(am.euclides_extendido(n, u)[0]==1)
    nlista=[(a*u)%n for a in lista] #LLave publica
    c=sum([e*a for e,a in zip(m, nlista)])
    return nlista, c
예제 #6
0
def cifrado_RSA(m, e, n):
    if am.euclides_extendido(m, n)[0]==1:
        c=am.potencia_modular(m, e, n)
        return c
    else:
        return "Mensaje m no es primo relativo con n"
예제 #7
0
def calculo_e(p, q):
    n=(p-1)*(q-1)
    e=random.randint(2, n-1)
    while am.euclides_extendido(e, n)[0]!=1:
        e=random.randint(2, n-1)
    return e