Created
July 3, 2013 08:17
-
-
Save soul0592/5916298 to your computer and use it in GitHub Desktop.
Algoritmo RSA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import argv | |
from primos import rprime, pickone, inverse | |
from potmod import potmod | |
from gcd import gcdr | |
def e(m,(e,n)): | |
return potmod(m, e, n) | |
def d(c,(d,n)): | |
return potmod(c, d, n) | |
def cifrar(m): | |
cyphertxt = 0 | |
for c in m: | |
cyphertxt <<= 8 | |
cyphertxt += ord(c) | |
return cyphertxt | |
def decifrar(c): | |
mensaje = [] | |
while c > 0: | |
mensaje.insert(0, chr(c & 255)) | |
c >>= 8 | |
return ''.join(mensaje) | |
def public(d): | |
p = rprime(d) | |
q = rprime(d) | |
n = p * q | |
phin = (p - 1) * (q - 1) | |
while True: | |
e = pickone(phin) | |
if gcdr(phin, e) == 1: | |
break | |
d = inverse(e, phin) | |
return ((e, n),(d, n)) | |
def main(): | |
(publica,privada) = public(randint(1,5)) | |
#(publica,privada) = public(int(argv[1])) | |
print 'Publica = ',publica | |
print 'Privada = ',privada | |
plantxt = argv[2] | |
cifrado = cifrar(plantxt) | |
print 'Cifrado = ', cifrado | |
c = e(cifrado, publica) | |
print 'C = ',c | |
decifrado = decifrar(cifrado) | |
print 'Decifrado = ',decifrado | |
m = d(decifrado,privada) | |
print 'M = ',m | |
#main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment