Using computational algebraic geometry techniques and Hilbert bases of polyhedral cones we derive explicit formulas and generating functions for the number of magic squares and magic cubes.