-
-
Save marioalverto/26effe40083e815fa548 to your computer and use it in GitHub Desktop.
rsa python
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
import socket | |
import rsa | |
s = socket.socket() | |
s.connect(("127.0.0.1", 9999)) | |
#Generar llaves propias | |
rsa.arch_key(10, "cli") | |
publicServ = rsa.get_key('servpub.key') | |
privateCli = rsa.get_key('clipriv.key') | |
while True: | |
mensaje = raw_input(" Ingresa Mensaje: ") | |
publicServ = rsa.get_key('servpub.key') | |
s.send(str(rsa.E(rsa.cifrar(mensaje), publicServ))) | |
print "Mensaje encriptado enviado: %s" %(str(rsa.E(rsa.cifrar(mensaje), publicServ))) | |
if mensaje == "quit": | |
break | |
print "adios" | |
s.close() |
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
#!/usr/bin/python | |
from sys import argv | |
def gcdit(a, b): | |
while True: | |
c = (a%b) | |
if c == 0: | |
return a | |
(a, b) = (b, c) | |
def gcdrec(a,b): | |
c = a % b | |
if c == 0: | |
return (b,0,1) | |
(r,x,y) = gcdrec(b,c) | |
return (r,y, x - (y * (a/b))) | |
def gcd(a,b): | |
if a%b == 0: | |
return b | |
return gcd(b, a%b) | |
def egcd(a,b): | |
c = a % b | |
if c == 0: | |
return (b,0,1) | |
(r,x,y) = egcd(b,c) | |
return (r,y, x - (y * (a/b))) |
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
#!/usr/bin/python | |
from sys import argv | |
from potmod import potmod | |
from gcd import gcd, egcd | |
from randprime import rprime,pickone,inversa | |
def E(m,(e,n)): | |
return potmod(m,e,n) | |
def D(c,(d,n)): | |
return potmod(c,d,n) | |
# Codigo extraido de http://pastebin.com/ypg2PXt2 | |
# Convierte un string en un numero | |
def msg2num(s): | |
n = 0 | |
for c in s: | |
n <<= 8 | |
n += ord(c) | |
return n | |
# Convierte un numero en un string | |
def num2msg(n): | |
s = [] | |
while n > 0: | |
s.insert(0, chr(n & 255)) | |
n >>= 8 | |
return ''.join(s) | |
def cifrar(m): | |
chipertext = 0 | |
for c in m: | |
chipertext <<= 8 | |
chipertext += ord(c) | |
return chipertext | |
def decifrar(c): | |
mensaje = [] | |
while c > 0: | |
mensaje.insert(0, chr(c & 255)) | |
c >>= 8 | |
return ''.join(mensaje) | |
# Fin de codigo extraido | |
def arch_key(size, name): | |
(publica, privada) = key_gen(size) | |
pub = open(name+'pub.key', 'w') | |
priv = open(name+'priv.key', 'w') | |
for i in range(len(publica)): | |
pub.write(str(publica[i])+'\n') | |
priv.write(str(privada[i])+'\n') | |
return | |
def get_key(archivo): | |
f = open(archivo, 'r') | |
e = int(f.readline()) | |
n = int(f.readline()) | |
return (e, n) | |
def key_gen(d): | |
p = rprime(d) | |
q = rprime(d) | |
n = p * q | |
phin = (p-1) * (q-1) | |
while True: | |
e = pickone(phin) | |
if gcd(phin, e) == 1: | |
break | |
d = inversa(e, phin) | |
return ((e, n), (d, n)) |
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
import socket | |
import rsa | |
s = socket.socket() | |
s.bind(("127.0.0.1", 9999)) | |
s.listen(1) | |
sc, addr = s.accept() | |
#generar llaves propias | |
rsa.arch_key(10, "serv") | |
public = rsa.get_key('servpub.key') | |
private = rsa.get_key('servpriv.key') | |
while True: | |
recibido = sc.recv(1024) | |
if recibido == "quit": | |
break | |
print "Mensaje Encriptado Recibido: ", recibido | |
print "Mensaje Desencriptado: %s" %(rsa.decifrar(rsa.D(int(recibido), private))) | |
sc.send(recibido) | |
print "adios" | |
sc.close() | |
s.close() |
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
#!/usr/bin/python | |
from random import randint | |
from gcd import egcd | |
from math import sqrt | |
def prime(n): | |
if n <=1: | |
return False | |
if n % 2 == 0: | |
if n == 2: | |
return True | |
else: | |
return False | |
i = 3 | |
while i <= sqrt(n): | |
if n % i == 0: | |
return False | |
i += 2 | |
return True | |
def inversa(e, n): | |
(gcd,tmp,d) = egcd(e, n) | |
if not gcd == 1: | |
return None | |
else: | |
return (tmp+n) %n | |
def rprime(digits): | |
minimum = 10**(digits-1) | |
maximum = minimum * 10 - 1 | |
while True: | |
r = randint(minimum,maximum) | |
if prime(r): | |
return r | |
def checkgen(n,mod): | |
t = 1 | |
c = 0 | |
while not t == 1 or c == 0: | |
t = (t*n) % mod | |
c+=1 | |
if c == mod: | |
break | |
if c == mod - 1: | |
return True | |
else: | |
return False | |
def findgen(mod): | |
while True: | |
cand = randint(2,mod -1) | |
if checkgen(cand,mod): | |
return cand | |
def pickone(mod): | |
return randint(2,mod-1) | |
def potmod(x, pot, mod): | |
bits = pot | |
pot | |
res = 1 | |
temp = x | |
while bits > 0: | |
if bits %2 == 1: | |
res = (res * temp % mod) | |
temp = (temp * temp) % mod | |
bits = bits >> 1 | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment