def main(): textoEnClaro = input("Introduzca el mensaje a cifrar: ") p = int(input("Introduzca el parámetro p: ")) q = int(input("Introduzca el parámetro q: ")) d = int(input("Introduzca el parámetro d: ")) rsa(textoEnClaro, p, q, d)
def create_keys(self,f="k.out",gen=1): #get,or generate keys if gen is 1: #generate keys and save to f x=rsa() r=x.rand_prime(860) #get random prime on 860b ok=0 while ok is 0: q=x.rand_prime(160) #get random prime on 160b p=2*q*r+1 ok=x.miller_rabin(p,10) #until p=2qr+1 is prime ok=0 while ok is 0: betta=x.rand(1024)%p #get random betta if pow(betta,(p-1)/r,p) is not 1: if pow(betta,(p-1)/q,p) is not 1: if pow(betta,(p-1)/2,p) is not 1: ok=1 alfa=pow(betta,(p-1)/q,p) #calculte alfa self.a=randrange(1,q) #get random signing key betta=pow(alfa,self.a,p) #calculate betta file=open(f,"w+") #save to file self.keys=[p,q,alfa,betta] pickle.dump(self.keys,file) pickle.dump(self.a,file) else: file=open(f,"r") #load from file self.keys=pickle.load(file) #load public keys self.a=pickle.load(file) #load private key
def sign_message(self,m): #sign message k=self.keys a=self.a x=rsa() h=hash(m) #digest messasge d=h.digest() r=randrange(0,k[1]) #get random r from (0,q) ri=x.imod(r,k[1]) #get modular inverse of r gamma=pow(k[2],ri,k[0])%k[1] #sign message delta=(r*(d+a*gamma))%k[1] ret=[gamma,delta] #return signed message return ret
def verify_message(self,m,s,k): #verify message, s-signature,k-public keys x=rsa() h=hash(m) #digest message d=h.digest() inv=x.imod(s[1],k[1]) #modular inverse of delta e1=(d*inv)%k[1] e2=(s[0]*inv)%k[1] o1=pow(k[2],e1,k[0]) o2=pow(k[3],e2,k[0]) o=((o1*o2)%k[0])%k[1] print "? - ",s[0],"==",o #test validity if s[0] == o: print "valid" return 1 else: print "invalid" return 0
] if sim_measure == "cka": similarities_of_step = [ kernel_CKA(np.reshape(s, (N, -1)), np.reshape(r, (N, -1))) for s, r in zip(start, representations) ] similarities_of_step_prev = [ kernel_CKA(np.reshape(s, (N, -1)), np.reshape(r, (N, -1))) for s, r in zip(prev, representations) ] else: print(np.mean(start[0]), np.mean(representations[0])) similarities_of_step = [ rsa( np.array( [np.reshape(s, (N, -1)), np.reshape(r, (N, -1))]), sim_measure) for s, r in zip(start, representations) ] similarities_of_step_prev = [ rsa( np.array( [np.reshape(s, (N, -1)), np.reshape(r, (N, -1))]), sim_measure) for s, r in zip(prev, representations) ] similarities.append(similarities_of_step) similarities_prev.append(similarities_of_step_prev) prev = representations.copy()
def test_rsa1(self) : resultat = rsa(101, 103, 10331, 7) self.assertEqual(resultat, 964)
import rsa rsa = rsa() #Cotés Destinataire qui posséde sa clés privée. rsa.decrypt(['80488','2826','241808','183218','45154','84918'])
from rsa import * print "Alice" Alice=rsa(1,0,512) print "Alice is ready" print "Bob" Bob=rsa(1,1,512) print "Bob is ready" print "Bob requests Alice's public key" public=Alice.request_public_key() c=Bob.encrypt_message(55778896654455662555544488779987782554,public) print "Encrypted: ",c print "Alice decrypts message with her private key" print "Decrypted: ",Alice.decrypt_message(c) print "\n\n" p,q=Alice.wiener(Bob.request_public_key()) print "------------------------------------------------------------------------" print "Weiner deduced keys" print "p=",p print "q=",q print "------------------------------------------------------------------------" print "Bob's original keys" p,q=Bob.get_keys() print "p=",p print "q=",q ##
try: rotKey = int(input("Write the key: ")) wtk(rotKey) except ValueError: print("Only numbers!"); raise SystemExit def encryptDecrypt(mode, message, key, final=""): for symbol in message: if mode == 'E': final += chr((ord(symbol) + key - 13) % 26 + ord('A')) else: final += chr((ord(symbol) - key - 13) % 26 + ord('A')) return final wtf(encryptDecrypt(cryptMode, startMessage, rotKey)) print("Final message:", encryptDecrypt(cryptMode, startMessage, rotKey)) cryptMode = input("[A]es|[R]sa|[C]aesar: ").upper() if cryptMode not in ['A', 'R', 'C']: print("Error: mode is not Found!"); raise SystemExit if cryptMode in ['A', 'a']: aes() if cryptMode in ['R', 'r']: rsa() if cryptMode in ['C', 'c']: caesar()
from rsa import * import numpy as np import cv2 as cv import ctypes obj = rsa() img = cv.imread("green.jpg", cv.IMREAD_COLOR) height, width, channels = img.shape temp = np.zeros((height, width, 3), np.int64) for a in range(0, height): for b in range(0, width): x, y, z = img[a][b] x = np.int(x) y = np.int(y) z = np.int(z) x = obj.Cipher(x) y = obj.Cipher(y) z = obj.Cipher(z) temp[a][b] = x, y, z cv.imshow("image", temp) cv.waitKey(0) cv.destroyAllWindows() temp1 = np.zeros((height, width, 3), np.uint8) for a in range(0, height): for b in range(0, width): x, y, z = temp[a][b]
def decode_int(i): v = hex(long(i))[2:-1] if len(v) & 1: v = '0' + v return v.decode('hex') def extract_bits(priv, pub, cipher): N = pub[1] c2 = rsa.encrypt(pub, 2) cipher = (cipher * c2) % N for _ in range(1024): yield oracul(priv, cipher) cipher = (cipher * c2) % N def main() pub, priv = rsa(1024) N = pub[1] cipher = rsa.encrypt(pub, plain_i) lo, hi = 0, N for b in extract_bits(priv, pub, cipher): mid = (lo + hi) / 2 if b == 1: lo = mid else: hi = mid print decode_int(hi) main()