Decrypting openssl aes-256-cbc using node.js? - node.js

I'm encrypting my file on the command line using:
openssl aes-256-cbc -e -in test.env -out test.env.encrypted
and then trying to decrypt on node.js with:
crypto = require('crypto')
algorithm = 'aes-256-cbc'
password = 'test'
fs = require 'fs'
decrypt = (text) ->
decipher = crypto.createDecipher(algorithm, password)
dec = decipher.update(text, 'hex', 'utf8')
dec += decipher.final('utf8')
dec
file = fs.readFileSync './test.env.encrypted', 'utf-8'
console.log decrypt file
But i'm getting the following error:
TypeError: Bad input string at Decipher.update (crypto.js:168:26) at decrypt (/Users/h/tmp/encrypt_test/test.coffee:10:18) at Object.<anonymous> (/Users/h/tmp/encrypt_test/test.coffee:16:13) at Object.<anonymous> (/Users/h/tmp/encrypt_test/test.coffee:1:1) at Module._compile (module.js:569:30) at Object.CoffeeScript.run (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/index.js:63:23) at compileScript (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:265:29) at compilePath (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:220:14) at Object.exports.run (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:141:20) at Object.<anonymous> (/usr/local/lib/node_modules/coffeescript/bin/coffee:15:45) at Module._compile (module.js:569:30) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:503:32) at tryModuleLoad (module.js:466:12) at Function.Module._load (module.js:458:3) at Function.Module.runMain (module.js:605:10) at startup (bootstrap_node.js:158:16) at bootstrap_node.js:575:3
What am i doing wrong?
UPDATE:
As per James note i'm now sending a "hex" string to my decrypt function:
crypto = require('crypto')
algorithm = 'aes-256-cbc'
password = 'test'
fs = require 'fs'
decrypt = (text) ->
decipher = crypto.createDecipher(algorithm, password)
dec = decipher.update(text, 'hex', 'utf8')
dec += decipher.final('utf8')
dec
file = fs.readFileSync './test.env.encrypted'
console.log decrypt file.toString('hex')
But then it's yielding a new error:
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Decipher.final (crypto.js:181:26)
at decrypt (/Users/h/tmp/encrypt_test/test.coffee:12:19)
at Object.<anonymous> (/Users/h/tmp/encrypt_test/test.coffee:17:13)
at Object.<anonymous> (/Users/h/tmp/encrypt_test/test.coffee:1:1)
at Module._compile (module.js:569:30)
at Object.CoffeeScript.run (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/index.js:63:23)
at compileScript (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:265:29)
at compilePath (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:220:14)
at Object.exports.run (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:141:20)
at Object.<anonymous> (/usr/local/lib/node_modules/coffeescript/bin/coffee:15:45)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
File was encrypted using:
openssl aes-256-cbc -e -in test.env -out test.env.encrypted
with
test
as password

Your call to decipher.update expects a hex encoded string but you're passing a utf-8 string.
Either change your readFileSync to read with hex encoding or change the update call to expect utf-8 input.

Related

auth operation not supported in this environment

