use crypto lib
This commit is contained in:
parent
5f4c2e04af
commit
1ab5255bfa
|
@ -1,17 +1,21 @@
|
||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
const forge = require('node-forge');
|
const crypto = require('crypto');
|
||||||
|
// documentation:
|
||||||
|
// https://medium.com/@spatocode/symmetric-encryption-in-javascript-bcb5fd14c273
|
||||||
|
// https://nodejs.org/api/crypto.html
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-ciphering',
|
selector: 'app-ciphering',
|
||||||
templateUrl: './ciphering.component.html',
|
templateUrl: './ciphering.component.html',
|
||||||
styleUrls: ['./ciphering.component.scss'],
|
styleUrls: ['./ciphering.component.scss'],
|
||||||
})
|
})
|
||||||
export class CipheringComponent implements OnInit {
|
export class CipheringComponent implements OnInit {
|
||||||
public plainText = 'le texte à chiffrer';
|
public plainText = 'le texte à chiffrer, coucou !';
|
||||||
public cipheredText = '';
|
public cipheredText = '';
|
||||||
public salt = forge.random.getBytesSync(128);
|
algorithm = 'aes-192-cbc';
|
||||||
public key = forge.random.getBytesSync(128);
|
public salt = crypto.randomBytes(16);
|
||||||
|
public initial_vector = crypto.randomBytes(16);
|
||||||
|
public key = '';
|
||||||
public otherCipheredText: any;
|
public otherCipheredText: any;
|
||||||
|
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
@ -21,27 +25,42 @@ export class CipheringComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
encrypt(someText) {
|
encrypt(someText) {
|
||||||
// generate a random key and IV
|
this.key = crypto.scryptSync(someText, 'salt', 24);
|
||||||
// Note: a key size of 16 bytes will use AES-128, 24 => AES-192, 32 => AES-256
|
this.initial_vector = crypto.randomBytes(16);
|
||||||
// const key = forge.random.getBytesSync(16);
|
|
||||||
// const iv = forge.random.getBytesSync(16);
|
|
||||||
|
|
||||||
/* alternatively, generate a password-based 16-byte key
|
const cipher = crypto.createCipheriv(this.algorithm, this.key, this.initial_vector);
|
||||||
var salt = forge.random.getBytesSync(128);
|
|
||||||
var key = forge.pkcs5.pbkdf2('password', salt, numIterations, 16);
|
cipher.on('readable', () => {
|
||||||
*/
|
this.cipheredText = cipher.read().toString('hex');
|
||||||
|
console.log(cipher.read().toString('hex'));
|
||||||
|
});
|
||||||
|
|
||||||
|
let output = '';
|
||||||
|
|
||||||
// encrypt some bytes using CBC mode
|
|
||||||
// (other modes include: ECB, CFB, OFB, CTR, and GCM)
|
|
||||||
// Note: CBC and ECB modes use PKCS#7 padding as default
|
|
||||||
// const cipher = forge.cipher.createCipher('AES-CBC', this.key);
|
|
||||||
// cipher.start({ iv: this.salt });
|
|
||||||
// cipher.update(forge.util.createBuffer(someText));
|
|
||||||
// cipher.finish();
|
|
||||||
// const encrypted = cipher.output;
|
|
||||||
// this.cipheredText = encrypted.toHex();
|
|
||||||
this.cipheredText = someText;
|
this.cipheredText = someText;
|
||||||
// outputs encrypted hex
|
// outputs encrypted hex
|
||||||
console.log(this.cipheredText);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
convertTextToInt(someText) {
|
||||||
|
let output = '';
|
||||||
|
|
||||||
|
for (let i = 0; i < someText.length; i++) {
|
||||||
|
output += someText[i].charCodeAt(0);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue