简体   繁体   English

Crypto++ 和 node.js 组合

[英]Crypto++ and node.js combination

I'm having problems with getting data encrypted in c++, sent to Node.js server and decrypt it there.我在用 C++ 加密数据、发送到 Node.js 服务器并在那里解密时遇到问题。 I'm using Crypto++ which works fine if I encrypt it and decrypt it.我正在使用 Crypto++,如果我对其进行加密和解密,它就可以正常工作。 I tried various modes but nothing seemed to help.我尝试了各种模式,但似乎没有任何帮助。

I set key as 32x 'A' and IV as 16x '\\0' just for getting consistent data我将密钥设置为 32x 'A' 并将 IV 设置为 16x '\\0' 只是为了获得一致的数据

This is code in c++这是 C++ 中的代码

AutoSeededRandomPool rand;

// Generate a random key
SecByteBlock key(0x00, AES::MAX_KEYLENGTH);
//rand.GenerateBlock(key, key.size());
memset(key.BytePtr(), 'A', key.size());

// Generate a random IV
byte iv[AES::BLOCKSIZE];
//rand.GenerateBlock(iv, AES::BLOCKSIZE);
memset(iv, 0, AES::BLOCKSIZE);

char plainText[] = "AAAAAAAAAAAAAAA";
int messageLen = (int)strlen(plainText) + 1;

CFB_Mode<AES>::Encryption cfbEncryption(key, key.size(), iv);
cfbEncryption.ProcessData((byte*)plainText, (byte*)plainText, messageLen);

/*CFB_Mode<AES>::Decryption cfbDecryption(key, key.size(), iv);
cfbDecryption.ProcessData((byte*)plainText, (byte*)plainText, messageLen);*/

unsigned int messageLength = messageLen + key.size();
const auto testData = std::vector<byte>(sizeof(unsigned int) + messageLength);

memcpy((void*)&testData[0], reinterpret_cast<void*>(&messageLength), sizeof(unsigned int));
memcpy((void*)&testData[4], (void*)key.BytePtr(), key.size());
memcpy((void*)&testData[4+key.size()], (void*)plainText, messageLen);

testClient.Send(testData);

testClient.Disconnect();

And this is the code in Node.js这是 Node.js 中的代码

socket.on('data', (data) => {

        var messageSizeBuffer = data.slice(0, 4);
        var messageKeyBuffer = data.slice(4, 36);
        var messageDataBuffer = data.slice(36);

        var decipher = crypto.createDecipher('AES-256-CFB', messageKeyBuffer)
        var dec = Buffer.concat([decipher.update(messageDataBuffer) , decipher.final()]);

        console.log(dec.toString());
    });

I needed to use createDecipheriv and provide the same Initialization Vector as used in encryption.我需要使用 createDecipheriv 并提供与加密中使用的相同的初始化向量。 Beware of hardcoded key and iv, since this is used only for getting consistent data on the other side.注意硬编码的 key 和 iv,因为这仅用于在另一侧获取一致的数据。 Use random generated key and iv.使用随机生成的密钥和 iv。

Code looks like this now代码现在看起来像这样

C++ C++

AutoSeededRandomPool rand;

// Generate a random key
SecByteBlock key(0x00, AES::MAX_KEYLENGTH);
//rand.GenerateBlock(key, key.size());
memset(key.BytePtr(), 'A', key.size());

// Generate a random IV
byte iv[AES::BLOCKSIZE];
//rand.GenerateBlock(iv, AES::BLOCKSIZE);
memset(iv, 0, AES::BLOCKSIZE);

char plainText[] = "AAAAAAAAAAAAAAA";
int messageLen = (int)strlen(plainText) + 1;

CFB_Mode<AES>::Encryption cfbEncryption(key, key.size(), iv);
cfbEncryption.ProcessData((byte*)plainText, (byte*)plainText, messageLen);


unsigned int messageLength = messageLen + key.size() + AES::BLOCKSIZE;
const auto testData = std::vector<byte>(sizeof(unsigned int) + messageLength);

auto currentIndex = 0;
memcpy((void*)&testData[currentIndex], reinterpret_cast<void*>(&messageLength), sizeof(unsigned int));
currentIndex += sizeof(unsigned int);
memcpy((void*)&testData[currentIndex], (void*)key.BytePtr(), key.size());
currentIndex += key.size();
memcpy((void*)&testData[currentIndex], iv, AES::BLOCKSIZE);
currentIndex += AES::BLOCKSIZE;
memcpy((void*)&testData[currentIndex], (void*)plainText, messageLen);

testClient.Send(testData);

testClient.Disconnect();

Node.js节点.js

socket.on('data', (data) => {
    var messageSizeBuffer = data.slice(0, 4);
    var messageKeyBuffer = data.slice(4, 36);
    var messageIvBuffer = data.slice(36, 52);
    var messageDataBuffer = data.slice(52);

    var decipher = crypto.createDecipheriv('AES-256-CFB', messageKeyBuffer, messageIvBuffer)
    var dec = Buffer.concat([decipher.update(messageDataBuffer) , decipher.final()]);

    console.log(dec.toString());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM