2021-02-08 11:32:58 +01:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2021-02-10 11:23:09 +01:00
|
|
|
import CipherLib from './lib_cipher';
|
2021-02-09 18:26:13 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2021-02-09 18:35:29 +01:00
|
|
|
|
2021-02-09 18:26:13 +01:00
|
|
|
// documentation:
|
|
|
|
// https://medium.com/@spatocode/symmetric-encryption-in-javascript-bcb5fd14c273
|
|
|
|
// https://nodejs.org/api/crypto.html
|
2021-02-10 10:35:36 +01:00
|
|
|
// https://preview.npmjs.com/package/ecma-nacl
|
2021-02-08 11:32:58 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'app-ciphering',
|
|
|
|
templateUrl: './ciphering.component.html',
|
|
|
|
styleUrls: ['./ciphering.component.scss'],
|
|
|
|
})
|
|
|
|
export class CipheringComponent implements OnInit {
|
2021-02-10 10:35:36 +01:00
|
|
|
public plainText = 'le texte à chiffrer, coucou !';
|
|
|
|
// public plainText = '1234';
|
2021-02-09 22:47:57 +01:00
|
|
|
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';
|
2021-02-09 18:26:13 +01:00
|
|
|
algorithm = 'aes-192-cbc';
|
2021-02-10 10:35:36 +01:00
|
|
|
public salt = 'ou du poivre heh';
|
2021-02-09 22:47:57 +01:00
|
|
|
public initial_vector = '';
|
2021-02-09 18:35:29 +01:00
|
|
|
public key;
|
2021-02-09 22:47:57 +01:00
|
|
|
public otherCipheredText = 'le texte à chiffrer, coucou !';
|
2021-02-08 11:32:58 +01:00
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.encrypt(this.plainText);
|
|
|
|
}
|
|
|
|
|
2021-02-09 22:47:57 +01:00
|
|
|
// ---------------------- make unreadable ----------------------
|
|
|
|
get simpleCipheredText() {
|
|
|
|
return this.convertTextToInt(this.plainText);
|
|
|
|
}
|
2021-02-10 10:35:36 +01:00
|
|
|
|
2021-02-09 22:47:57 +01:00
|
|
|
get simpleCipheredTextWithSalt() {
|
|
|
|
return this.convertTextToInt(this.salt + this.plainText);
|
|
|
|
}
|
2021-02-10 10:35:36 +01:00
|
|
|
get simpleCipheredTextWithSpreadSalt() {
|
|
|
|
return this.convertTextToIntMixedSalt(this.salt + this.plainText);
|
|
|
|
}
|
2021-02-10 11:23:09 +01:00
|
|
|
get simpleCipheredTextWithHashedSalt() {
|
|
|
|
return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText);
|
|
|
|
}
|
2021-02-10 10:35:36 +01:00
|
|
|
get layerOnSpread() {
|
2021-02-10 11:23:09 +01:00
|
|
|
return this.convertIntToText(this.simpleCipheredTextWithHashedSalt, false);
|
2021-02-10 10:35:36 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 22:47:57 +01:00
|
|
|
// ---------------------- make readable ----------------------
|
|
|
|
get simpleDeCipheredText() {
|
|
|
|
return this.convertIntToText(this.cipheredText);
|
|
|
|
}
|
2021-02-10 10:35:36 +01:00
|
|
|
|
2021-02-09 22:47:57 +01:00
|
|
|
get simpleDeCipheredTextWithSalt() {
|
|
|
|
return this.convertIntToText(this.salt + this.cipheredText);
|
|
|
|
}
|
2021-02-10 10:35:36 +01:00
|
|
|
get simpleDeCipheredTextWithSpreadSalt() {
|
|
|
|
return this.convertIntToTextMixedSalt(this.salt + this.cipheredText);
|
|
|
|
}
|
|
|
|
|
2021-02-09 22:47:57 +01:00
|
|
|
// ---------------------- conversions ----------------------
|
2021-02-08 11:32:58 +01:00
|
|
|
encrypt(someText) {
|
2021-02-09 18:35:29 +01:00
|
|
|
// 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);
|
2021-02-09 22:47:57 +01:00
|
|
|
return someText;
|
2021-02-08 11:32:58 +01:00
|
|
|
}
|
2021-02-09 18:26:13 +01:00
|
|
|
|
|
|
|
decrypt(sometext) {
|
2021-02-09 18:35:29 +01:00
|
|
|
// 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();
|
2021-02-09 22:47:57 +01:00
|
|
|
return sometext;
|
2021-02-09 18:26:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 22:47:57 +01:00
|
|
|
convertTextToInt(value) {
|
|
|
|
let result = '';
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
if (i < value.length - 1) {
|
|
|
|
result += value.charCodeAt(i) + 10;
|
|
|
|
result += '-';
|
|
|
|
} else {
|
|
|
|
result += value.charCodeAt(i) + 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2021-02-10 10:35:36 +01:00
|
|
|
|
|
|
|
convertIntToText(value, removeBeginning = true) {
|
2021-02-09 22:47:57 +01:00
|
|
|
let result = '';
|
|
|
|
const array = value.split('-');
|
2021-02-09 18:26:13 +01:00
|
|
|
|
2021-02-09 22:47:57 +01:00
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
result += String.fromCharCode(array[i] - 10);
|
2021-02-09 18:26:13 +01:00
|
|
|
}
|
2021-02-10 10:35:36 +01:00
|
|
|
console.info('value, result', value, result);
|
|
|
|
// remove salt characters
|
|
|
|
if (removeBeginning) {
|
|
|
|
result = result.slice(this.salt.length, -1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
convertTextToIntMixedSalt(value) {
|
|
|
|
let result = '';
|
|
|
|
let saltIndex = 0;
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
if (i < value.length - 1) {
|
|
|
|
result += value.charCodeAt(i) + 10 + this.salt.charCodeAt(saltIndex);
|
|
|
|
result += '-';
|
|
|
|
} else {
|
|
|
|
result += value.charCodeAt(i) + 10 + this.salt.charCodeAt(saltIndex);
|
|
|
|
}
|
|
|
|
saltIndex++;
|
|
|
|
if (saltIndex >= this.salt.length) {
|
|
|
|
saltIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
convertIntToTextMixedSalt(value) {
|
|
|
|
let result = '';
|
|
|
|
const array = value.split('-');
|
|
|
|
let saltIndex = -1;
|
|
|
|
|
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
// console.log(
|
|
|
|
// 'encodage',
|
|
|
|
// i,
|
|
|
|
// value.charCodeAt(i),
|
|
|
|
// saltIndex,
|
|
|
|
// this.salt.charCodeAt(saltIndex),
|
|
|
|
// value.charCodeAt(i) + 10 + this.salt.charCodeAt(saltIndex)
|
|
|
|
// );
|
|
|
|
|
|
|
|
result += String.fromCharCode(array[i] - 10 - this.salt.charCodeAt(saltIndex));
|
|
|
|
if (saltIndex > this.salt.length) {
|
|
|
|
saltIndex = 0;
|
|
|
|
} else {
|
|
|
|
saltIndex++;
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 22:47:57 +01:00
|
|
|
// remove salt characters
|
|
|
|
return result.slice(this.salt.length);
|
2021-02-09 18:26:13 +01:00
|
|
|
}
|
2021-02-10 11:23:09 +01:00
|
|
|
|
|
|
|
md5(someText) {
|
|
|
|
return CipherLib.md5(someText);
|
|
|
|
}
|
2021-02-08 11:32:58 +01:00
|
|
|
}
|