I don’t know much about physics, but it seems that gravity can be used to “exchange” energy, as well as re-direct it.
I know, just having some fun here. In reality, when a spacecraft zips by a planet and speeds up (picks up energy), the planet, at the same time, slows down (loses energy)*...net result is zero new energy.
*of course the planet doesn’t slow down very much, maybe a trillionith of an MPH or something (probably much less, in fact), so not very noticeable!