Skip to content

Commit 32796d1

Browse files
committed
enhanced code
1 parent 188ebf5 commit 32796d1

6 files changed

Lines changed: 35 additions & 41 deletions

src/allinone/ExampleAsymmetricStringEncryption.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const demonstrateKeyBasedAsymmetricEncryption = () => {
4141
.toString("base64");
4242

4343
// decrypt String
44-
let toDecrypt = Buffer.from(encrypted, "base64");
4544
let decrypted = keypair.privateKey
4645
.decrypt(encrypted, "RSA-OAEP")
4746
.toString("utf8");

src/allinone/ExampleFileEncryption.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ const demonstrateFileEncryption = () => {
4646
// read file and encrypt/decrypt with an ansynchronous control flow
4747
let input;
4848
let encrypted;
49-
let decrypted;
5049
fs.readFile("file.txt", (error, data) => {
5150
input = data;
5251

src/allinone/ExampleStringEncryptionPasswordBased.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,4 @@ const demonstratePasswordBasedSymmetricEncryption = () => {
7575

7676
demonstratePasswordBasedSymmetricEncryption();
7777

78-
demonstratePasswordBasedSymmetricEncryption;
7978
module.exports = { demonstratePasswordBasedSymmetricEncryption, logger };

src/objectOriented/ExampleAsymmetricStringEncryption.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
*/
99

1010
var forge = require("node-forge"),
11-
winston = require("winston");
11+
winston = require("winston");
1212

1313
// to enable Logging, having winston logger installed is required
1414
const logger = winston.createLogger({
15-
format: winston.format.combine(
16-
winston.format.splat(),
17-
winston.format.simple()
18-
),
19-
transports: [
20-
new winston.transports.Console({
21-
format: winston.format.simple(),
22-
handleExceptions: true
23-
})
24-
]
15+
format: winston.format.combine(
16+
winston.format.splat(),
17+
winston.format.simple()
18+
),
19+
transports: [
20+
new winston.transports.Console({
21+
format: winston.format.simple(),
22+
handleExceptions: true
23+
})
24+
]
2525
});
2626

2727
/**
@@ -32,10 +32,9 @@ const logger = winston.createLogger({
3232
* @return {String} encrypted The encrypted String
3333
*/
3434
const pubEncryptString = (stringToEncrypt, publicKey) => {
35-
let toEncrypt = Buffer.from(stringToEncrypt);
36-
let encrypted = publicKey.encrypt(stringToEncrypt, "RSA-OAEP");
37-
encrypted = forge.util.encode64(encrypted);
38-
return encrypted;
35+
let encrypted = publicKey.encrypt(stringToEncrypt, "RSA-OAEP");
36+
encrypted = forge.util.encode64(encrypted);
37+
return encrypted;
3938
};
4039
/**
4140
* A Method to decrypt a String with a Public key. Note that to decrypt the String,
@@ -45,32 +44,32 @@ const pubEncryptString = (stringToEncrypt, publicKey) => {
4544
* @return {String} decrypted The decrypted String
4645
*/
4746
const privDecryptString = (stringToDecrypt, privateKey) => {
48-
let decrypted = privateKey.decrypt(
49-
forge.util.decode64(stringToDecrypt),
50-
"RSA-OAEP"
51-
);
52-
decrypted = decrypted.toString("utf8");
53-
return decrypted;
47+
let decrypted = privateKey.decrypt(
48+
forge.util.decode64(stringToDecrypt),
49+
"RSA-OAEP"
50+
);
51+
decrypted = decrypted.toString("utf8");
52+
return decrypted;
5453
};
5554

5655
// EXAMPLE START
5756
try {
58-
// replace with the actual string
59-
let exampleString = "Test Test 123";
60-
// generate Keypair, in asynchronous encryption both keys need to be related
61-
// and cannot be independently generated keys
62-
// keylength adheres to the "ECRYPT-CSA Recommendations" on "www.keylength.com"
63-
let keypair = forge.rsa.generateKeyPair({ bits: 3072, e: 0x10001 });
64-
// encrypt String
65-
let encrypted = pubEncryptString(exampleString, keypair.publicKey);
66-
// decrypt String
67-
let decrypted = privDecryptString(encrypted, keypair.privateKey);
68-
logger.info(
69-
"Decrypted String and original String are the same: %s",
70-
exampleString.localeCompare(decrypted) === 0 ? "yes" : "no"
71-
);
57+
// replace with the actual string
58+
let exampleString = "Test Test 123";
59+
// generate Keypair, in asynchronous encryption both keys need to be related
60+
// and cannot be independently generated keys
61+
// keylength adheres to the "ECRYPT-CSA Recommendations" on "www.keylength.com"
62+
let keypair = forge.rsa.generateKeyPair({ bits: 3072, e: 0x10001 });
63+
// encrypt String
64+
let encrypted = pubEncryptString(exampleString, keypair.publicKey);
65+
// decrypt String
66+
let decrypted = privDecryptString(encrypted, keypair.privateKey);
67+
logger.info(
68+
"Decrypted String and original String are the same: %s",
69+
exampleString.localeCompare(decrypted) === 0 ? "yes" : "no"
70+
);
7271
} catch (error) {
73-
logger.error(error.message);
72+
logger.error(error.message);
7473
}
7574

7675
// this exports the functions, making them usable in different files by importing them

src/objectOriented/ExampleStringEncryptionKeyBased.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const encryptString = (stringToEncrypt, key, iv) => {
3737
cipher.update(forge.util.createBuffer(stringToEncrypt));
3838
cipher.finish();
3939
let encrypted = cipher.output;
40-
let test = forge.util.encode64(encrypted.data);
4140
return forge.util.encode64(encrypted.data);
4241
};
4342

src/objectOriented/ExampleStringEncryptionPasswordBased.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const encryptString = (stringToEncrypt, key, iv) => {
4949
cipher.update(forge.util.createBuffer(stringToEncrypt));
5050
cipher.finish();
5151
let encrypted = cipher.output;
52-
let test = forge.util.encode64(encrypted.data);
5352
return forge.util.encode64(encrypted.data);
5453
};
5554

0 commit comments

Comments
 (0)