From 73ad7426ee1f1b1b31f252ea30d16a810272d792 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Fri, 12 Feb 2021 12:06:30 +0100 Subject: [PATCH] chiffrement matrice ok --- .../ciphering/ciphering.component.html | 60 ++++++ .../ciphering/ciphering.component.ts | 174 ++++++++++++++---- 2 files changed, 203 insertions(+), 31 deletions(-) diff --git a/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.html b/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.html index d1a44de4..435fb44c 100644 --- a/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.html +++ b/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.html @@ -2,6 +2,66 @@

Essais de chiffrement pour un framadate zéro knoledge

+
+

+ Chiffrement par matrice +

+ Voici un texte que l'on voudrait chiffrer:
+
+ Vous pouvez le changer, la page se met à jour automatiquement. Le texte clair fait + {{ plainText.length }} caractères. +
+ Nous avons besoin d'une matrice de codage de 4 nombres. +
+ + +
+ +
+ Et voilà la résultat: + + + {{ cipheredTextCIL }} + + +

Comment on a fait?

+ + Voici notre alphabet autorisé ( {{ alphab.length }} caractères): +
+			{{ alphab }}
+		
+ Tout caractère qui n'est pas présent dans notre alphabet sera converti en souligné "_". +
+ Notre texte après vérification : {{ filterCharacters(plainText) }} +
+ On sépare le texte en {{ separatedCouples.length }} couples de caractères pour le mettre dans une matrice. On + prend la pilule rouge. + + +
+ + +
+					{{ c | json }}
+				
+
+
+		separatedCouples :
+			{{ separatedCouples | json }}
+		
+

1) Chiffrement simple

