# -*- coding: utf-8 -*-
"""Szyfrowanie.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/github/Conrad-Push/AGH_repo/blob/main/Cryptograhy/Szyfrowanie_do_programu.ipynb
"""

import numpy as np
import matplotlib.pyplot as plt 
import math 
from sympy import *
import random as rnd
import hashlib as hash

"""**Liniowy generator kongruencyjny**<br>
a - mnożnik<br>
c - przyrost<br>
m - moduł<br>
x0 - pierwsza liczba pseudolosowa<br>
ilosc_liczb - ilość licz pseudolosowych do wygenerowania

"""

def LCG(a,c,m,x0,ilosc_liczb):
  tab = []
  iteracja = [0]
  tab.append(x0)
 
  for i in range(0,ilosc_liczb-1):
    x = (a*tab[i] + c) % m 
    tab.append(x)
    next_i = i+1
    iteracja.append(next_i)
    
  return tab

"""**Średnia i odchylenie standardowe**"""

x = LCG(5,7,8,2,30)

y = sum(x)/30
print(y)

X = 0
for i in range(0,30):
  X += (x[i]-y)**2
X = sqrt(X/30)
print(X)

"""**Szyfr podstawieniowy**"""

def key(text, m, a, c, x0):
  Key = LCG(a,c,m,x0,len(text)+1)
  return Key

Cipher = "VPDDWLOGLKJBNTMGMOQNXPNHAAOZTPUEAW"
m = 3956280000
a = 1978141
c = 1309
x0 = 13
Key = key(Cipher,m,a,c,x0)
print(Key)

"""Deszyfrowanie"""

def decrypt(cipher, key):
  cipherA = []
  for i in cipher:
    cipherA.append(ord(i))
  decryptA = []
  t = 1
  for i in cipherA:
    a = 65 + (i - 39 - (key[t]) % 26) % 26
    decryptA.append(a)
    t += 1
  plaintext = "".join([chr(i) for i in decryptA])
  plaintext = plaintext.replace("X", " ")
  return plaintext

text = decrypt(Cipher,Key)
print(text)

"""Szyfrowanie"""

def encrypt(text,key):
  plaintext = text.replace(" ", "X")
  textA = []
  for i in plaintext:
    textA.append(ord(i))
  encryptA = []
  t = 1
  for i in textA:
    a = 65 + (i - 65 + (key[t]) % 26) % 26
    encryptA.append(a)
    t += 1
  ciphertext = "".join([chr(i) for i in encryptA])
  return ciphertext

ciphertext = encrypt(text,Key)
print(ciphertext)

"""**Hashowanie**"""

a = b"Ala ma kota"
b = b"ROD"
c = b"fajraaant"

d = hash.sha256(b).hexdigest()
print(d)
e = b"9e5b0ee018f932bceb77851b1ba997b824b562f8ae765bd8020da726efce34ca"
print(hash.sha256(e).hexdigest())

"""**RSA**

Szyfrowanie
"""

def RSA_encrypt(key_public,plaintext):
  text_padding = []
  for i in plaintext:
    text_padding.append(ord(i))

  encryptedA = []
  for i in text_padding:
    a = pow(i, key_public[0], key_public[1])
    encryptedA.append(a)

  return encryptedA

"""Deszyfrowanie"""

def RSA_decrypt(key_private,cipher):
  decryptedA = []
  for i in cipher:
    a = pow(i, key_private[0], key_private[1])
    decryptedA.append(a)
  
  plaintext = "".join([chr(i) for i in decryptedA])
  
  return plaintext

"""Zadanie 4

Odszukanie mojej listy
"""

Cipher = "MSBWKDAYPLR"
m = 75
a = 402
c = 77
x0 = 0
Key = key(Cipher,m,a,c,x0)
name = decrypt(Cipher,Key)
print(name)

"""Wczytanie listy i odszyfrowanie znaków"""

Cipher_text = [512000, 1367631, 1000000, 1481544, 1367631, 1815848, 1331000, 1771561, 32768, 1000000, 1367631, 32768, 1061208, 1367631, 1259712, 1685159, 912673, 1481544, 1225043, 1601613, 32768, 1331000, 1157625, 1030301, 32768, 941192, 1157625, 1030301, 1092727, 1259712, 32768, 1520875, 1259712, 1601613, 1092727, 32768, 1815848, 912673, 1404928, 1771561, 1560896, 912673, 970299]
key_private = [5971645067, 8957657851]

text = RSA_decrypt(key_private, Cipher_text)
print(text)

"""Hashowanie ciągu znaków"""

text = "KONRAD"
h_text = hash.sha256(text.encode()).hexdigest()
print(h_text)

"""Szyfrowanie hashu za pomocą RSA i klucza publicznego  (5, 4472424641)"""

key_public = [5, 4472424641]

cipher_hash = RSA_encrypt(key_public, h_text)
print(cipher_hash)

"""Mój klucz publiczny: (3, 9523)<br>
Klucz prywatny: (6219, 9523)
"""