Ahhh yes... The art of silencing your rifle
To get a silent spring rifle, as whisper has mentioned, you will NEED to downgrade your spring big time for starters. Once you have found the lowest power spring you can, I have a couple tricks that work pretty well. As long as your stock piston is plastic, you can easily grind it down to some very thin dimensions in the non vital areas(anywhere on the outside that is not sliding along the cylinder or hitting the cylinder head when fired) to lessen the weight of your piston which reduces the force/sound made by the piston hitting home. Next up is your piston head and cylinder head; I like to add a rubber piece(like the Laylax power cup or whatever) onto the piston head and attach the foam onto the inside of the cylinder head with a tapered hole for the air to channel through. Last thing I do is make sure the piston fits into the cylinder with no wiggles(but not too tight) and check that the spring fits over the spring guide nicely(spring guide needs to be stable) and inside of the piston with no left over room for shakes. This is what I call my silent cylinder setup
Next, fill your stock up full of foam or a light, airy filler/sealant foam to reduce sound reverberation within your gun. Put molding clay around and cracks or holes that lead into the trigger box/cylinder. Once everything on the inside is done, cover any spaces from the inside to the outside of your gun with a thin layer of molding clay(make sure to take the receiver/barrel back out for the last phase of drying so you can disassemble your rifle in the future without messing up your sealant).
Lastly, tightly wrap your barrel in a good amount of Teflon tape(electrical tape) and put that into your external barrel with a 'one piece paper barrel spacer' (a bunch of printing paper wrapped tightly around your barrel which acts as a pretty good spacer and fills cavity that would normally amplify sound).
If you still feel the need, go ahead and put your silencer on(foam filled, or not; just try and use one with the widest diameter possible) and place a layer of tape( duct tape or electrical tape) over the exit hole then fire a good 20 shots to create the smallest exit hole possible(might want to stretch the tape out a tiny bit more so it never touches the bb) making a large expansion chamber from which air/gas/sound leaks very slowly from.
If anything, just keep in mind that you need to make all of your seals as good as possible.
Now for a gas gun... things are a bit more simple. Fill any extra space there might be just as you did for the spring rifle and try to add a little extra sealant/ foam near all your connection points the gas passes through or near. The most important thing to install onto your rifle is a properly crafted silencer; the goal is to get the widest diameter expansion chamber with the smallest possible exit hole diameter. As far as foam goes, I put a thin layer of relatively dense foam lining the inside of the silencer to dampen the echoing inside of it but no more because all that will do is reduce the volume of your expansion chamber so your escaping gas is coming out faster and in a larger volume(so its louder).
Something I heard rumors about was a type of paint you can use on your gun that is light but kind of thick which apparently puts a sort of sound dampening shell over the entire rifle; just something to look into.
Well, hope I helped you achieve your goals, let me know if you have any question man! I hope to see some of everyone elses ideas put up here soon