88 */
99
1010var forge = require ( "node-forge" ) ,
11- winston = require ( "winston" ) ;
11+ winston = require ( "winston" ) ;
1212
1313// to enable Logging, having winston logger installed is required
1414const 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 */
3434const 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 */
4746const 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
5756try {
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
0 commit comments