diff --git a/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts b/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts index 17a5cd0e..e29193fa 100644 --- a/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts +++ b/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts @@ -12,20 +12,30 @@ import CipherLib from './lib_cipher'; styleUrls: ['./ciphering.component.scss'], }) export class CipheringComponent implements OnInit { - public plainText = 'le texte à chiffrer, coucou !'; + public plainText = 'coucou !'; // public plainText = '1234'; public cipheredText = '121-127-42-110-127-42-122-121-115-128-124-111-125-107-118-127-126-42-118-111-125-42-122-115-122-121-118-111'; - algorithm = 'aes-192-cbc'; + public algorithm = 'aes-192-cbc'; public salt = 'ou du poivre heh'; public initial_vector = ''; public key; public otherCipheredText = 'le texte à chiffrer, coucou !'; + public alphab: string; + public prem: number; + public dico: string[]; + public separatedCouples: any[]; + public codingMatrice: string = '2 3 5 8'; + private cipheredTextCIL: string; - constructor() {} + constructor() { + this.alphab = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .+-*/_!?,éèêëàçôî'âù()@&$"; + this.prem = this.alphab.length; + this.dico = this.alphab.split(''); + } ngOnInit(): void { - this.encrypt(this.plainText); + this.encrypt(this.codingMatrice, this.plainText); } // ---------------------- make unreadable ---------------------- @@ -36,15 +46,19 @@ export class CipheringComponent implements OnInit { get simpleCipheredTextWithSalt() { return this.convertTextToInt(this.salt + this.plainText); } + get simpleCipheredTextWithSpreadSalt() { return this.convertTextToIntMixedSalt(this.salt + this.plainText); } + get simpleCipheredTextWithHashedSalt() { return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText); } + get simpleCipheredTextWithHashedSpreadSalt() { return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText); } + get layerOnSpread() { return this.convertIntToText(this.simpleCipheredTextWithHashedSalt, false); } @@ -57,40 +71,137 @@ export class CipheringComponent implements OnInit { get simpleDeCipheredTextWithSalt() { return this.convertIntToText(this.salt + this.cipheredText); } + get simpleDeCipheredTextWithSpreadSalt() { return this.convertIntToTextMixedSalt(this.salt + this.cipheredText); } // ---------------------- conversions ---------------------- - encrypt(someText) { - // this.key = Crypto.scryptSync(someText, 'salt', 24); - // this.initial_vector = Crypto.randomBytes(16); - // - // const cipher = Crypto.createCipheriv(this.algorithm, this.key, this.initial_vector); - // - // cipher.on('readable', () => { - // this.cipheredText = cipher.read().toString('hex'); - // console.log(cipher.read().toString('hex')); - // }); - // - // - // this.cipheredText = someText; - // // outputs encrypted hex - // console.log(this.cipheredText); - return someText; + + /** + * chiffrer avec la méthode du CIL-gometz + * avec une matrice + * @param someText + */ + encrypt(matriceDefinition, messageToCipher) { + if (!matriceDefinition || !messageToCipher) { + return null; + } + let cipheredText = ''; + const lcpl = this.separecpl(this.filterCharacters(messageToCipher)); + this.separatedCouples = lcpl; + console.log('lcpl', lcpl); + console.log('this.dico', this.dico); + /** + * Pour chaque couple de caractères + * on crée le couple d'indices de ces caractères + * On applique la matrice au couple d'indices + * et on en déduit le couple de caractères codés (ou décodés) + * qu'on accumule dans la chaine msgout + */ + lcpl.forEach((couple) => { + console.log('couple', couple); + const cplnum = [this.dico.indexOf(couple[0]), this.dico.indexOf(couple[1])]; + console.log('cplnum', cplnum); + const cplnumout = this.applique(matriceDefinition, cplnum); + console.log('cplnumout', cplnumout); + const cplout = [this.alphab[cplnumout[0]], this.alphab[cplnumout[1]]]; + console.log('cplout', cplout); + cipheredText += cplout[0]; + cipheredText += cplout[1]; + }); + console.log('cipheredText', cipheredText); + this.cipheredTextCIL = cipheredText; + return cipheredText; } + /** + * séparation d'une chaine de caractères en liste de couples de caractères + * @param msg + */ + separecpl(msg) { + /** + * Produit une liste formée des couples de caractères de la chaine + msg reçue en argument. + Dans le cas où le nb de caractères de la chaine est impair, + un underscore est ajouté en fin de chaine de façon à pouvoir + inclure le dernier caractère dans un couple. + */ + const nbcar = msg.length; + if (nbcar % 2) { + msg += '_'; + } + const coupleList = []; + let i = 0; + while (i < nbcar) { + coupleList.push([msg[i], msg[i + 1]]); + i += 2; + } + return coupleList; + } + + /** + * calcul de l'inverse de d dans Z/pZ + * @param d + * @param p + */ + invdet(d, p) { + let inv = 1; + while ((d * inv) % p !== 1) { + inv++; + } + return inv; + } + + /** + * inversion de matrice + * @param matriceString + * @param p + */ + invmat(matriceString: string, p) { + const [a, b, c, d] = this.convertStringToMatrix(matriceString); + const mul = this.invdet(a * d - b * c, p); + return [(d * mul) % p, (-b * mul) % p, (-c * mul) % p, (a * mul) % p]; + } + + applique(mat, cplnum) { + const [a, b, c, d] = this.convertStringToMatrix(mat); + const [x, y] = cplnum; + const xout = (a * x + b * y) % this.prem; + const yout = (c * x + d * y) % this.prem; + return [xout, yout]; + } + + /** + * Pour chaque caractère de la chaine msg, on vérifie qu'il est + dans l'alphabet des caractères autorisés : si oui on le garde, + sinon on le remplace par un underscore + * @param monTexte + */ + filterCharacters(monTexte) { + const convertedText = monTexte.split('').map((letter) => { + if (this.alphab.indexOf(letter) === -1) { + letter = '_'; + } + return letter; + }); + + return convertedText.join(''); + } + + /** + * on entre une définition de matrice en écrivant 4 nombres dans un input + * @param stringMatrix + */ + convertStringToMatrix(stringMatrix: string) { + return stringMatrix.replace(',', ' ').replace(' ', ' ').slice(' '); + } + + /** + * chiffrer avec la méthode du CIL-gometz + * @param someText + */ decrypt(sometext) { - // const decipher = Crypto.createDecipheriv(this.algorithm, this.key, this.initial_vector); - // - // decipher.on('readable', () => { - // this.otherCipheredText = decipher.read().toString('utf8'); - // - // console.log('otherCipheredText decrypt', this.otherCipheredText); - // }); - // - // decipher.write(sometext, 'hex'); - // decipher.end(); return sometext; } @@ -114,7 +225,6 @@ export class CipheringComponent implements OnInit { for (let i = 0; i < array.length; i++) { result += String.fromCharCode(array[i] - 10); } - console.info('value, result', value, result); // remove salt characters if (removeBeginning) { result = result.slice(this.salt.length, -1); @@ -169,6 +279,7 @@ export class CipheringComponent implements OnInit { md5(someText) { return CipherLib.md5(someText); } + getEntropy(s) { let sum = 0, len = s.length; @@ -178,6 +289,7 @@ export class CipheringComponent implements OnInit { }); return sum; } + process(s, evaluator) { let h = Object.create(null), k;