This commit is contained in:
2022-03-11 21:57:26 +01:00
parent f611388ba7
commit 79c12a6ed4
10 changed files with 160 additions and 1 deletions

3
BrutForce.py Normal file
View File

@@ -0,0 +1,3 @@
from chiffrement import *
from readbytetest import *
readByteFile('data')

65
Main V3.py Normal file
View File

@@ -0,0 +1,65 @@
def cryptByteFile(filenameIn: str, keya: int, keyb: int, filenameOut = 'result') -> None:
""" Ecrit dans filenameOut, filenameIn crypté avec le chiffrement affine
"""
fdin = open(filenameIn, "rb")
fdout = open(filenameOut, "ab")
data = fdin.read(1)
while data != b'':
fdout.write(chiffrementAffineByte(data, keya, keyb))
data = fdin.read(1)
fdin.close()
fdout.close()
def chiffrementAffineByte(data: bytes, keya: int, keyb: int) -> bytes:
""" Renvoie le byte data crypté avec le chiffrement affine
"""
alphabet = []
for i in range(256):
alphabet.append(i)
x=alphabet.index(int.from_bytes(data, "little"))
y=(keya*x+keyb)%256
return bytes([alphabet[y]])
def inverse(a):
""" Renvoie l'inverse du module de a et x par 256
"""
x=0
while (a*x%256!=1):
x += 1
return x
def dechiffrementAffineByte(data: bytes, keya: int, keyb: int) -> bytes:
""" Renvoie le byte data décrypté avec le chiffrement affine
"""
alphabet = []
for i in range(256):
alphabet.append(i)
x=alphabet.index(int.from_bytes(data, "little"))
y=(inverse(keya)*(x-keyb))%256
return bytes([alphabet[y]])
def decryptByteFile(filenameIn: str, keya: int, keyb: int, filenameOut = 'result') -> None:
""" Ecrit dans filenameOut, filenameIn décrypté avec le chiffrement affine
"""
fdin = open(filenameIn, "rb")
fdout = open(filenameOut, "ab")
data = fdin.read(1)
while data != b'':
fdout.write(dechiffrementAffineByte(data, keya, keyb))
data = fdin.read(1)
fdin.close()
fdout.close()
#bytes([1])
#Jeu de test
print(chiffrementAffineByte(b'\xd8', 1, 3))
#cryptByteFile('tescrypt.txt', 1, 3, "resluttest")
#cryptByteFile('data', 1, 3)
#cryptByteFile('monkey.jpg', 1, 3)
print(dechiffrementAffineByte(b'\xd8', 1, 3))
decryptByteFile('resluttest', 1, 3, "decryptresult")
#decryptByteFile('result', 1, 3, "decryptmonkey.jpg")

View File

@@ -1 +1,2 @@
Hello
This project is a school project

View File

@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}

72
chiffrement.py Normal file
View File

@@ -0,0 +1,72 @@
#R = machin.read(1)
#if not R = \00
def pgcd(a,b):
while b!=0:
a,b=b,a%b
return a
def chiffrementAffine(a,b,L):
alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
x=alphabet.index(L)
y=(a*x+b)%26
return alphabet[y]
def inverse(a):
x=0
while (a*x%26!=1):
x=x+1
return x
def dechiffrementAffine(a,b,L):
alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
x=alphabet.index(L)
y=(inverse(a)*(x-b))%26
return alphabet[y]
def crypt(M,a,b):
if (pgcd(a,26)==1):
mot = []
for i in range(0,len(M)):
mot.append(chiffrementAffine(a,b,M[i]))
return "".join(mot)
else:
return "Chiffrement impossible. Veuillez choisir un nombre a premier avec 26."
def decrypt(M,a,b):
if (pgcd(a,26)==1):
mot = []
for i in range(0,len(M)):
mot.append(dechiffrementAffine(a,b,M[i]))
return "".join(mot)
else:
return "Déchiffrement impossible. Le nombre a n'est pas premier avec 26."
def readByteFile(A: str):
with open(A, "rb") as f:
with open("result.txt", "a+") as o:
byte = f.read(1)
while (byte := f.read(1)):
o.write(str(byte))
def decouper(F: str):
fd = open("result.txt", "r").readlines()
for line in fd:
line = line.sp
def cryptFichier(A: str, a,b):
X = openBytesFile(A)
fdout = open("resulttest.txt", "wb")
for i in range(len(X)):
fdout.write(X[i])
crypt(fdout, a, b)
def openBytesFile(A: str):
opened = []
fdin = open(A, "rb")
data = fdin.read(1)
while data != b'':
opened.append(data)
data = fdin.read(1)
fdin.close()
return opened
print(chiffrementAffine(1, 3, 'P'))
print(crypt("JAIMELESFRITESALAFRITEUSES", 1, 3))
print(openBytesFile("testcrypt.txt"))

1
data Normal file

File diff suppressed because one or more lines are too long

3
decryptresult Normal file
View File

@@ -0,0 +1,3 @@
fichier test lalalla
aa
ahouhda

BIN
monkey.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

3
resluttest Normal file
View File

@@ -0,0 +1,3 @@
ilfklhu#whvw#ododood
dd
dkrxkgd

3
tescrypt.txt Normal file
View File

@@ -0,0 +1,3 @@
fichier test lalalla
aa
ahouhda