44
55const crypto = require ( 'crypto' )
66
7- exports . encryptText = ( algor , key , iv , text , encoding ) => {
7+ const encryptText = ( algor , key , iv , text , encoding ) => {
88 const cipher = crypto . createCipheriv ( algor , key , iv )
99 encoding = encoding || 'binary'
1010 let result = cipher . update ( text , 'utf8' , encoding )
1111 result += cipher . final ( encoding )
1212 return result
1313}
1414
15- exports . decryptText = ( algor , key , iv , text , encoding ) => {
15+ const decryptText = ( algor , key , iv , text , encoding ) => {
1616 const decipher = crypto . createDecipheriv ( algor , key , iv )
1717 encoding = encoding || 'binary'
1818 let result = decipher . update ( text , encoding )
1919 result += decipher . final ( )
2020 return result
2121}
2222
23- /*
24- const data = 'This is a test'
25- const password = 'hello'
26- const algorithm = 'aes256'
27- const args = process.argv.slice(3)
23+ const genIv = ( ) => new Buffer . alloc ( 16 , crypto . pseudoRandomBytes ( 16 ) )
2824
29- data = args[0]
30- password = args[1]
31- algorithm = args[2]
32-
33- console.log('Text: ' + data)
34- console.log('Password: ' + password)
35- console.log('Type: ' + algorithm)
25+ const genKey = ( algorithm , password ) => {
26+ let hash , key
27+ if ( algorithm . includes ( "256" ) ) {
28+ hash = crypto . createHash ( 'sha256' ) // NOSONAR
29+ hash . update ( password )
30+ key = new Buffer . alloc ( 32 , hash . digest ( 'hex' ) , 'hex' )
31+ } else if ( algorithm . includes ( "192" ) ) {
32+ hash = crypto . createHash ( 'sha192' ) // NOSONAR
33+ hash . update ( password ) ;
34+ key = new Buffer . alloc ( 24 , hash . digest ( 'hex' ) , 'hex' )
35+ } else if ( algorithm . includes ( "128" ) ) {
36+ hash = crypto . createHash ( 'md5' ) // NOSONAR
37+ hash . update ( password )
38+ key = new Buffer . alloc ( 16 , hash . digest ( 'hex' ) , 'hex' )
39+ }
40+ return key
41+ }
3642
37- let hash, key
43+ const test_aes = ( ) => {
44+ const data = 'This is a test'
45+ const password = 'hello' // NOSONAR
46+ const algorithm = 'aes256'
47+ //NOSONAR const args = process.argv.slice(3)
48+ // data = args[0]
49+ // password = args[1]
50+ // algorithm = args[2]
51+
52+ console . log ( 'Text: ' + data )
53+ console . log ( 'Password: ' + password )
54+ console . log ( 'Type: ' + algorithm )
3855
39- if (algorithm.includes("256")) {
40- hash = crypto.createHash('sha256')
41- hash.update(password)
42- key = new Buffer.alloc(32,hash.digest('hex'),'hex')
43- } else if (algorithm.includes("192")) {
44- hash = crypto.createHash('sha192')
45- hash.update(password);
46- key = new Buffer.alloc(24,hash.digest('hex'),'hex')
47- } else if (algorithm.includes("128")) {
48- hash = crypto.createHash('md5')
49- hash.update(password)
50- key = new Buffer.alloc(16,hash.digest('hex'),'hex')
56+
57+ const iv = genIv ( )
58+ const key = genKey ( algorithm , password )
59+ console . log ( 'Key: ' + key . toString ( 'base64' ) )
60+ console . log ( 'Salt: ' + iv . toString ( 'base64' ) )
61+
62+ const encText = encryptText ( algorithm , key , iv , data , 'base64' )
63+ console . log ( 'Encrypted: ' + encText )
64+
65+ const decText = decryptText ( algorithm , key , iv , encText , 'base64' )
66+ console . log ( 'Decrypted: ' + decText ) ;
5167}
5268
53- const iv = new Buffer.alloc(16,crypto.pseudoRandomBytes(16))
54- console.log('Key: ' + key.toString('base64'))
55- console.log('Salt: ' + iv.toString('base64'))
56-
57- const encText = encryptText(algorithm, key, iv, data, 'base64')
58- console.log('Encrypted: ' + encText)
69+ const test_decrypt = ( ) => {
70+ let ct = crypto . enc . Base64 . parse ( 'your-cipher-text' )
71+ let iv = crypto . enc . Base64 . parse ( 'your-iv' )
72+ let key = crypto . enc . Base64 . parse ( 'your-key' )
73+ const decrypt = crypto . algo . AES . createDecryptor ( key , { iv } )
74+ const proc = decrypt . process ( ct )
75+ const final = decrypt . finalize ( )
76+ const plain = proc . toString ( crypto . enc . Utf8 ) + final . toString ( crypto . enc . Utf8 )
77+ console . log ( plain )
78+ }
5979
60- const decText = decryptText(algorithm, key, iv, encText, 'base64')
61- console.log('Decrypted: ' + decText);
62- */
80+ test_aes ( )
6381
64- // let ct = crypto.enc.Base64.parse('your-cipher-text')
65- // let iv = crypto.enc.Base64.parse('your-iv')
66- // let key = crypto.enc.Base64.parse('your-key')
67- // const decrypt = crypto.algo.AES.createDecryptor(key, { iv })
68- // const proc = decrypt.process(ct)
69- // const final = decrypt.finalize()
70- // const plain = proc.toString(crypto.enc.Utf8) + final.toString(crypto.enc.Utf8)
71- // console.log(plain)
82+ module . exports = {
83+ genIv,
84+ genKey,
85+ encryptText,
86+ decryptText
87+ }
0 commit comments