用 Express 加密
Crypto with Express
我有一个问题,来自 npm 的加密包。
我不想将加密的字符串发送到服务器以使用快速路由器参数解密。
但是有一个大问题,如果我输入无效的字符串,服务器会给我这个错误:
TypeError: Bad input string
at Decipher.update (crypto.js:144:26)
at decrypt (APPDIRECTORY\encryption.js:17:22)
at app.get (APPDIRECTORY\encryption.js:35:17)
at Layer.handle [as handle_request] (APPDIRECTORY\node_modules\express\lib\router\layer.js:95:5)
at next (APPDIRECTORY\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (APPDIRECTORY\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (APPDIRECTORY\node_modules\express\lib\router\layer.js:95:5)
at APPDIRECTORY\node_modules\express\lib\router\index.js:281:22
at param (APPDIRECTORY\node_modules\express\lib\router\index.js:354:14)
at param (APPDIRECTORY\node_modules\express\lib\router\index.js:365:14)
我只是想知道,如何禁用这个错误,并输入我自己的错误!
谢谢!
编辑:
const express = require('express')
const app = express();
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
function encrypt(text){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
app.get('/decrypt/:string', (request, response) => {
let string = request.params.string;
response.send(decrypt(string));
})
app.listen(3030, (request, response) => {
console.log("Server started succesfully!")
})
如果字符串不是 aes-256-ctr 格式,我会出错。那么,如果字符串是 aes-256-ctr 格式,是否有某种方法可以验证字符串?
你只需要try-catch:
function decrypt(text){
try {
var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return { result: dec };
} catch(err) {
return { error: 'INVALID_ENCRYPTED_TEXT' };
}
}
app.get('/decrypt/:string', (request, response) => {
let string = request.params.string;
const dec = decrypt(string);
if (dec.error) {
response.status(400).end();
} else {
response.send(dec.result);
}
});
我有一个问题,来自 npm 的加密包。
我不想将加密的字符串发送到服务器以使用快速路由器参数解密。
但是有一个大问题,如果我输入无效的字符串,服务器会给我这个错误:
TypeError: Bad input string
at Decipher.update (crypto.js:144:26)
at decrypt (APPDIRECTORY\encryption.js:17:22)
at app.get (APPDIRECTORY\encryption.js:35:17)
at Layer.handle [as handle_request] (APPDIRECTORY\node_modules\express\lib\router\layer.js:95:5)
at next (APPDIRECTORY\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (APPDIRECTORY\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (APPDIRECTORY\node_modules\express\lib\router\layer.js:95:5)
at APPDIRECTORY\node_modules\express\lib\router\index.js:281:22
at param (APPDIRECTORY\node_modules\express\lib\router\index.js:354:14)
at param (APPDIRECTORY\node_modules\express\lib\router\index.js:365:14)
我只是想知道,如何禁用这个错误,并输入我自己的错误!
谢谢!
编辑:
const express = require('express')
const app = express();
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
function encrypt(text){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
app.get('/decrypt/:string', (request, response) => {
let string = request.params.string;
response.send(decrypt(string));
})
app.listen(3030, (request, response) => {
console.log("Server started succesfully!")
})
如果字符串不是 aes-256-ctr 格式,我会出错。那么,如果字符串是 aes-256-ctr 格式,是否有某种方法可以验证字符串?
你只需要try-catch:
function decrypt(text){
try {
var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return { result: dec };
} catch(err) {
return { error: 'INVALID_ENCRYPTED_TEXT' };
}
}
app.get('/decrypt/:string', (request, response) => {
let string = request.params.string;
const dec = decrypt(string);
if (dec.error) {
response.status(400).end();
} else {
response.send(dec.result);
}
});