Not illegal, at least for most currencies from stable and democratic countries.
I once traded money from a friend for a house. It was based on trust. The friend paid the escrow agent and I paid them by check.
I suppose you could do that for small amounts with a friend. Maybe you could do it for excess money from a trip. For example, if you had Japanese yen, go to the gate of a flight for Japan, say DTW NRT. However, you would have to trade a convenient amount for the other person, say exactly $20 or $100.