I am trying to build a google authentication in signup/login using firebase in my express with handlebar application but on clicking button & doing the pre-requisites as per firebase-auth docs it is giving me the error as follows:
auth/operation-not-supported-in-this-environment
to be precise as follows :
FirebaseError: Firebase: Error (auth/operation-not-supported-in-this-environment).
at createErrorInternal (D:\web developement\myna\sample project\node_modules#firebase\auth\dist\node\index-69e083b1.js:509:47)
at _createError (D:\web developement\myna\sample project\node_modules#firebase\auth\dist\node\index-69e083b1.js:485:32)
at Object. (D:\web developement\myna\sample project\node_modules#firebase\auth\dist\node\index-69e083b1.js:7273:27)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object. (D:\web developement\myna\sample project\node_modules#firebase\auth\dist\node\index.js:5:13) {
code: 'auth/operation-not-supported-in-this-environment',
customData: {}
}
i am not getting a clue, what is causing it & in order to solve it what should i do

"Error: error:0909006C:PEM routines:get_name:no start line" when calling tls.createSecureContext with a self-signed certificate

I've created a self-signed certificate for testing purposes with the pass phrase "foobar" using this command:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365
I'm then trying to call tls.createSecureContext with that certificate-key pair like so:
var tls = require('tls');
var fs = require('fs');
const [key, cert] = [
fs.readFileSync("key.pem"),
fs.readFileSync("cert.pem"),
];
tls.createSecureContext({key, cert})
However, this leads to the following error:
> node main.js
node:internal/tls/secure-context:69
context.setCert(cert);
^
Error: error:0909006C:PEM routines:get_name:no start line
at node:internal/tls/secure-context:69:13
at Array.forEach (<anonymous>)
at setCerts (node:internal/tls/secure-context:67:3)
at configSecureContext (node:internal/tls/secure-context:156:5)
at Object.createSecureContext (node:_tls_common:116:3)
at Object.<anonymous> (/Users/kurtpeek/go/src/github.pie.apple.com/kurt-peek/scratch/node/main.js:9:5)
at Module._compile (node:internal/modules/cjs/loader:1246:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1300:10)
at Module.load (node:internal/modules/cjs/loader:1103:32)
at Module._load (node:internal/modules/cjs/loader:942:12) {
library: 'PEM routines',
function: 'get_name',
reason: 'no start line',
code: 'ERR_OSSL_PEM_NO_START_LINE'
}
Node.js v19.5.0
This question appears to be the same as Error: error:0909006C:PEM routines:get_name:no start line - node but I wasn't able to find an answer there. (For example, I tried applying unix2dos to cert.pem and key.pem, but to no avail). Any ideas on how to fix this?

Getting node API up and running

I am attempting to follow this tutorial on using using node, Express, and postgresql to build and API. All my code for this can be found on Github.
In my server/routes/index.js is the following (the numbering being line numbers).
1: app.get('/api/todos', todosController.list);
2: // app.post('/api/todos/:todoId/items', todoItemsController.create);
3: app.post('/api/todos', todosController.create);
4: app.get('/api/todos/:todoId', todosController.retrieve);
5: app.put('/api/todos/:todoId', todosController.update);
6: app.delete('/api/todos/:todoId', todosController.destroy);
I am having two problems.
The first problem is lines 4 and 5 don't work (all of the other lines do minus line 2). When I use postman/browser to test them I get this error
ReferenceError: TodoItem is not defined
at retrieve (/Users/X-lem/Development/TestAPI/server/controllers/todos.js:25:20)
at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:281:22
at param (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:354:14)
at param (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:410:3)
at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)
at urlencodedParser (/Users/X-lem/Development/TestAPI/node_modules/body-parser/lib/types/urlencoded.js:91:7)
at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:317:13)
at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:335:12)
at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/Users/X-lem/Development/TestAPI/node_modules/body-parser/lib/types/json.js:109:7)
at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:317:13)
at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:335:12)
at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)
Secondly, when I uncomment line 2 the console crashes and I get this error.
ReferenceError: todoItemsController is not defined
at module.exports (/Users/X-lem/Development/TestAPI/server/routes/index.js:9:40)
at Object.<anonymous> (/Users/X-lem/Development/TestAPI/app.js:16:27)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Module.require (module.js:556:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/X-lem/Development/TestAPI/bin/www:3:13)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Function.Module.runMain (module.js:653:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
[nodemon] app crashed - waiting for file changes before starting...
I am new to building API's and am trying to teach myself how to do it. I have tried a number of different troubleshooting methods with no avail. Any help is appreciated, thank you.
Hope I can help a little bit. Your problem #2 was because you did mistype. Please, take a look at your routes/index.js file. You imported controllers to the variable todosController, but on the 9-th line you are using not defined variable todoItemsController. So, just
app.post('/api/todos/:todoId/items', todosController.create); will work.

node- Some file references are missing in stack trace with async/await

Stacktrace is not having the reference to actual file name from where the error is triggered for the protractor.
Spec reporter configuration:
jasmine.getEnv().addReporter(new SpecReporter({spec: {displayStacktrace: true,}}));
Sample spec:
it('Protractor stacktrce issue', async () =>
{
const helper = new SearchHelper();
await helper.doSearch('test');
});
search-helper.ts:
async doSearch(text) {
await browser.get('http://google.com');
await browser.wait(EC.visibilityOf(element(by.name('q1'))), 10000, 'No Element found');
await element(by.name('q1')).click(); //invalid locator
}
1. Stack Trace with browser.wait
✗ Protractor stack trace issue
-Failed: No Element found Wait timed out after 10007ms
Wait timed out after 10007ms
at WebDriverError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
at TimeoutError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:238:5)
at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2107:17
at process._tickCallback (internal/process/next_tick.js:109:7)
From asynchronous test: Error at Suite.<anonymous> (/Users/e2e/sample-spec.ts:27:3)
at Object.<anonymous> (/Users/e2e/sample-spec.ts:15:1)
at Module._compile (module.js:570:32) at Module.m._compile (/Users/node_modules/ts-node/src/index.ts:392:23)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/node_modules/ts-node/src/index.ts:395:12)
2. If we comment browser.wait statement then the stacktrace is
✗ Protractor stack trace issue
Failed: No element found using locator: By(css selector, *[name="q1"])
at WebDriverError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
at NoSuchElementError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:168:5)
at elementArrayFinder.getWebElements.then (/usr/local/lib/node_modules/protractor/lib/element.ts:851:17)
at process._tickCallback (internal/process/next_tick.js:109:7)Error
at ElementArrayFinder.applyAction_ (/usr/local/lib/node_modules/protractor/lib/element.ts:482:23)
at ElementArrayFinder.(anonymous function) [as click] (/usr/local/lib/node_modules/protractor/lib/element.ts:96:21)
at ElementFinder.(anonymous function) [as click] (/usr/local/lib/node_modules/protractor/lib/element.ts:873:14)
at SearchHelper.<anonymous> (/Users/e2e/search-helper.ts:14:34) **at step (/Users/e2e/search-helper.ts:32:23)
at Object.next (/Users/e2e/search-heper.ts:13:53)
at fulfilled (/Users/e2e/search-helper.ts:4:58)**
at process._tickCallback (internal/process/next_tick.js:109:7)
From asynchronous test: Error at Suite.<anonymous> (/Users/e2e/sample-spec.ts:27:3)
at Object.<anonymous> (/Users/e2e/sample-spec.ts:15:1) at Module._compile (module.js:570:32)
at Module.m._compile (/Users/node_modules/ts-node/src/index.ts:392:23)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/node_modules/ts-node/src/index.ts:395:12)
Observe the 2nd stack trace, it has references to search-helper file but it's not there in the first stack trace. It's very important to have this reference in stack trace for debugging.

