|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
|
|
import * as Crypto from 'crypto';
|
|
|
|
|
// documentation:
|
|
|
|
|
// https://medium.com/@spatocode/symmetric-encryption-in-javascript-bcb5fd14c273
|
|
|
|
|
// https://nodejs.org/api/crypto.html
|
|
|
|
@ -13,9 +14,9 @@ export class CipheringComponent implements OnInit {
|
|
|
|
|
public plainText = 'le texte à chiffrer, coucou !';
|
|
|
|
|
public cipheredText = '';
|
|
|
|
|
algorithm = 'aes-192-cbc';
|
|
|
|
|
public salt = crypto.randomBytes(16);
|
|
|
|
|
public initial_vector = crypto.randomBytes(16);
|
|
|
|
|
public key = '';
|
|
|
|
|
// public salt = Crypto.randomBytes(16);
|
|
|
|
|
// public initial_vector = Crypto.randomBytes(16);
|
|
|
|
|
public key;
|
|
|
|
|
public otherCipheredText: any;
|
|
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
@ -25,34 +26,33 @@ export class CipheringComponent implements OnInit {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let output = '';
|
|
|
|
|
|
|
|
|
|
this.cipheredText = someText;
|
|
|
|
|
// outputs encrypted hex
|
|
|
|
|
console.log(this.cipheredText);
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
convertTextToInt(someText) {
|
|
|
|
|