whats wrong the decryption function is failing inline bool weakEncrypt2(QByteArray&…
- User
whats wrong the decryption function is failing
inline bool weakEncrypt2(QByteArray& data) {
VM_S_START // C898if (data.isEmpty()) return false; // Generate the password unsigned char random_byte; RAND_bytes(&random_byte, 1); int passwordSize = 9 + (random_byte % 3) * 3; // Random size of 9, 12, or 15 std::vector<unsigned char> password(passwordSize); RAND_bytes(password.data(), passwordSize); // Generate a random salt RAND_bytes(&random_byte, 1); int saltSize = 9 + (random_byte % 3) * 3; // Random size of 9, 12, or 15 std::vector<unsigned char> salt(saltSize); RAND_bytes(salt.data(), saltSize); // Encode the salt as base64 BIO* bio = BIO_push(BIO_new(BIO_f_base64()), BIO_new(BIO_s_mem())); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); BIO_write(bio, salt.data(), saltSize); BIO_flush(bio); BUF_MEM* bptr; BIO_get_mem_ptr(bio, &bptr); std::vector<unsigned char> b64Salt(bptr->length); memcpy(b64Salt.data(), bptr->data, bptr->length); OPENSSL_cleanse(salt.data(), saltSize); OPENSSL_cleanse(bptr->data, bptr->length); OPENSSL_cleanse(bptr, sizeof(*bptr)); BIO_free_all(bio); // SHA256 the salt and password int b64SaltSize = b64Salt.size(); unsigned char keyHash[SHA256_DIGEST_LENGTH]; unsigned char ivHash[SHA256_DIGEST_LENGTH]; SHA256(password.data(), passwordSize, keyHash); SHA256(b64Salt.data(), b64SaltSize, ivHash); std::vector<unsigned char> hashedKey(16); std::vector<unsigned char> hashedIv(16); bio = BIO_push(BIO_new(BIO_f_base64()), BIO_new(BIO_s_mem())); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); if (BIO_write(bio, keyHash, SHA256_DIGEST_LENGTH) != SHA256_DIGEST_LENGTH || BIO_flush(bio) != 1) { BIO_free_all(bio); return false; } BIO_get_mem_ptr(bio, &bptr); std::memcpy(hashedKey.data(), bptr->data, 16); BIO_reset(bio); if (BIO_write(bio, ivHash, SHA256_DIGEST_LENGTH) != SHA256_DIGEST_LENGTH || BIO_flush(bio) != 1) { BIO_free_all(bio); return false; } BIO_get_mem_ptr(bio, &bptr); std::memcpy(hashedIv.data(), bptr->data, 16); BIO_free_all(bio); OPENSSL_cleanse(keyHash, sizeof(keyHash)); OPENSSL_cleanse(ivHash, sizeof(ivHash)); std::vector<unsigned char> cipherBuf(data.size() + EVP_MAX_BLOCK_LENGTH); const EVP_CIPHER* cipherType = EVP_aes_128_cbc(); EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (!ctx) return false; if (EVP_EncryptInit_ex(ctx, cipherType, NULL, hashedKey.data(), hashedIv.data()) != 1) { OPENSSL_cleanse(data.data(), data.size()); data.clear(); EVP_CIPHER_CTX_free(ctx); return false; } OPENSSL_cleanse(hashedKey.data(), hashedKey.size()); OPENSSL_cleanse(hashedIv.data(), hashedIv.size()); int outLength1 = 0; if (EVP_EncryptUpdate(ctx, cipherBuf.data(), &outLength1, reinterpret_cast<unsigned char*>(data.data()), data.size()) != 1) { OPENSSL_cleanse(data.data(), data.size()); data.clear(); EVP_CIPHER_CTX_free(ctx); return false; } int outLength2 = 0; if (EVP_EncryptFinal_ex(ctx, cipherBuf.data() + outLength1, &outLength2) != 1) { OPENSSL_cleanse(data.data(), data.size()); data.clear(); EVP_CIPHER_CTX_free(ctx); return false; } EVP_CIPHER_CTX_free(ctx); OPENSSL_cleanse(data.data(), data.size()); data.clear(); // Generate random string to prepend unsigned char randomByte; RAND_bytes(&randomByte, 1); int prependStrSize = 4 + (randomByte % 7); // Random size between 4 and 10 std::vector<unsigned char> prependStr(prependStrSize); RAND_bytes(prependStr.data(), prependStrSize); data.append(prependStr); data.append(password); data.append(salt); data.append(cipherBuf); VM_S_END // C898 return true; - Assistant