Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


Problemes II Criptografia, Ejercicios de Criptografía y Seguridad del Sistema

Problemes del tema 2 de Enginyeria de Dades assignatura criptografia

Tipo: Ejercicios

2024/2025

Subido el 04/11/2025

odette-16
odette-16 🇪🇸

2 documentos

1 / 7

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
problemes 2
March 1, 2025
Amina Aasifar El Ouahabi - 1672430
Criptografia i Seguretat
### Activity Cryptographical Mathematical Background
In these set of exercises you are required to implement the specified code described in the questions.
1. Make a program that for a given number n tells wether it is prime or not looking for factors
in [1 𝑛]
[1]: def is_prime(n):
for num in range(1,n+1):
res =n%num
#print(res)
if res !=0:
continue
else:
if num == 1 or num == n:
continue
else:
return "It's not prime :(",False
return "It's prime!! :)",True
[2]: print(is_prime(48))
("It's not prime :(", False)
2. Make a program that for a given number n tells wether it is prime or not looking for factors
in [1 𝑛/2].
[3]: def is_prime2(n):
fi =n%2
if fi != 0:
print("Odd number!")
for num in range(1, n//2):
res =n%num
#print(res)
if res !=0:
1
pf3
pf4
pf5

Vista previa parcial del texto

¡Descarga Problemes II Criptografia y más Ejercicios en PDF de Criptografía y Seguridad del Sistema solo en Docsity!

problemes 2

March 1, 2025

Amina Aasifar El Ouahabi - 1672430

Criptografia i Seguretat

Activity Cryptographical Mathematical Background

In these set of exercises you are required to implement the specified code described in the questions.

  1. Make a program that for a given number n tells wether it is prime or not looking for factors in [1 ∶ 𝑛]

[1]: def is_prime(n): for num in range( 1 ,n+ 1 ): res = n% num #print(res) if res != 0 : continue else : if num == 1 or num == n: continue else : return "It's not prime :(", False

return "It's prime!! :)", True

[2]: print(is_prime( 48 ))

("It's not prime :(", False)

  1. Make a program that for a given number n tells wether it is prime or not looking for factors in [1 ∶ 𝑛/2].

[3]: def is_prime2(n): fi = n% 2 if fi != 0 : print("Odd number!") for num in range( 1 , n// 2 ): res = n% num #print(res) if res != 0 :

continue else : if num == 1 or num == n/ 2 : continue else : return "It's not prime :("

return "It's prime!! :)"

[4]: def is_prime2(n): if n <= 1 : return "It's not prime :(" if n == 2 : return "It's prime!! :)" if n% 2 == 0: return "It's not prime :(" for num in range( 3 , n// 2 + 1 , n// 2 ): #comprovar els números imparells if n% num == 0: return "It's not prime :(", False return "It's prime!! :)", True

#// -> entero / -> flotante

[5]: print(is_prime2( 3 ))

("It's prime!! :)", True)

  1. Make a program that for a given number n tells wether it is prime or not using Fermat’s theorem.

[6]: from random import randint

#k --> al número d'iteracions per verificar la primalitat

def fermat(n,k): if n <= 3 : return (str(n)+' is prime! :)', True ) for i in range(k): a = randint( 2 , n- 2 ) if (a**(n- 1 ))% n != 1: return (str(n)+' is not prime :(', False ) return (str(n)+' is prime! :)', True )

#probability = 1-(1/2)**k #return str(n)+' is prime with a probability of'+ str(probability)

[7]: print(fermat( 561 , 10 ))

60 sec completed! Results: [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109]

[10]: #tercer programa -> primer per fermrat import time

ll_prime2 = [] times2 = []

initial_time = time.time()

n = 1 while time.time() - initial_time < 60 : temps_actual = time.time() prime = fermat(n, 10 ) if prime[- 1 ] == True : #the last value ll_prime2.append(n) elapsed_time = time.time() - temps_actual times2.append(elapsed_time) time.sleep( 2 ) n+= 1

print("60 sec completed! Results:", ll_prime)

60 sec completed! Results: [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109]

  1. Draw a graph (x-axis number, y-axis time) using both programs to calculate the time spent for every number.

[11]: import matplotlib.pyplot as plt

#primer algoritme -> primer per n plt.bar(ll_prime, times, color='blue') plt.xlabel("Nombres primers trobats") plt.ylabel("Temps emprat (s)") plt.title("Temps de càlcul de cada número primer (primer algoritme)") plt.xticks(rotation= 45 ) plt.grid(axis='y', linestyle='--', alpha=0.7) plt.show()

[12]: import matplotlib.pyplot as plt

#primer algoritme -> primer per n/ plt.bar(ll_prime1, times1, color='blue') plt.xlabel("Nombres primers trobats") plt.ylabel("Temps emprat (s)") plt.title("Temps de càlcul de cada número primer (segon algoritme)") plt.xticks(rotation= 45 ) plt.grid(axis='y', linestyle='--', alpha=0.7) plt.show()