Current theory hold that they are generated by various sources within the galaxy and then accelerated by some unknown process. Supernova shock waves are one possible mechanism for this acceleration. The galactic magnetic field confines most of these particles which circulate for about 10 million years. During that time they will interact with the interstellar medium which contains trace amounts of hydrogen and helium. These interactions slow the nuclei down through ionizational energy loss and change the overall composition through nuclear spallation.
Spallation refers to the collisional fragmentation of heavy nuclei into lighter nuclei.
Some of the shorter lived nuclei will also decay. Thus the measured composition and energy of the cosmic rays will be quite different from the composition and energy at their source. This difference between the source values and the measured values is further complicated by conditions within our own solar system. Here the cosmic rays interact with the outgoing solar wind. The solar wind consists of particles accelerated in the sun's corona and traveling outward into space.