im trying to decrypt data encrypted from c++ using chacha20 but nodejs crypto doesnt…
- User
im trying to decrypt data encrypted from c++ using chacha20 but nodejs crypto doesnt support it then im using the jschacha20 lib
but the nodejs weakDecrypt function decrypted data is wrong whats wrong
bool weakEncrypt(QByteArray& data) { if (data.isEmpty()) return false;
// Generate a salt from the password unsigned char pwhash[SHA256_DIGEST_LENGTH]; SHA256(reinterpret_cast<const unsigned char*>(password.data()), password.size(), pwhash); std::vector<unsigned char> salt(pwhash, pwhash + 32); constexpr size_t NONCE_SIZE = 12; constexpr size_t KEY_SIZE = 32; // Derive key from password unsigned char key[KEY_SIZE]; SHA256(reinterpret_cast<const unsigned char*>(password.data()), password.size(), key); // Derive nonce std::vector<unsigned char> context; context.reserve(password.size() + salt.size()); context.insert(context.end(), password.begin(), password.end()); context.insert(context.end(), salt.begin(), salt.end()); unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256(context.data(), context.size(), hash); std::vector<unsigned char> nonce(hash, hash + NONCE_SIZE); qq << "\nkey\n" << QByteArray::fromStdString(std::string(reinterpret_cast<char*>(key), sizeof(key))).toBase64(); qq << "\nnonce\n" << QByteArray::fromStdString(std::string(reinterpret_cast<char*>(nonce.data()), nonce.size())).toBase64(); qq << "\nsalt\n" << QByteArray::fromStdString(std::string(reinterpret_cast<char*>(salt.data()), salt.size())).toBase64(); qq << "\ncontext\n" << QByteArray::fromStdString(std::string(reinterpret_cast<char*>(context.data()), context.size())).toBase64(); // Encrypt with derived nonce EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (!ctx) return false; if (EVP_EncryptInit_ex(ctx, EVP_chacha20(), nullptr, key, nonce.data()) != 1) { EVP_CIPHER_CTX_free(ctx); return false; } QByteArray ciphertext(data.size(), 0); int ciphertextLen; if (EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char*>(ciphertext.data()), &ciphertextLen, reinterpret_cast<const unsigned char*>(data.data()), data.size()) != 1) { EVP_CIPHER_CTX_free(ctx); return false; } EVP_CIPHER_CTX_free(ctx); OPENSSL_cleanse(key, sizeof(key)); ciphertext.resize(ciphertextLen); data = std::move(ciphertext); return true;}
function weakDecrypt (data, password) { if (!data.length) return false
// Generate a salt from the password const pwhash = crypto.createHash('sha256').update(password).digest() const salt = Buffer.from(pwhash) const NONCE_SIZE = 12 const KEY_SIZE = 32 // Derive key from password const key = crypto.createHash('sha256').update(password).digest() // Derive nonce const context = Buffer.concat([Buffer.from(password), salt]) const hash = crypto.createHash('sha256').update(context).digest() const nonce = hash.slice(0, NONCE_SIZE) // Log values for comparison console.log('\nkey\n', key.toString('base64')) console.log('\nnonce\n', nonce.toString('base64')) console.log('\nsalt\n', salt.toString('base64')) console.log('\ncontext\n', context.toString('base64')) try { const decipher = new JSChaCha20(key, nonce) let decrypted = decipher.decrypt(Buffer.from(data)) decrypted = Buffer.from(decrypted).toString('utf8') return decrypted } catch (error) { console.error('Decryption failed:', error.message) return false } - Assistant