0

Am trying to encrypt and decrypt user data when they login or input some data in my website using php and am able to encrypt it using this

function encryptData($data, $key) {
    $enKey = base64_decode($key);
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
    $encrypted = openssl_encrypt($data, 'aes-256-cbc', $enKey, 0, $iv);
    return base64_encode($encrypted.'::'.$iv);
}

and then decrypt it using this too

function decryptData($data, $key) {
    $enKey = base64_decode($key);
    list($encrypted_data, $iv) = array_pad(explode('::', base64_decode($data), 2), 2, null);
    return openssl_decrypt($encrypted_data, 'aes-256-cbc', $enKey, 0, $iv);
}

If i want to get a particular data it is easier to decrypt and show to user but when the data comes in an array how do i go about decrypting it Like

    $data = array();
    $sql = $conn->prepare("SELECT * FROM user_feedback");
    $sql->execute();
    $res = $sql->get_result();
    if (mysqli_num_rows($res) > 0) {
        while ($row = $res -> fetch_assoc()) {
            $data[] = $row;
        }


        echo decryptData(json_encode($data[0]['feedback']), $key); //It works like this for single data
        echo decryptData(json_encode($data), $key); //but how do i do it like this


    }
    else {
        echo json_encode("NO_DATA");
    }