我有一个问题,来自npm的加密包。
我不想将加密的字符串发送到服务器以使用快速路由器参数解密。
但是有一个大问题,如果我输入无效字符串,服务器会给我这个错误:
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!")
})
我只是想知道,如何禁用此错误,并输入我自己的错误!
谢谢!
编辑:
$success
如果string不是aes-256-ctr格式,我就会出错。因此,如果字符串是aes-256-ctr格式,那么有什么方法可以验证字符串吗?
答案 0 :(得分:0)
你只需要试一试:
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);
}
});