来自解密的base64字符串的垃圾

时间:2016-07-13 07:59:11

标签: node.js qt rsa crypto++

Qt客户端生成RSA密钥对,然后将json字符串发送到Nodejs服务器,其中包含RSA公钥和要加密的文本。 NodeJs服务器发回一个base64字符串,其中包含加密的文本(使用node-rsa)和客户端提供的RSA公钥。

我在客户端的解密功能是:

QString Crypto::decrypt(QString b64msg)
{
    //decode the base64 string
    std::string decoded;
    CryptoPP::StringSource ss(b64msg.toStdString(), true,
                           new CryptoPP::Base64Decoder(
                               new CryptoPP::StringSink(decoded)));

    //create a CryptoPP::Integer out of the decoded string
    CryptoPP::Integer encrypted((const byte *)decoded.data(), decoded.size());

    //decrypt using the private key
    CryptoPP::Integer r = privKey.CalculateInverse(rng, encrypted);
    size_t req = r.MinEncodedSize();
    std::string recovered;
    recovered.resize(req);
    r.Encode((byte *)recovered.data(), recovered.size());
    std::cout << "Recovered: " << recovered << std::endl;

    return QString::fromStdString(recovered);
}

我刚从解密函数中获取垃圾。我想我做得很好,我错过了什么?

EDIT 这是一个更详细的迭代:

客户端生成以下密钥对:

-----BEGIN RSA PRIVATE KEY-----
MIICWgIBAAKBgQDCe3LEnSuvjc9sERk5JJTnThaBnTPbQSiSr6fi2pOyREKkOyAG
R1DdW1ND96reWkDIrnKE7jw7sTXs/BTKALlXpFdF1V7FRfiHlB3gRsJYpRzo3Odo
XgS/KG/7W9N9H13t+vmi7HSC2EMiqFrpuSJjJm52MjBW8+wmDMDMmpb0bQIBEQKB
gCJSBTHBYg/r2U9OT77KOGUNx7yFJ0TPQ2UuDpFx3dQqKeC/I8TfaJ+IljkrtL3T
sRRbBSaEZPt5oBrDEro8XPEKdV+a4s6EhNgvLWozFsfbOcXhVLJhWbodnnPsJ7P+
pbXHRySTSXBHkRPjnyoJMlEssaCMopiXztJt7A+9YzYFAkEA5PDjgisaRl3St4dM
VBtH987WCu0G5BprfBlcdCt0Cpyg1x/al42/dZIRnJUcXdofW+yfp5+VvIxcOUS1
e5deIwJBANl37zrZGLtUlnvqj3vQpChcBtUabqeYilnUuK1xt8cWRw/xTmeb8c0l
oFc/3ycrInfonhhMtdshIWSXve3NZC8CQQDXeU6YoQmro5klNAuadAd/0bpkotlP
J+yxCM98ZSHr3rV/LQn4DPBup5ga5rFJRcMpVyzZ/5v8wFbMfOcK6NERAkAmYHWC
2wRdSymdZaDZnUolPWqeE7ksonLEjvNp5uQyMRuZZtGZ0Dm6ymeW7SdhQ9jn+9+p
72ti57qZR/RXJD7bAkAyhbtM8rdRcJ1aXRD08Nq1HI1XM3HcblVDssABQMm3BkyP
prD+pxiGBrw59vZ6OeKo+4J6oAZXRazbnv38aBZO
-----END RSA PRIVATE KEY-----

-----BEGIN PUBLIC KEY-----
MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDCe3LEnSuvjc9sERk5JJTnThaB
nTPbQSiSr6fi2pOyREKkOyAGR1DdW1ND96reWkDIrnKE7jw7sTXs/BTKALlXpFdF
1V7FRfiHlB3gRsJYpRzo3OdoXgS/KG/7W9N9H13t+vmi7HSC2EMiqFrpuSJjJm52
MjBW8+wmDMDMmpb0bQIBEQ==
-----END PUBLIC KEY-----

然后客户端向NodeJs服务器发送json请求。请求的格式为

{"command": "encrypt","phrase":"TestString","pubkey":<PEM public key>}

这是NodeJs方面的代码片段:

[...]
var WebSocketServer = require('ws').Server;
var NodeRSA = require('node-rsa');
[...]
var TCP_PORT = 8081;
var wss = new WebSocketServer({port: TCP_PORT});

wss.on('connection', function connection(socket) {
    socket.on('message', function (data) {
        var request;
        try {
            request = JSON.parse(data);
            console.log("Request:");
            console.log(data);
        } catch (e) {
            console.log("Error parsing message: " + e);
            socket.send("json or gtfo");
            return;
        }
        switch (request.command) {
            case "encrypt":
                console.log("phrase: " + request.phrase);
                console.log("pubkey: " + request.pubkey);
                var keypu = new NodeRSA(request.pubkey);
                var resultB64 = keypu.encrypt(request.phrase, "base64", 'utf8');
                socket.send(resultB64);
                [...]

此迭代中的“resultB64”是

wBuWpfelCB9yIOUQ6ZMPMCPJvn+NYfgjywh4Ye7ybYvNqJdf05yqgrqudkwAJT9+VaIIS/KE/38WvGxiTTY5qZm+rlRrY5OBDZzKRRH4iEKp1NFRJXhh9ZtHPRM/2JfVMUYSFdIgyP3CojIUz2dyLfZu7XLn3RYXI29EJEyHxKM=

事实上,如果我也传递私钥并尝试在服务器端解密,则解密成功,所以我假设Nodejs方面没问题。

@Kombinator:我注意到在客户端,如果我替换它,我会获得完全相同的(垃圾)结果:

std::string decoded;
CryptoPP::StringSource ss(b64msg.toStdString(), true,
                       new CryptoPP::Base64Decoder(
                           new CryptoPP::StringSink(decoded)));

与QByteArray等效:

std::string decoded;
QByteArray fromB64 = QByteArray::fromBase64(b64msg.toLatin1());
decoded = fromB64.toStdString();

1 个答案:

答案 0 :(得分:0)

您必须使用单元测试来解决此问题。首先使用内联值调用来降低复杂方法。 仅将std:string与测试值一起使用。另一方面,你应该使用QByteArray作为base64的东西。