# Exámen de Ciberseguridad. Sección Criptografía.
>[name=Yanis Ouakrim]
## 1 - Bajo el concepto de un criptosistema, define un cifrador de flujo.
According to the definition given by cryptologist Bruce Schneider, a cryptosystem is a term used in cryptography to designate a set of cryptographic algorithms and all plain text, encrypted texts and possible keys.
A stream cipher is a symmetric key cipher where plaintext digits are combined with a pseudorandom cipher digit stream. In a digit stream, each plaintext digit is encrypted one at a time with the corresponding digit of the keystream, to give a digit of the ciphertext stream.
## 2 - Encuentra las llaves involutorias
We obtain the involutive keys 0, and 13, they allow to find the initial text with the same key when encrypting the decryption.
## 3 - Elementos invertibles en $Z_m$ para $m = 28, 33$ y $35$
In $Z_m$ an $k$ is invertible if and only if $k<m$ and $gcd(k,n)=1$. I wrote the following python code:
```python
def involutory_elements(m):
primes = []
for i in range(0,m):
if gcd(i, m)==1:
primes.append(i)
return primes
```
and I obtained the following results:
```
In[2]: involutory_elements(28)=
Out[2]: [1, 3, 5, 9, 11, 13, 15, 17, 19, 23, 25, 27]
```
```
In[3]: involutory_elements(33)
Out[3]: [1, 2, 4, 5, 7, 8, 10, 13, 14, 16, 17, 19, 20, 23, 25, 26, 28, 29, 31, 32]
```
```
In[4]: involutory_elements(35)
Out[4]: [1, 2, 3, 4, 6, 8, 9, 11, 12, 13, 16, 17, 18, 19, 22, 23, 24, 26, 27, 29, 31, 32, 33, 34]
```
## 4 - Cifrador de Hill
First we convert every letter to its postion in the alphabet, starting at 0:

We're going to encode the plain text "soyunestudiante". After transforming with the table, we have:
```
s : 18 ;
o : 14 ;
y : 24 ;
u : 20 ;
n : 13 ;
e : 4 ;
s : 18 ;
t : 19 ;
u : 20 ;
d : 3 ;
i : 8 ;
a : 0 ;
n : 13 ;
t : 19 ;
e : 4 ;
````
Now, we create couple of letters, if the number of letter is odd, we add a random letter. We get:
```
(18, 14), (24, 20), (13, 4), (18, 19), (20, 3), (8, 0), (13, 19), (4, 2)
```
Then, we compute the dot product of every tuple with the A matrix we use mod26 to obtain letter codes and we turn every number into a letter, we obtain the following text:
```
cyseuhbxdnqureqs
```
I wrote the following code to compute the ciphered text:
```python=
import string
import random
import numpy as np
def cypher(text, a):
letter_codes = []
letter_couples = []
for letter in text:
letter_codes.append(string.ascii_lowercase.index(letter))
for i in range(0, len(text), 2):
if i+1>(len(letter_codes)-1):
letter_couples.append(np.array([letter_codes[i],random.randrange(26)]))
else:
letter_couples.append(np.array([letter_codes[i],letter_codes[i+1]]))
final_code=[]
for couple in letter_couples:
final_code.append(np.dot(couple,a))
final_text = ''
for coupe_codes in final_code:
final_text += chr(ord('a') + (coupe_codes[0]%26))
final_text += chr(ord('a') + (coupe_codes[1]%26))
return final_text
```
```python
In[2]: cypher("soyunestudiante", np.array([[2,9],[5,5]]))
Out[2]: 'cyseuhbxdnqureqs'
```
### Inverse Matrix
Matrix A must be reversed. It is enough to take the transposition of its adjugate matrix
and multiply it (modulo 26) by a modular inverse of the determinant of A. After doing so, we obtain:
$\begin{pmatrix}-15 & 15 \\ 27 & -6 \end{pmatrix}$
## 5 - Cifrador Afìn
The involutory key makes the encypher and the decypher the same, if K is an involutory key of an affine cypher, then:
$e(x)=d(x)$
$(ax+b)\mod(n)=(a^{-1}(x-b))\mod(n)$
$=(a^{-1}x-a^{-1}b)\mod{n}$
We obtain : $a = a^{-1} \mod{n}$
$b=-a^{-1}b$
$a=-1$
$a+1=0$
$b(a+1)=0$
$0\mod n = 0$
Therefore, we have:
$b(a+1)\equiv0\mod n$
We proved that if the key a, b was involutory in the case of an affine cypher, $b(a+1)\equiv0\mod n$ and $a = a^{-1} \mod{n}$.
## 6 - Determinar el valor de b
1. I obtain 603, and as general solution:
$b = 1025 n + 603$ and $n$ element $Z$
2. I obtain two solutions:
$b = 0, x = 0$ and $b = 3, x = 1$
## 7 - Application scenario
Online purchases are more and more numerous, it is essential to ensure the security of online payments. We will show that online payment services need to respect the following cubersecurity rules: confidentiality, integrity, authentication and non-repudiation.
* __Confidentiality__: it is essential that the bank data used to make the payment be confidential
* __Integrity__: the issuer and the recipient of the transaction cannot be modified, nor the amount of the transaction.
* __Authentication__: the system must be able to ensure that the person carrying out the transaction is who he or she claims to be by means of authentication, for example by means of a password or biometric sensor
* __Non-repudiation__: Neither party should be able to deny the transaction, if a payment has been made, neither the company nor the customer can question the transaction happened.