The rapid advancement of the artificial intelligence revolution during the past decade has significantly affected vehicular ad hoc networks (VANETs). Several applications have been introduced that must meet the requirements of a VANET, including automatic driving and preaccident alerts and broadcasting of video.The customization of vehicles for implementation of these applications is costly and might not be possible due to many constraints, particularly resource limitations. In order to achieve compliance with the VANET framework within the resource limitations, this article proposes limiting the time frame related with the individual parts of the process. To this end, this article recommends that the resource-intensive ciphertext-policy attribute-based encryption (CP-ABE) task be simplified by virtue of partitioning it into subtasks. This can be achieved by a machine-learning technique (decision tree) in a manner that significantly influences the completion times of all subtasks. An approach based on particle swarm optimization (PSO), called task-distribution PSO (TD-PSO) , is proposed to perform the CP-ABE task distribution on a VANET. The performance of this approach is evaluated by comparison with a genetic algorithm (GA), followed by comparison of these two solutions with the optimal solution proposed by the linear programming (LP) method. Results show that the TD-PSO approach consumes less overhead than the GA. Moreover, comparison with the optimal solution proposed by LP shows that the near-optimal solution obtained using TD-PSO is more accurate than that obtained using the GA in most scenarios.