mirror of
https://framagit.org/framasoft/framadate/funky-framadate-front.git
synced 2023-08-25 13:53:14 +02:00
chiffrement matrice ok
This commit is contained in:
parent
e9a9d1a959
commit
73ad7426ee
@ -2,6 +2,66 @@
|
|||||||
<h2 class="title">
|
<h2 class="title">
|
||||||
Essais de chiffrement pour un framadate zéro knoledge
|
Essais de chiffrement pour un framadate zéro knoledge
|
||||||
</h2>
|
</h2>
|
||||||
|
<section class="boxed-shadow">
|
||||||
|
<h3 class="title">
|
||||||
|
Chiffrement par matrice
|
||||||
|
</h3>
|
||||||
|
Voici un texte que l'on voudrait chiffrer: <br />
|
||||||
|
<input type="text" [(ngModel)]="plainText" (ngModelChange)="encrypt(codingMatrice, plainText)" /><br />
|
||||||
|
Vous pouvez le changer, la page se met à jour automatiquement. Le texte clair fait
|
||||||
|
{{ plainText.length }} caractères.
|
||||||
|
<br />
|
||||||
|
Nous avons besoin d'une matrice de codage de 4 nombres.
|
||||||
|
<br />
|
||||||
|
<input type="text" [(ngModel)]="codingMatrice" />
|
||||||
|
|
||||||
|
<br />
|
||||||
|
<button class="btn is-primary" (click)="encrypt(codingMatrice, plainText)">
|
||||||
|
chiffrer avec la matrice
|
||||||
|
</button>
|
||||||
|
<br />
|
||||||
|
Et voilà la résultat:
|
||||||
|
<strong class="cipher-result">
|
||||||
|
<app-copy-text [textToCopy]="cipheredTextCIL" [displayLabelButton]="false"></app-copy-text>
|
||||||
|
{{ cipheredTextCIL }}
|
||||||
|
</strong>
|
||||||
|
|
||||||
|
<h4 class="title">Comment on a fait?</h4>
|
||||||
|
|
||||||
|
Voici notre alphabet autorisé ( {{ alphab.length }} caractères):
|
||||||
|
<pre>
|
||||||
|
{{ alphab }}
|
||||||
|
</pre
|
||||||
|
>
|
||||||
|
Tout caractère qui n'est pas présent dans notre alphabet sera converti en souligné "_".
|
||||||
|
<br />
|
||||||
|
Notre texte après vérification : <code>{{ filterCharacters(plainText) }}</code>
|
||||||
|
<br />
|
||||||
|
On sépare le texte en {{ separatedCouples.length }} couples de caractères pour le mettre dans une matrice. On
|
||||||
|
prend la pilule rouge.
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li *ngFor="let c; in: separecpl(plainText)">
|
||||||
|
<pre class="debug padded warning">
|
||||||
|
{{ c | json }}
|
||||||
|
</pre
|
||||||
|
>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<fieldset class="couple" *ngFor="let c; in: separatedCouples">
|
||||||
|
<!-- {{c[0]}}-->
|
||||||
|
<!-- {{c[1]}}-->
|
||||||
|
<pre class="debug padded warning">
|
||||||
|
{{ c | json }}
|
||||||
|
</pre
|
||||||
|
>
|
||||||
|
</fieldset>
|
||||||
|
<pre class="debug padded warning">
|
||||||
|
separatedCouples :
|
||||||
|
{{ separatedCouples | json }}
|
||||||
|
</pre
|
||||||
|
>
|
||||||
|
</section>
|
||||||
<section class="boxed-shadow">
|
<section class="boxed-shadow">
|
||||||
<h3 class="title">1) Chiffrement simple</h3>
|
<h3 class="title">1) Chiffrement simple</h3>
|
||||||
<div class="notification is-info">
|
<div class="notification is-info">
|
||||||
|
@ -12,20 +12,30 @@ import CipherLib from './lib_cipher';
|
|||||||
styleUrls: ['./ciphering.component.scss'],
|
styleUrls: ['./ciphering.component.scss'],
|
||||||
})
|
})
|
||||||
export class CipheringComponent implements OnInit {
|
export class CipheringComponent implements OnInit {
|
||||||
public plainText = 'le texte à chiffrer, coucou !';
|
public plainText = 'coucou !';
|
||||||
// public plainText = '1234';
|
// public plainText = '1234';
|
||||||
public cipheredText =
|
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';
|
'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 salt = 'ou du poivre heh';
|
||||||
public initial_vector = '';
|
public initial_vector = '';
|
||||||
public key;
|
public key;
|
||||||
public otherCipheredText = 'le texte à chiffrer, coucou !';
|
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 {
|
ngOnInit(): void {
|
||||||
this.encrypt(this.plainText);
|
this.encrypt(this.codingMatrice, this.plainText);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------- make unreadable ----------------------
|
// ---------------------- make unreadable ----------------------
|
||||||
@ -36,15 +46,19 @@ export class CipheringComponent implements OnInit {
|
|||||||
get simpleCipheredTextWithSalt() {
|
get simpleCipheredTextWithSalt() {
|
||||||
return this.convertTextToInt(this.salt + this.plainText);
|
return this.convertTextToInt(this.salt + this.plainText);
|
||||||
}
|
}
|
||||||
|
|
||||||
get simpleCipheredTextWithSpreadSalt() {
|
get simpleCipheredTextWithSpreadSalt() {
|
||||||
return this.convertTextToIntMixedSalt(this.salt + this.plainText);
|
return this.convertTextToIntMixedSalt(this.salt + this.plainText);
|
||||||
}
|
}
|
||||||
|
|
||||||
get simpleCipheredTextWithHashedSalt() {
|
get simpleCipheredTextWithHashedSalt() {
|
||||||
return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText);
|
return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText);
|
||||||
}
|
}
|
||||||
|
|
||||||
get simpleCipheredTextWithHashedSpreadSalt() {
|
get simpleCipheredTextWithHashedSpreadSalt() {
|
||||||
return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText);
|
return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText);
|
||||||
}
|
}
|
||||||
|
|
||||||
get layerOnSpread() {
|
get layerOnSpread() {
|
||||||
return this.convertIntToText(this.simpleCipheredTextWithHashedSalt, false);
|
return this.convertIntToText(this.simpleCipheredTextWithHashedSalt, false);
|
||||||
}
|
}
|
||||||
@ -57,40 +71,137 @@ export class CipheringComponent implements OnInit {
|
|||||||
get simpleDeCipheredTextWithSalt() {
|
get simpleDeCipheredTextWithSalt() {
|
||||||
return this.convertIntToText(this.salt + this.cipheredText);
|
return this.convertIntToText(this.salt + this.cipheredText);
|
||||||
}
|
}
|
||||||
|
|
||||||
get simpleDeCipheredTextWithSpreadSalt() {
|
get simpleDeCipheredTextWithSpreadSalt() {
|
||||||
return this.convertIntToTextMixedSalt(this.salt + this.cipheredText);
|
return this.convertIntToTextMixedSalt(this.salt + this.cipheredText);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------- conversions ----------------------
|
// ---------------------- conversions ----------------------
|
||||||
encrypt(someText) {
|
|
||||||
// this.key = Crypto.scryptSync(someText, 'salt', 24);
|
/**
|
||||||
// this.initial_vector = Crypto.randomBytes(16);
|
* chiffrer avec la méthode du CIL-gometz
|
||||||
//
|
* avec une matrice
|
||||||
// const cipher = Crypto.createCipheriv(this.algorithm, this.key, this.initial_vector);
|
* @param someText
|
||||||
//
|
*/
|
||||||
// cipher.on('readable', () => {
|
encrypt(matriceDefinition, messageToCipher) {
|
||||||
// this.cipheredText = cipher.read().toString('hex');
|
if (!matriceDefinition || !messageToCipher) {
|
||||||
// console.log(cipher.read().toString('hex'));
|
return null;
|
||||||
// });
|
}
|
||||||
//
|
let cipheredText = '';
|
||||||
//
|
const lcpl = this.separecpl(this.filterCharacters(messageToCipher));
|
||||||
// this.cipheredText = someText;
|
this.separatedCouples = lcpl;
|
||||||
// // outputs encrypted hex
|
console.log('lcpl', lcpl);
|
||||||
// console.log(this.cipheredText);
|
console.log('this.dico', this.dico);
|
||||||
return someText;
|
/**
|
||||||
|
* 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) {
|
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;
|
return sometext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +225,6 @@ export class CipheringComponent implements OnInit {
|
|||||||
for (let i = 0; i < array.length; i++) {
|
for (let i = 0; i < array.length; i++) {
|
||||||
result += String.fromCharCode(array[i] - 10);
|
result += String.fromCharCode(array[i] - 10);
|
||||||
}
|
}
|
||||||
console.info('value, result', value, result);
|
|
||||||
// remove salt characters
|
// remove salt characters
|
||||||
if (removeBeginning) {
|
if (removeBeginning) {
|
||||||
result = result.slice(this.salt.length, -1);
|
result = result.slice(this.salt.length, -1);
|
||||||
@ -169,6 +279,7 @@ export class CipheringComponent implements OnInit {
|
|||||||
md5(someText) {
|
md5(someText) {
|
||||||
return CipherLib.md5(someText);
|
return CipherLib.md5(someText);
|
||||||
}
|
}
|
||||||
|
|
||||||
getEntropy(s) {
|
getEntropy(s) {
|
||||||
let sum = 0,
|
let sum = 0,
|
||||||
len = s.length;
|
len = s.length;
|
||||||
@ -178,6 +289,7 @@ export class CipheringComponent implements OnInit {
|
|||||||
});
|
});
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
process(s, evaluator) {
|
process(s, evaluator) {
|
||||||
let h = Object.create(null),
|
let h = Object.create(null),
|
||||||
k;
|
k;
|
||||||
|
Loading…
Reference in New Issue
Block a user