selenium WebDriverError: connection refused nodejs

node version: v8.5.0
npm version: 5.3.0
geckodriver: v0.19.0
"selenium-webdriver": "^3.5.0"
I'm trying to test selenium webdriver in nodejs, however I keep getting the following error:
/home/app_path/node_modules/selenium-webdriver/lib/promise.js:2634
throw error;
^
WebDriverError: connection refused
at Object.throwDecodedError (/home/app_path/node_modules/selenium-webdriver/lib/error.js:497:15)
at parseHttpResponse (/home/app_path/node_modules/selenium-webdriver/lib/http.js:519:13)
at doSend.then.response (/home/app_path/node_modules/selenium-webdriver/lib/http.js:441:30)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
From: Task: WebDriver.createSession()
at Function.createSession (/home/app_path/node_modules/selenium-webdriver/lib/webdriver.js:769:24)
at Function.createSession (/home/app_path/node_modules/selenium-webdriver/firefox/index.js:572:41)
at createDriver (/home/app_path/node_modules/selenium-webdriver/index.js:170:33)
at Builder.build (/home/app_path/node_modules/selenium-webdriver/index.js:645:16)
at Object.<anonymous> (/home/app_path/app.js:20:2)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at startup (bootstrap_node.js:201:16)
at bootstrap_node.js:626:3
A Mozilla firefox window just opens, and after some time, this error appears in console. This is what I'm trying to do:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build().then(function(){
console.log("log");
driver.get("https://google.co.in");
});
how to fix this?
EDIT Here's the stacktrace
{"value":{"error":"unknown error","message":"connection refused","stacktrace":"stack backtrace:\n 0: 0x4f388c - backtrace::backtrace::trace::h736111741fa0878e\n 1: 0x4f38c2 - backtrace::capture::Backtrace::new::h63b8a5c0787510c9\n 2: 0x442d88 - webdriver::error::WebDriverError::new::hea6d4dbf778b2b24\n 3: 0x449f99 - geckodriver::marionette::MarionetteHandler::create_connection::hf8b6061dba65cdd0\n 4: 0x42ac91 - >::run::hba9181b5aacf8f04\n 5: 0x402c59 - std::sys_common::backtrace::__rust_begin_short_backtrace::h19de262639927233\n 6: 0x40c065 - std::panicking::try::do_call::h6c1659fc4d01af51\n 7: 0x5e38ec - panic_unwind::__rust_maybe_catch_panic\n at /checkout/src/libpanic_unwind/lib.rs:98\n 8: 0x420d32 - >::call_box::h953e5f59694972c5\n 9: 0x5dc00b - alloc::boxed::{{impl}}::call_once<(),()>\n at /checkout/src/liballoc/boxed.rs:661\n - std::sys_common::thread::start_thread\n at /checkout/src/libstd/sys_common/thread.rs:21\n - std::sys::imp::thread::{{impl}}::new::thread_start\n at /checkout/src/libstd/sys/unix/thread.rs:84"}}

Resources