使用RSA Angular和Express.js编码解码JSON
我有一个前端Angular.ts和一个后端Express.js。我正在使用RSA解密前端中的用户凭据,并将所有数据作为加密字符串发送到后端。 当我尝试在后端中捕获数据解密时,它将接收到的对象读为[对象对象],这使得无法解密。这是代码=>:
frontend httpservice.ts :
sendmail(user: { name: any; email: any; host: any }) {
console.log(user);
const encJsonUser = this.rsaHelper.encryptWithPublicKey(
JSON.stringify(user)
);
console.log(encJsonUser);
return this.http.post(environment.apiUrl + '/sendemail', encJsonUser);
}
后端 email.js.js :
router.post('/', async (req, res) => {
let path = null;
const encryptedData = req.body;
console.log("RECEIVED ENCRYPTED BODY: " + encryptedData);
const privateKey = fs.readFileSync("privateKey.key.pem", "utf8");
console.log("toDecryptData::: " + encryptedData);
const decryptedData = decryptedDataFromAngular(encryptedData, privateKey);
console.log("DECRYPTED-DATA: " + decryptedData);
axios.post("http://localhost:8080/pdf", {host: encryptedData.host}).then(async function (response) {
path = response.data.scan_id;
}).then(() => {
emailPdfGenerator(encryptedData);
});
})
function decryptedDataFromAngular(encryptedData, privateKey) {
// encryptedData = Buffer.from(encryptedData, "base64");
const body = JSON.parse(encryptedData);
// console.log("ENCRYPTED DATA 2222:::: " + encryptedData);
console.log("ENCRYPTED DATA 2222:::: " + body);
const decryptedData = crypto.privateDecrypt(
{
key: privateKey,
// In order to decrypt the data, we need to specify the
// same hashing function and padding scheme that we used to
// encrypt the data in the previous step
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
body
);
console.log("decrypted data: ", decryptedData.toString());
console.log("DE RAW DATA" + decryptedData);
return decryptedData;
}
错误消息:
接收到的加密主体:[对象] todecryptdata ::: [对象对象]未定义:1 [对象对象]
SyntaxError:位置1的JSON中的意外令牌O 在json.parse() 在decrypteddatafromangular(c:\ users \ hayan \ desktop \ hsleiden \ year2-in2b \ ipsen5 \ security-check-eck-exck-express-user \ utares \ emaires \ email.js:60:23) 在C:\ users \ Hayan \ Desktop \ Hsleiden \ Yene2-In2b \ ipsen5 \ security-check-exck-express-user \ doutes \ emaires \ email.js:31:27 在layer.handle [as hander_request](c:\ users \ hayan \ desktop \ hsleiden \ year2-in2b \ ipsen5 \ security-check-eck-eck-express-user \ node_modules \ node_modules \ express \ express \ express \ lib \ lib \ router \ router \ router \ layer.js:95:95:95:5):5) 在Next(C:\ users \ Hayan \ desktop \ hsleiden \ year2-in2b \ ipsen5 \ security-check-eck-express-user \ node_modules \ express \ express \ lib \ lib \ router \ router \ rout.js:144:13) 在Route.disPatch(C:\ Users \ Hayan \ desktop \ hsleiden \ eary2-in2b \ ipsen5 \ security-check-eck-express-user \ node_modules \ node_modules \ express \ express \ express \ lib \ router \ router \ rout.js:114:3) 在layer.handle [as hander_request](c:\ users \ hayan \ desktop \ hsleiden \ year2-in2b \ ipsen5 \ security-check-eck-eck-express-user \ node_modules \ node_modules \ express \ express \ express \ lib \ lib \ router \ router \ router \ layer.js:95:95:95:5):5) 在C:\ users \ Hayan \ desktop \ hsleiden \ year2-in2b \ ipsen5 \ security-check-exck-express-user \ node_modules \ express \ express \ lib \ lib \ router \ router \ index.js:284:15 在function.process_params(c:\ users \ hayan \ desktop \ hsleiden \ year2-in2b \ ipsen5 \ security-check-eck-express-user \ node_modules \ node_modules \ express \ express \ express \ lib \ lib \ router \ router \ index.js:js:346:12):12):12) 在下一步(C:\ users \ Hayan \ desktop \ hsleiden \ year2-in2b \ ipsen5 \ security-check-eck-express-user \ node_modules \ express \ express \ express \ lib \ lib \ router \ index.js:280:10) [nodemon]应用程序崩溃了 - 启动之前等待文件更改...
I have a frontend Angular.ts and a backend Express.js. I am decrypting user credentials in the frontend using RSA and sending all the data as an encrypted string to the backend.
When I try to catch the data in the backend to decrypt it, it reads the received object as [object Object] which makes it impossible to decrypt. This is the code =>:
FRONTEND httpService.ts:
sendmail(user: { name: any; email: any; host: any }) {
console.log(user);
const encJsonUser = this.rsaHelper.encryptWithPublicKey(
JSON.stringify(user)
);
console.log(encJsonUser);
return this.http.post(environment.apiUrl + '/sendemail', encJsonUser);
}
BACKENDemail.js:
router.post('/', async (req, res) => {
let path = null;
const encryptedData = req.body;
console.log("RECEIVED ENCRYPTED BODY: " + encryptedData);
const privateKey = fs.readFileSync("privateKey.key.pem", "utf8");
console.log("toDecryptData::: " + encryptedData);
const decryptedData = decryptedDataFromAngular(encryptedData, privateKey);
console.log("DECRYPTED-DATA: " + decryptedData);
axios.post("http://localhost:8080/pdf", {host: encryptedData.host}).then(async function (response) {
path = response.data.scan_id;
}).then(() => {
emailPdfGenerator(encryptedData);
});
})
function decryptedDataFromAngular(encryptedData, privateKey) {
// encryptedData = Buffer.from(encryptedData, "base64");
const body = JSON.parse(encryptedData);
// console.log("ENCRYPTED DATA 2222:::: " + encryptedData);
console.log("ENCRYPTED DATA 2222:::: " + body);
const decryptedData = crypto.privateDecrypt(
{
key: privateKey,
// In order to decrypt the data, we need to specify the
// same hashing function and padding scheme that we used to
// encrypt the data in the previous step
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
body
);
console.log("decrypted data: ", decryptedData.toString());
console.log("DE RAW DATA" + decryptedData);
return decryptedData;
}
error message:
RECEIVED ENCRYPTED BODY: [object Object] toDecryptData::: [object Object] undefined:1 [object Object] ^
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse ()
at decryptedDataFromAngular (C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\routes\email.js:60:23)
at C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\routes\email.js:31:27
at Layer.handle [as handle_request] (C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\node_modules\express\lib\router\index.js:346:12)
at next (C:\Users\hayan\Desktop\HSLeiden\Year2-IN2B\IPSEN5\Security-Check-Express-User\node_modules\express\lib\router\index.js:280:10)
[nodemon] app crashed - waiting for file changes before starting...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我停止使用加密货币,并且我正在使用Forge解决问题,因为加密无济于事。
这是我的代码:
I stopped using crypto, and I am using forge to solve the problem because crypto just didn't help me.
This is my code: