[英]Node.js AES decryption of Base64 String
I'm decrypting messages from Firebase
that is being sent to a Firebase function
. 我正在解密从
Firebase
发送到Firebase function
。
I'm quite new with encryption. 我对加密还很陌生。 But i'm using
AES-256
, with a static IV
(i'm aware that not having a random iv isn't the best solution). 但是我使用的是带有静态
IV
AES-256
(我知道没有随机IV不是最佳解决方案)。
but i'm getting the correct results on both Android
and iOS
but not in node.js
. 但是我在
Android
和iOS
上都得到了正确的结果,但是在node.js
却没有。
The message is Base64 encoded 该消息是Base64编码的
this is my code: 这是我的代码:
const crypto = require('crypto');
const cryptoKey = "MyEncryptionKey1MyEncryptionKey1";
const text = "Cidor8Ph7pZqPw0x2AwIKw=="
let messageToDecrypt = new Buffer(text, "utf8")
let key = new Buffer(cryptoKey, "utf8");
var decipher = crypto.createCipheriv('aes-256-cbc', key, iv);
var decoded = decipher.update(messageToDecrypt,'base64','utf-8');
decoded += decipher.final('utf-8');
text = decoded
console.log(decoded
The code compiles and runs, but it gives me a mumbo-jumbo answer that looks similar to this ŴC p -Q ) 1H& 8pD}5 i ǁ g
该代码可以编译并运行,但它给了我一个巨大的答案,看起来类似于此
ŴC p -Q ) 1H& 8pD}5 i ǁ g
Any ideas on how to improve this code? 关于如何改进此代码的任何想法?
What immediately jumps out to me is that you aren't creating your messageToDecrypt
Buffer correctly. 我突然想到的是您没有正确创建
messageToDecrypt
Buffer。 You are writing: 您正在写:
const text = "Cidor8Ph7pZqPw0x2AwIKw=="
let messageToDecrypt = new Buffer(text, "utf8")
Which means, make a Buffer, the text I'm giving you is utf8 encoded. 这意味着制作一个缓冲区,我给您的文本是utf8编码的。 It isn't utf8 encoded, it is base64 encoded.
它不是 utf8编码的,而是base64编码的。
So you should use: 因此,您应该使用:
const text = "Cidor8Ph7pZqPw0x2AwIKw=="
let messageToDecrypt = Buffer.from(text, "base64")
Consider the difference in Buffers that the encoding makes: 考虑一下编码所产生的缓冲区差异:
> Buffer.from(text, "utf8");
<Buffer 43 69 64 6f 72 38 50 68 37 70 5a 71 50 77 30 78 32 41 77 49 4b 77 3d 3d>
> Buffer.from(text, "base64");
<Buffer 0a 27 68 af c3 e1 ee 96 6a 3f 0d 31 d8 0c 08 2b>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.